I have an ActionBarActivity
which has fragments for a navigation drawer. When an item in the navigation drawer is pressed, it starts the corresponding fragment.
In each of these fragments, the onCreate()
method looks like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTheme(R.style.AppTheme);
setHasOptionsMenu(true);
}
The theme that is applied is different for each of these fragments however.
I noticed that the setTheme()
method does not appear to change the colour of the ActionBar
or status bar as declared in the styles.xml
from which I am referencing:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/brand_red_primary</item>
<item name="colorPrimaryDark">@color/brand_red_primaryDark</item>
<item name="android:actionBarStyle">@style/AppTheme.ActionBarStyle</item>
</style>
Again, there are different styles with different colorPrimary
and colorPrimaryDark
attributes for the different fragments in my activity.
So, the setTheme()
method does not change the colour of the ActionBar
or status bar, but it does change some things like the colour of the pullback feature in a listview, for example.
Am I missing something, or is there something I need to do for setTheme()
to change the ActionBar
and status bar colour?
I tried the solution posted here but it gives me exactly the same result as what I had tried before (using setTheme()
).
I decided to use the following method to solve my issue:
private void updateActionBar(int title, int actionBarColor, int statusBarColor) {
if (title != 0) {
getSupportActionBar().setTitle(getString(title));
getSupportActionBar().setSubtitle(null);
}
if (actionBarColor != 0) {
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(actionBarColor)));
}
if (statusBarColor != 0) {
if (Build.VERSION.SDK_INT >= 21) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(getResources().getColor(statusBarColor));
}
}
}
I have been calling this method before changing the Fragment
, but have also kept using getActivity().setTheme(R.style.AppTheme);
or getActivity().setTheme(R.style.AppTheme_colourTwo);
in the onCreate
method of the Fragment
.