KurdTt-
KurdTt-

Reputation: 533

Android ActionBar color

I want to change color in my whole application.

In my AndroidManfiest.xml I've got proper code:

<application
      android:label="@string/AppName"
      android:icon="@drawable/Icon"
      android:theme="@style/MyCustomTheme"
      android:name="MyAppName"
      android:allowBackup="true">

And in values folder, I've got app_theme.xml:

<style name="MyCustomTheme" parent="Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
    <item name="android:icon">@android:color/transparent</item>
    <item name="android:displayOptions"></item>
</style>

<style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@color/actionBarColor</item>
    <item name="android:titleTextStyle">@style/Theme.TitleTextStyle</item>
</style>

<style name="Theme.TitleTextStyle" parent="@android:style/Widget.TextView">
    <item name="android:textSize">22sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">#FFFFFF</item>
</style>

It works very strange... I've got my actionbar color only during application loading, after this returns to the default color.

SOLUTION

public class BaseActivity extends AnnotatedActivity {

@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        if(getSupportActionBar()!=null) {
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#009688")));
            getSupportActionBar().show();
        }
    }

}

and every activity must extends BaseActivity.

Regards

Upvotes: 1

Views: 117

Answers (2)

Tarun Sharma
Tarun Sharma

Reputation: 832

 // not able to get action  
    if(getActionBar()!=null){// actionbar is not null 
       // now  change color of action bar 
       getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E77A2B")));
       //put any color in parseColor.
       getActionBar().show();
     }else{
         System.out.println("getActionbar is null");

     }

Upvotes: 1

capt.swag
capt.swag

Reputation: 10651

Since you are using AppCompat library

<item name="colorPrimary">@color/color_name</item> 

to set color to ActionBar

So you default Theme will be

<style name="MyCustomTheme" parent="Theme.AppCompat.Light">

    <item name="colorPrimary">@color/color_name</item>

    <item name="android:icon">@android:color/transparent</item>
    <item name="android:displayOptions"></item>
</style>

Upvotes: 0

Related Questions