Search code examples
androidandroid-layoutandroid-activityandroid-appcompatandroid-theme

ActionBarCompat custom style


I use Android 4.2 and minSdkVersion 17 and the application is for tablet.

i can modify the default theme (change only color for my actionbar) with my but not found.

the my manifest is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.example.customtheme" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

My style.xml is:

<resources>

<style name="AppTheme" parent="@style/Theme.AppCompat">

    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>


<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">#A2a</item>
</style>

and MainActivity is:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();


    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

the layout is banal

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".MainActivity">
</RelativeLayout>

The my problem is: the actionbar is not color (#a2a) but is black


Solution

  • To fix this problem please update your styles.xml as following:

    <resources>
    
        <style name="AppTheme" parent="@style/Theme.AppCompat">
            <item name="actionBarStyle">@style/MyActionBar</item>
            <item name="android:actionBarStyle">@style/MyActionBar</item>
        </style>
    
        <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
            <item name="android:background">@color/ab_color</item>
            <item name="background">@color/ab_color</item>
        </style>
    
    </resources>
    

    line <item name="background">@color/ab_color</item> was added and color was moved to colors.xml because aapt could fail to parse it otherwise.

    and your colors.xml:

    <resources>
        <color name="ab_color">#a2a</color>
    </resources>
    

    More details about this case: https://developer.android.com/training/basics/actionbar/styling.html