2

I want to use my custom theme for changing the ActionBar color, but I can't.
Here my styles.xml file:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="NewTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/CustomActionBar</item>
</style>

<style name="CustomActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/pink</item>
</style>

and here my AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>

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

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

Look at this:

android:theme="@style/AppTheme.NoActionBar"

If I change this theme to CustomActionBar or NewTheme everything seems normal, but when I launch my virtual device, I see this:

enter image description here

I researched a lot of resources, but I can't find an answer.
How can I fix this? Please help me!

Adil Saiyad
  • 1,582
  • 2
  • 17
  • 34
Serdar Gun
  • 97
  • 6

3 Answers3

1

You can do this programmatically

ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
  Drawable drawable = getResources().getDrawable(R.drawable.yourColor);
  actionBar.setBackgroundDrawable(drawable);
}
0

Change theme back to android:theme="@style/AppTheme.NoActionBar"

Make sure your layout xml is correct. Here's an example of correct xml layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 xmlns:tools="http://schemas.android.com/tools"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 android:fitsSystemWindows="true"
                                                 >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="BACKGROUND COLOR"
            app:titleTextColor="TITLE TEXT COLOR"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <!--CONTENT-->

</android.support.design.widget.CoordinatorLayout>

Now you can either style toolbar using attributes like android:background or app:titleTextColor, etc. or you can use AppTheme.PopupOverlay to style the action bar.

Oleg Filimonov
  • 1,323
  • 4
  • 13
  • 36
  • Thank you for your answer but I think the problem is not here. Because I didn't change any of letter in layout xml file. I want to use my custom theme and when I delete or change this "android:theme="@style/AppTheme.NoActionBar" in manifest, my app is not working despite all codes are OK. – Serdar Gun Aug 28 '17 at 14:13
0

Do it this way. Define a custom style.

<resources>
<style name="myActionTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">YourColor</item>
</style>
</resources>
Abhi
  • 3,431
  • 1
  • 17
  • 35
  • When I delete this: "android:theme="@style/AppTheme.NoActionBar" in AndroidManifest.xml, I face the same problem whatever i do. I mean the main problem is this line. – Serdar Gun Aug 28 '17 at 13:54
  • Try chaging the application theme. If it still doesn't work, use a real device for testing. – Abhi Aug 28 '17 at 14:06