How to change Action Bar and Status Bar color in activity?

14,522

Solution 1

if I understand correctly you want to change from java code .. When you call an if try to use this method:

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        changeColor(R.color.red);
    }


    public void changeColor(int resourseColor) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(ContextCompat.getColor(getApplicationContext(), resourseColor));
        }

        ActionBar bar = getSupportActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(resourseColor)));

    }

}

Solution 2

You can do it by defining the custom theme for activity. First you need to create styles.xml file in res/values/ path.

styles.xml

<resources>
    <style name="MyCustomTheme" parent="Theme.AppCompat.Light">
        <item name="android:statusBarColor">@color/color_primary</item>
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/color_primary</item>
    </style>
</resources>  

Then in your AndroidManifest.xml write:

<activity 
       android:name=".MyActivity"
       android:theme="@style/MyCustomTheme"/>  

Or in your activity:

setTheme(android.R.style.MyCustomTheme);
Share:
14,522
Dumbo
Author by

Dumbo

Work: IT Solutions Developer at SEB, Lithuania. Mainly working with hibernate. Creating web services to connect server with ambulance portable radio. Also writing postgreSQL database scripts and supporting developers who are working with android applications.

Updated on June 15, 2022

Comments

  • Dumbo
    Dumbo almost 2 years

    I want to change the color of my action bar and status bar programmatically only on one activity, depending it's state, green for go and red for stop. I want to keep the system wide theme of orange and only have the red or green depending on the outcome of whats inside the activity, with an if Statement.
    How could it be possible to do that?

    • Admin
      Admin about 6 years
      I also need to change the colour of my status bar
  • Admin
    Admin about 6 years
    Needs to be done programmatically within the activity not the XML file
  • Dumbo
    Dumbo about 6 years
    You can use setTheme(android.R.style.MyCustomTheme) method after your if() statement. @zarrexx
  • Admin
    Admin about 6 years
    What do I call "red"? can I call it as a variable named "#FF0000
  • Dumbo
    Dumbo about 6 years
    R.color.red is defined color name in res/values/colors.xml. Example: <color name="red">#770000</color> @zarrexx
  • Dumbo
    Dumbo about 6 years
    @MattDeveloper How to do that in lover version than API 21?
  • Admin
    Admin about 6 years
    no but red is undefined
  • Admin
    Admin about 6 years
    How can i set it within the java rather than xml?
  • Admin
    Admin about 6 years
    changeColor(Color.parseColor("#00FF00")); causes my activity to crash
  • Dumbo
    Dumbo about 6 years
    @zarrexx if you want to use class variable with "#FF0000" value. You can use Color.parseColor(stringVariable); instead of ContextCompat.getColor(getApplicationContext(), resourseColor) and getResources().getColor(resourseColor).
  • Admin
    Admin about 6 years
    Could you show how I could adapt the changeColor method to use changeColor(Color.parseColor("#00FF00"));
  • Dumbo
    Dumbo about 6 years
  • Admin
    Admin about 6 years
    Thank you very much