1

I am using MainActivity to extend Activity class. Project is using minimum API as 11. When I am inflating Menu Items, it always gets displayed in overflow.

public class MainActivity extends Activity {
....
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the Menu Items
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return true;
}

menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
 <menu 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"
 tools:context=".MainActivity">
 <item android:id="@+id/attach"
    android:title="Attachment"
    android:orderInCategory="1"
    app:showAsAction="always"/>

</menu>

Styles.xml

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

MLavoie
  • 9,671
  • 41
  • 36
  • 56

3 Answers3

2

Your MainActivity extends Activity not AppcompatActivity and you are using android: Theme.Holo.Light native theme so, can u just replace

app:showAsAction="always" with android:showAsAction="always"

and try, like

<?xml version="1.0" encoding="utf-8"?>
 <menu 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"
 tools:context=".MainActivity">
 <item android:id="@+id/attach"
    android:title="Attachment"
    android:orderInCategory="1"
    android:showAsAction="always"/>

</menu>
Raghavendra
  • 2,305
  • 25
  • 30
0

Set it as showAsAction="always"

0

Try this code,

to show in overflow :

yourapp:showAsAction="always"

to show underflow

yourapp:showAsAction="never"

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto">
    <!-- Search, should appear as action button -->
    <item
        android:id="@+id/attach"
        android:icon="@drawable/ic_action_hardware_keyboard_arrow_left"
        android:title="Attachment"
        android:orderInCategory="1"
        yourapp:showAsAction="ifRoom" />

</menu>

check this menu resource doc

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • try my updated answer and tell me if you have any issues@Biswajit ghosh – Amit Vaghela Sep 06 '17 at 08:09
  • HI Amit thanks for your answer . But It will work only if your project has a dependency on AppCompat. Since i deleted Appcompat dependency so i had to use android:showAsAction instead of a custom namespace like yourapp: . – Biswajit ghosh Sep 07 '17 at 18:20