How can I change the color in ActionBar (android), for example on blue?

    3 answers 3

    In the style.xml file we write the following

    <!-- стиль для заднего фона --> <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar"> <item name="android:background">@color/actionbar_background</item> <item name="android:titleTextStyle">@style/MyTextAppearance</item> </style> <!-- стиль для текста --> <style name="MyTextAppearance" parent="@android:style/TextAppearance"> <item name="android:textColor">@color/actionbar_text_color</item> </style> <style name="AppTheme" parent="@android:style/Theme.Light"> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> 

    Well, in the minifest we prescribe our topic, for example for the entire application.

     <application android:theme="@style/AppTheme" 

    Well, in the colors.xml file add color

     <color name="actionbar_background">#669f36</color> <color name="actionbar_text_color">#ffffff</color> 

      Can be different

      If using AppCompatActivity :

       getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.BLUE)); 

      If just an Activity :

       getActionBar().setBackgroundDrawable(new ColorDrawable(Color.BLUE)); 

      You can also set a color from the styles, using the colorPrimary attribute, if the ActionBar is a toolbar inside CollapsingToolbarLayout

      And there are many more ways, including assigning the background attribute in markup and styling in the same place.

      Everything depends on the situation.

        Here is a working example for the case.

         public class MyActivity extends AppCompatActivity 

        Changes the color of the background, title and subtitle of the ActionBar.

         // настроим панель Action Bar Android приложения ActionBar actionBar = getSupportActionBar(); actionBar.setBackgroundDrawable(new ColorDrawable(Color.DKGRAY)); actionBar.setTitle((Html.fromHtml("<font color=\"#999999\">" + getString(R.string.app_name) + "</font>"))); actionBar.setSubtitle((Html.fromHtml("<font color=\"#999999\">" + getString(R.string.app_subname) + "</font>"))) 

        ;

        The above example with styles changes the color of the whole Activity, but this will not work, for example, in the case of a preview of a photo, this preview will be filled with a background and become "opaque." I mean an example:

         <item name="android:background">@color/actionbar_background</item>