Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Support ActionBar won't display the right color with API 21

I'm trying to make an appcompat theme but the color isn't working, and the icon won't show either... maybe im missing something. Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <color name="background_test">#410000</color>
    <color name="font_general">#ffffff</color>
    <style name="MyTheme"
        parent="@style/Theme.AppCompat">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>

    </style>
    <style name="AudioFileInfoOverlayText">
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingBottom">2dp</item>
        <item name="android:textColor">#ffffffff</item>
        <item name="android:textSize">18sp</item>
        <item name="android:shadowColor">#000000</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">1</item>
        <item name="android:shadowRadius">1</item>
    </style>

    <!-- general styles for the action bar -->
    <style name="MyActionBar"
        parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:icon">@drawable/ic_launcher</item>
        <item name="icon">@drawable/ic_launcher</item>
        <item name="background">@color/background_test</item>
        <item name="android:background">@color/background_test</item>
  </style>

</resources>

What should i add to make it work?

EDIT:

It won't work with a toolbar either... After i set:

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);

It will lose all the user defined theme and will only get the parent theme... Here are my xmls (Yes i have a lot of redundant/test values as i wanted to try everything)

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        background="?attr/colorPrimary"
        app:popupTheme="@style/MyActionBarStyle"
        android:background="?attr/colorPrimary"
        app:theme="@style/MyActionBarStyle"
        />

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- the theme applied to the application or activity -->
    <color name="background_test">#410000</color>
    <color name="font_general">#ffffff</color>
    <!-- general styles for the action bar -->
    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="actionBarStyle">@style/MyActionBarStyle</item>
        <item name="android:panelColorBackground">@color/background_test</item>
        <item name="android:headerBackground">@color/background_test</item>
        <item name="android:colorBackground">@color/background_test</item>
        <item name="android:backgroundSplit">@color/background_test</item>

        <item name="backgroundSplit">@color/background_test</item>
        <item name="colorAccent">@color/background_test</item>

        <!-- Here we setting appcompat’s actionBarStyle -->


        <!-- ...and here we setting appcompat’s color theming attrs -->
        <item name="colorPrimary">@color/background_test</item>
        <item name="colorPrimaryDark">@color/background_test</item>
    </style>

    <style name="MyActionBarStyle">
        <item name="colorAccent">@color/background_test</item>

        <!-- Here we setting appcompat’s actionBarStyle -->
        <item name="android:panelColorBackground">@color/background_test</item>
        <item name="android:headerBackground">@color/background_test</item>
        <item name="android:colorBackground">@color/background_test</item>
        <item name="android:backgroundSplit">@color/background_test</item>

        <item name="backgroundSplit">@color/background_test</item>

        <!-- ...and here we setting appcompat’s color theming attrs -->
        <item name="colorPrimary">@color/background_test</item>
        <item name="colorPrimaryDark">@color/background_test</item>
        <item name="android:background">@color/background_test</item>

        <item name="background">@color/background_test</item>
    </style>

    <style name="AudioFileInfoOverlayText">
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingBottom">2dp</item>
        <item name="android:textColor">#ffffffff</item>
        <item name="android:textSize">18sp</item>
        <item name="android:shadowColor">#000000</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">1</item>
        <item name="android:shadowRadius">1</item>
    </style>



</resources>

Screenshot:

http://i.imgur.com/4iohVwP.png

New EDIT: It won't work from the code either...

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
    ActionBar test=getSupportActionBar();

    test.setLogo(R.drawable.ic_launcher); //this is the only one working...
    test.setBackgroundDrawable(new ColorDrawable(0x410000));

    test.setDisplayShowTitleEnabled(false);
    test.setDisplayShowTitleEnabled(true);

EDIT:

There are some bugs reported for the new Appcompat-v7, this is probably a bug:

http://code.google.com/p/android/issues/detail?id=77763&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
like image 415
Arnold Avatar asked Oct 19 '14 17:10

Arnold


People also ask

How to change the default color of the actionbar in Android?

Head over to style.xml file located in the values directory of the res folder. To change the default color of the ActionBar, one has to change the colorPrimary resource. Below is the code to make the ActionBar color ‘green’.

What is the actionbar support library?

Google announced a support library along with the introduction of ActionBar. This library is a part of AppCompat and its purpose is to provide backward compatibility for older versions of Android and to support tabbed interfaces.

What is spigot/CraftBukkit action bar API?

A easy to use API for developers to take advantage of the new Action Bar feature in Minecraft 1.8 A easy to use API for developers to take advantage of the new Action Bar feature in Minecraft 1.8/1.9. If you are running the full Spigot/CraftBukkit 1.8.x you can use the latest version.

How do I add an Actionbar to my activity?

You can add an ActionBar to your activity when running on API level 7 or higher by extending ActionBarActivity class for your activity and setting the activity theme to Theme.AppCompat or a similar theme.


Video Answer


2 Answers

If you are using Toolbar then your actionBarStyle is not used.

You need to style Toolbar directly:

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="..."
    android:background="..."
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:titleTextAppearance="..."
    app:subtitleTextAppearance="..." />

There are more attributes you can customize, these are just examples.

I'm not sure why getSupportActionBar().setBackgroundDrawable(...) doesn't work for you, I've just tried it in various places and it works fine.

like image 186
Chris Banes Avatar answered Oct 25 '22 02:10

Chris Banes


It is an example:

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">  
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/my_color</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/my_darker_color</item>

    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">@color/accent</item>

    <!-- appcompat’s actionBarStyle -->
   <item name="actionBarStyle">@style/MyActionBarStyle</item>

</style>  

Also I suggest you using the new Toolbar.

You can find more info here:

https://chris.banes.me/2014/10/17/appcompat-v21/

like image 20
Gabriele Mariotti Avatar answered Oct 25 '22 03:10

Gabriele Mariotti