2

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?

UPDATE:

I tried the solution posted here but it gives me exactly the same result as what I had tried before (using setTheme()).

Community
  • 1
  • 1
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125

3 Answers3

4

You need to call setTheme(..) before super.onCreate(..),and It works for me:

setTheme(R.style.YourThemeName);
super.onCreate(savedInstanceState);
Badr
  • 2,021
  • 1
  • 21
  • 27
1

You can change background color , text color of the action bar via defining a custom style.

Edit your styles.xml file in your project

<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
       parent="@style/Theme.AppCompat">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="android:actionMenuTextColor">@color/actionbar_text</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="actionMenuTextColor">@color/actionbar_text</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
       parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>

    <!-- Support library compatibility -->
    <item name="titleTextStyle">@style/MyActionBarTitleText</item>
</style>

<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
       parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/actionbar_text</item>
    <!-- The textColor property is backward compatible with the Support Library -->
</style>

<!-- ActionBar tabs text -->
<style name="MyActionBarTabText"
       parent="@style/Widget.AppCompat.ActionBar.TabText">
    <item name="android:textColor">@color/actionbar_text</item>
    <!-- The textColor property is backward compatible with the Support Library -->
</style>
</resources>

and then use the setTheme() to set the CustomActionBarTheme

for more info check here

Also if you are trying to implement material theme, do the below and it applies to the places as mentioned in the image you commented with

<style name="AppTheme" parent="AppTheme.Base"/>

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">

    <!-- ...and here we setting appcompat’s color theming attrs -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <!-- colorAccent is used as the default value for colorControlActivated
     which is used to tint widgets -->
    <item name="colorAccent">@color/colorAccent</item>

</style>

where my application uses the AppTheme (..usually specified in the manifest..as below )

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    ..
    android:theme="@style/AppTheme">
Nishant Srivastava
  • 4,723
  • 2
  • 24
  • 35
  • That could work, but I thought that the `colorPrimary` and `colorPrimaryDark` attributes would change the ActionBar as shown in the [diagram from the developer website](https://developer.android.com/training/material/images/ThemeColors.png). Also, I don't think this would change the status bar colour. – Farbod Salamat-Zadeh Apr 08 '15 at 20:22
1

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.

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125