Search code examples
javaandroidandroid-layoutandroid-actionbaroncreate

Android SDK, action bar color


So in my app I have the action bar color code as this in the create event of the "activity_main":

android.app.ActionBar redd = getActionBar();
redd.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#BA0202")));

And it changes,But I'd like to use a string. I have tried "R.string.ColorRed" In replacement of the "#BA0202", and it gives me an error. The reason why I want to do this and not change my app theme is because when the user clicks a button it will change the action bar color to a specific color. And another reason why I want to use the string is because if I have this code on multiple pages I would have to change the color code (#BA0202) to a different one on every page if I wanted to change the -Main Theme Of The ActionBar color-


Solution

  • Change your

    redd.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#BA0202")));
    

    to

    redd.setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.string.ColorRed))));
    

    and to fixed the problem thats causing this Change actionbar color programmatically more than once

    append this code to your actionbar :[assumng this problem existed in your app]

    redd.setDisplayShowTitleEnabled(false);
    redd.setDisplayShowTitleEnabled(true);
    

    hope it helps :)