Asha Soman
Asha Soman

Reputation: 1856

Action bar does not show

I tried to implement an action bar in my application.

menu.xml

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

    <item
        android:id="@+id/itemAdd"
        android:showAsAction="ifRoom|withText"
        android:title="ADD">
    </item>
    <item
        android:id="@+id/itemRefresh"
        android:showAsAction="ifRoom|withText"
        android:title="REFRESH">
    </item>
    <item
        android:id="@+id/itemHelp"
        android:title="HELP">
    </item>

</menu>

And created menu

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

enter image description here

But it does not show the action bar even if minSdkVersion is 11. What is the reason?

Upvotes: 34

Views: 72361

Answers (8)

Qu&#253; Hữu
Qu&#253; Hữu

Reputation: 1

You can try change the theme at res>values>themes.xml

change the parent to Theme.AppCompat.Light.DarkActionBar

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.Android_Test_2_1" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your dark theme here. -->
        <!-- <item name="colorPrimary">@color/my_dark_primary</item> -->
    </style>
</resources>

It works for me

Upvotes: 0

Jeffry LaySanto
Jeffry LaySanto

Reputation: 1

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".MyActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <!-- Put the RTA label here ... -->
            <meta-data android:name="RTA" android:value="RTA-5042-1996-1400-1577-RTA" />

        </activity>
    </application>
</manifest>

Upvotes: 0

sivi
sivi

Reputation: 11114

change Activity to AppCompatActivity in your class. That should be the easiest if you want to add it fast.. I'll add the code for someone who is new to Android OS:

public class YourActivity extends Activity 

into

public class YourActivity extends AppCompatActivity

Upvotes: 7

Abed Putra
Abed Putra

Reputation: 1215

I import import android.support.v7.app.AppCompatActivity;

then edit to public class MainActivity extends AppCompatActivity

Add to dependencies

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.0.0'
}

Upvotes: 2

Satheesh
Satheesh

Reputation: 1730

Remove your theme for your actionbar activity in androidManifest file. Now it will work...

<application
    android:allowBackup="true"
    android:icon="@drawable/tasktodo"
    android:label="@string/app_name"
    >

Don't add any theme in your application manifest file. If you added one, please remove and try running it...

Upvotes: 85

Glenn Bech
Glenn Bech

Reputation: 6182

An application with a Manifest like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.Actionbartest"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11" />
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity"
                  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>
</manifest>

Menu.xml like this

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

    <item
            android:id="@+id/itemAdd"
            android:showAsAction="ifRoom|withText"
            android:title="ADD">
    </item>
    <item
            android:id="@+id/itemRefresh"
            android:showAsAction="ifRoom|withText"
            android:title="REFRESH">
    </item>
    <item
            android:id="@+id/itemHelp"
            android:title="HELP">
    </item>
</menu>

And Activity like this

package com.example.Actionbartest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
}

Looks like this.

enter image description here

Are you sure your phone or emulator is running Android 3.0 or above? If not, you will end up with your screenshot.

To enable The Actionbar on older devices, you should use the AppCompat/support library (https://developer.android.com/tools/support-library/features.html)

Upvotes: 5

    android:allowBackup="true"
    android:icon="@drawable/ic2"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar" 

this works. Put it in your

Upvotes: 2

FD_
FD_

Reputation: 12919

You have to set the style of your Activity to Theme.Holo or one of its variants for the ActionBar to show. If you want to keep backwards-compatibility, call setTheme in onCreate of your Activity:

setTheme(android.R.style.Theme_Holo);

Upvotes: 5

Related Questions