I have made a completely new project. I have added items to the menu
layout file. Those items do not show up on the action bar's right side. I remember that an icon with three dots shows up which opens up the menu.
Here is my Activity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
actionBar.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And here is my main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_option1"/>
<item
android:id="@+id/action_settings34"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_option2"/>
<item
android:id="@+id/action_settings3"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_option3"/>
</menu>
I think you want to show the overflow menu icon on the top right always and want your menu to open from there. Try something like following to force it there:
Inside your
onCreate()
do:It will force the App to show the overflow menu. The menu button will still work, but it will open the menu in the top right corner. Taken from this answer at How to force use of overflow menu on devices with menu button . This might be a kind of 'hack' but it works.
Edit:
Based on useful comments from @gunar, I request not to use this method. Following are the reasons:
The private field
sHasPermanentMenuKey
can be refactored in a future releases. Your app will stop working then.Also If you force moving action items to action bar because you're used to them, you'll end up confusing users that are used to how apps run on their device. Those with hardware
menu
key will be used to have the options showed from menu hardware key. If you intentionally show them the three dots of overflow menu, it might confuse them.So let all the apps be uniform in that sense. Those devices with no hardware key for
menu
items will show those automatically.OR you could create a structure like:
Where the icon_menu you can create a new icon set with the three points icon clipart.
Add
in the onCreate method.
You need to call this function in onCreate function of the activity.
After searching and reading a lot of answer, I found that there is another little thing that you should pay attention to.
At least it was my problem and I didn't find an answer of any above. On your
.XML
file. Yourandroid:showAsAction=
shouldn't be formandroid
package, it should be fromapp
package like in the example below:Don't forget to import the
app
package.