1

I'm using Android Studio, and I've tried to add custom image to the action bar background, now I've used some tutorial on youtube which says that I should add the following code to my styles.xml

 <style name="custom_actionbar" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/custom_style</item>
    </style>
    <style name="custom_style" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/bar</item>
    </style>

and manifest.xml

<activity
       android:name=".MainActivity"
       android:label="@string/app_name"
       android:screenOrientation="portrait"
       android:theme="@style/custom_actionbar">

Now, it should be working though it showing me this error:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

So I've looked it up on internet and I've found this, though after using the solutions to this, the application still crashing with the same error (I've used debugging to check the error few times after using the solutions).

How can I use Theme.AppCompact theme correctly so the application will work?

My entire manifest.xml

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Food"
            android:label="@string/title_activity_food"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Time"
            android:label="@string/title_activity_time"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Info"
            android:screenOrientation="portrait" />
        <activity
            android:name=".Training"
            android:label="@string/title_activity_training"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".firstEntry"></activity>
    </application>

Styles.xml

<resources>

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

    <style name="custom_actionbar" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/custom_style</item>
    </style>

    <style name="custom_style" parent="@style/Widget.AppCompat.Light.ActionBar">
        <item name="android:background">@drawable/bar</item>
    </style>

    <style name="LightDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:textColor">@android:color/primary_text_light</item>
        <item name="background">@drawable/bar</item>
    </style>

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

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

</resources>
Community
  • 1
  • 1
Yotam Dahan
  • 689
  • 1
  • 9
  • 33

1 Answers1

1

Try using the AppCompatTheme:

in res/values/styles.xml : replace your style with

 <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="custom_actionbar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/custom_style</item>
</style>

<style name="custom_style" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@drawable/bar</item>
</style>

Now in your manifest add the custom action bar theme in your activity using:

 <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/custom_actionbar">

EDIT

in your mainactivity modify:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    prefs = getSharedPreferences("com.none.myapplication", MODE_PRIVATE);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

Alternative solution

Add the image to your toolbar background:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@drawable/bar"  //your image
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.v7.widget.Toolbar>

And also modify the theme in manifest: to use the toolbar instead of ActionBar

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • `This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.` - the error I received – Yotam Dahan Apr 29 '17 at 13:41
  • I've added my styles.xml file also, please watch the edit. – Yotam Dahan Apr 29 '17 at 13:43
  • you need to use a no action bar theme as parent like: Theme.AppCompat.NoActionBar – rafsanahmad007 Apr 29 '17 at 13:59
  • Where? I've changed the parent in `name="AppTheme"` to what you've just said, and still same error. – Yotam Dahan Apr 29 '17 at 14:09
  • @YotamDahan i have edited the answer..u can.check it – rafsanahmad007 Apr 29 '17 at 15:29
  • Still the same error - java.lang.RuntimeException: Unable to start activity ComponentInfo{com.none.myapplication/com.none.myapplication.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. – Yotam Dahan Apr 29 '17 at 15:37
  • in your mainactivity remove the toolbar related code...u can not use action bar and toolbar in same activity – rafsanahmad007 Apr 29 '17 at 15:42
  • https://pastebin.com/U2R9XNsw - If i'm not wrong, the ToolBar code is still there – Yotam Dahan Apr 29 '17 at 15:54
  • I have edited your mainactivity code for actionbar...also provide an alternative way to do it with toolbar.. – rafsanahmad007 Apr 29 '17 at 16:05
  • I've tried both, the first solution is canceling my menu button and fails to apply background image. the second solution is binary xml file error (After trying to add the toolbar code in app_bar_main.xml file). – Yotam Dahan Apr 29 '17 at 16:40
  • can you specify the logcat error in second case?..you were already adding toolbar in your xml – rafsanahmad007 Apr 29 '17 at 16:52
  • android.view.InflateException: Binary XML file line #11: Binary XML file line #22: Error inflating class - you can see more here https://pastebin.com/CppjSNXV – Yotam Dahan Apr 29 '17 at 17:03
  • `android.view.InflateException: Binary XML file line #11: Binary XML file line #16: Error inflating class android.support.v7.widget.Toolbar` - after removing the other toolbar – Yotam Dahan Apr 29 '17 at 17:18
  • i have edited the toolbar xml code...make sure your `drawable/bar` is a valid image ..try with another image ...also if the error occurs ..and just use one toolbar – rafsanahmad007 Apr 29 '17 at 17:23
  • So I've no idea why, though after moving the image to mipmap instead of drawable the toolbar background is working fine. – Yotam Dahan Apr 29 '17 at 17:44