0

I've a option in my Preference Screen to changing the ActionBar background color.

It works, but by changing the color, I must close and reopen the app to see the changes.

I have to put the ActionBar color changing code in onResume. But onResume not called by onBackPressed.

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    Misc.setActionBarColor(getActionBar(), null);
}
Anthon
  • 69,918
  • 32
  • 186
  • 246
Azcaq
  • 43
  • 1
  • 8

5 Answers5

1
private SharedPreferences settings;

private OnSharedPreferenceChangeListener listener;

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

    settings = PreferenceManager.getDefaultSharedPreferences(this);

    listener = new OnSharedPreferenceChangeListener() {

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                String key) {
            //Set Your action bar color here.
            //based on arguments
            //String key == android:key="pref_key"(where this is your PreferenceView)
        }
    };
    settings.registerOnSharedPreferenceChangeListener(listener);
}
Sai Phani
  • 259
  • 1
  • 6
0

You can define the color of the ActionBar (and other stuff) by creating a custom Style:

Simply edit the res/values/styles.xml file of your Android project.

For example like this:

<resources>
    <style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
    </style>

    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>

Then set "MyCustomTheme" as the Theme of your Activity that contains the ActionBar.

You can also set a color for the ActionBar like this:

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED));

For more info https://developer.android.com/training/basics/actionbar/styling.html

Tarun Tak
  • 421
  • 1
  • 5
  • 12
  • 1
    My actionbar color is changing, but not by going back to the activity. – Azcaq Apr 10 '15 at 10:46
  • 1
    Store the color in preference and update the color based on the status of the preference. You can put this code in onResume() of Activity – Tarun Tak Apr 10 '15 at 11:11
0

You can change action bar color like this: How do I change the background color of the ActionBar of an ActionBarActivity using XML?

And if you want to restart your activity without showing any animation you can do like this:

    Intent intent = new Intent(MainActivity.this, MainActivity.class);
    startActivity(intent);
    overridePendingTransition(0, 0);
Community
  • 1
  • 1
Varun Kumar
  • 1,241
  • 1
  • 9
  • 18
0

Sai Phani gave you a good answer. If you wanna change the color programmatically depeding on your preferences, you can also put something like this in onResume():

SharedPreferences mysettings = PreferenceManager.getDefaultSharedPreferences(this);
String st1 = mysettings.getString("COLOR_KEY", "default Value here");

if(st1.equals("Blue")){ //Blue example 
  //Change action bar color 
  //Set new value in your preferences
} 
if(st1.equals("Red")){ //Red example

} 

specify the color by doing this:

bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
JaimeToca
  • 46
  • 1
  • 5
0

If you are changing the action bar color from PreferenceScreen, then you should also update this changed color value of action bar somewhere like SharedPreferences or some variable. Use getter/setter for setting action bar colors using this variable in your onResume method.

Narendra
  • 609
  • 1
  • 12
  • 28