I am in a fragment and I want to change the actionbar color and view background color when the user clicks on a button. The code I currently have below causes the action bar to go gray no matter what color the user chooses.
private void changeColor( int colorId ) {
ActionBar actionBar = ((ActionBarActivity )getActivity()).getSupportActionBar();
actionBar.setBackgroundDrawable( new ColorDrawable( colorId ) );
this.getView().setBackgroundColor( getResources().getColor( colorId ) );
}
I have the actionbar themed to be blue in my styles.xml not sure if that has any effect. If you have any suggestion please let me know.
Thanks, Nathan
Solution: Had to change:
actionBar.setBackgroundDrawable( new ColorDrawable( colorId ) );
To:
actionBar.setBackgroundDrawable( new ColorDrawable( getResources().getColor( colorId ) ) );
in your style. xml in values folder this will change your action bar color.. Replace #666666 with your selected color code for title background color and replace #000000 for your title text color.
Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar. Step 3: In your MainActivity, add this code in your onCreate method.
If i understand your question correctly that you want to change the color base on user intervention, try this instead.
public void setActionBarColor(int parsedColor){
ActionBar mActionBar = getSupportActionBar();
mActionBar.setBackgroundDrawable(new ColorDrawable(parsedColor));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);
}
using this in a fragment
// check if instance of activity is MyActivity just an example
MyActivity mA = ((MyActivity)getActivity());
mA.setActionBarColor(Color.parseColor("#FFA2C13E"));
hope it helps :)
PS: im using ActionBarActivity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With