2

I am setting up my Toolbar in my main activity and trying to change the background color with different fragments. So basically, I am trying to access the Toolbar object inside fragment and set different background color. Few things which I have tried to do is :

Access toolbar like: ((ActionBarActivity)getActivity()).getSupportActionBar().setBackgroundColor(XXX);

But I am unable to access the setBackgroundColor function inside fragment. It is perfectly working inside the Main Activity.

nitinku5021a
  • 129
  • 2
  • 9

2 Answers2

4
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

or

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));

Have a look This or This

Community
  • 1
  • 1
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
  • Tried my life out, the color is getting changed but weird thing is: If I set it red, it becomes blue and so on. I think somewhere the color is getting mixed i.e Red is getting painted over something. I even tried of removing the color altogether but no success. I will look into this in more detail and will post back. – nitinku5021a Mar 27 '15 at 12:33
  • Have a look this for reference, http://developer.android.com/guide/topics/ui/actionbar.html#AdvancedStyles – Chirag Savsani Mar 27 '15 at 13:07
  • or Try this=> bar.getActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(248, 248, 248))); – Chirag Savsani Mar 27 '15 at 13:12
  • Hey I found 1 Solution related to your Issue... Try this... ===>> mActionBar.setBackgroundDrawable(new ColorDrawable(0xff00DDED)); mActionBar.setDisplayShowTitleEnabled(false); mActionBar.setDisplayShowTitleEnabled(true); – Chirag Savsani Mar 27 '15 at 13:16
  • Finally it worked !! Thanks a lot Chirag !! android.support.v7.app.ActionBar actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar(); actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F44336"))); – nitinku5021a Mar 27 '15 at 14:50
2

It is very easy to change the ToolBar, Actionbar color.

ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

or

   ActionBar bar = getSupportActionBar();
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59