Question

ok...i have tried all the stackoverflow solutions. None of them seems to work.

Manifest

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.firstapp.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.firstapp.MainActivity"
            android:theme="@style/Theme.AppCompat.Light" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.firstapp.MainActivity" />
        </activity>
        <activity
            android:name="com.example.firstapp.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Inside /res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/title_search"
        yourapp:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_share"
        android:icon="@drawable/ic_action_share"
        android:title="@string/title_share"
        yourapp:showAsAction="ifRoom"
        />
    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_settings"
        android:title="@string/title_settings"
        yourapp:showAsAction="ifRoom"/>
</menu>

Inside main activity

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

Here is the screen shot.

Vertical Screen

enter image description here

Horizontal Screen

enter image description here

*

According to me the last icon in ActionBar should automatically go in overflow as stated in the google document. However, its not happening. Whats wrong here ?

*

Was it helpful?

Solution

Add this code in onCreate method of those activities in which you want to show icon-

try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if(menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
    } catch (Exception ex) {
            // Ignore
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top