Changing Actionbar Color in Style

Asked

Viewed 10,894 times

0

I’m studying to develop Android applications, I’m making an App, but I can’t change the color of my Actionbar. I’m developing for Android Kitkat.

This is my style.xml

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">#009933</item>
</style>
</resources>

In my manifest is the theme 'Apptheme'.

What might be going on?

  • You are using Appcompat?

2 answers

4

I was racking my brain, but it’s solved. If you’re using API 11 or higher, just add, in the Oncreate method in the file. java from your app’s Main Activity (Mainactivity.java, for example):

android.support.v7.app.ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#cor")));

Of course, remember to change the #color to the hexadecimal code of the color you want.

If it doesn’t work, try this variation:

ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#cor")));

In my case, it only worked the first way. It’s the easiest to change without having to move or create files in the values folder

  • Where do I put this code?

-2

If you are using Appcompat Material Design, you will define by primary color:

<style name="AppTheme" parent="Theme.AppCompat.Light">
   <item name="colorPrimary">@color/material_blue_grey_900</item>
</style>

Otherwise (previous Apis), use the Widget style in your Actionbar’s Parent attribute, for example:

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.