Friday, November 27, 2015

Android - How change the background color of the Action Bar

It is easy to change the background color of the Action Bar without modify style xml.
You can change the background color by below code.
 // Change the background color into red.
 getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xFFFF0000));

Example : 
[Before to change color]



[After to change color]

[Full Source]
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class ActionBarTestActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_bar_test);
        // Change the background color into red.
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xFFFF0000));
    }
}


No comments:

Post a Comment