Reputation: 1529
I've been trying to add a functionality to my android application such that when I click a button, menu listing should be visible:
Here is my code:
menu.xml:
<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="com.example.MainActivity" >
<item android:id="@+id/action_onthego_sentence"
android:title="settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
From the main activity, upon clicking a button, I do:
button.setOnClickListener( new View.OnClickListener()
{
@Override
public void onClick( View view )
{
runOnUiThread( new Runnable()
{
@Override
public void run()
{
openOptionsMenu();
}
} );
}
} );
What I need is:
As shown in the image, I'd like to see the menu is opened. Any suggestions please?
Upvotes: 3
Views: 9749
Reputation: 669
Use Activity.openOptionsMenu() to open menu programmatically, but you may need to post delay to call it. like below
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
openOptionsMenu();
}
}, 1000);
If you need to open sub menu automatically. you can call menu.performIdentifierAction after openOptionsMenu, like below
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
openOptionsMenu();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mainMenu.performIdentifierAction(R.id.submenu, 0);
}
}, 500);
}
}, 1000);
Android Dev efficiency tools: https://play.google.com/store/apps/details?id=cn.trinea.android.developertools
Android Open Source Projects: https://p.codekk.com/
Upvotes: 0
Reputation: 1325
Try the below solution It will be display click on the button menu listing should be visible:
res/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">
<item
android:id="@+id/action_changeLang"
android:orderInCategory="100"
android:title="Change Lang" />
<item
android:id="@+id/action_color"
android:orderInCategory="100"
android:title="Select color" />
<item
android:id="@+id/action_applist"
android:orderInCategory="100"
android:title="App List" />
</menu>
MainActivity.java
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_changeLang:
// Add your code
break;
case R.id.action_color:
// Add your code
break;
case R.id.action_applist:
// Add your code
break;
}
return super.onOptionsItemSelected(item);
}
Try to use above solution. It will work for me
Upvotes: 0
Reputation: 2699
If you are using customized toolbar in your app, then the following way will be useful,
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
toolbar.showOverflowMenu();
}
}, 500);
Upvotes: 4
Reputation: 5600
Use:
MainActivity.this.openOptionsMenu();
Then, check your toolbar if is this okay.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acercade_activity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// MenuInflater inflater = getMenuInflater();
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
//case R.id.action_settings:
// return true;
case R.id.perfil:
drawerLayout.openDrawer(Gravity.LEFT);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Upvotes: 1
Reputation: 4705
Hello Dear if you are using toolbar following code
toolbar.showOverflowMenu();
And other wise you can directly call
MainActivity.this.openOptionsMenu();
Upvotes: 2
Reputation: 284
Use this in your menu layout:
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="Setting"
app:showAsAction="ifRoom" />
Upvotes: 1