I'm using menu items like this.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_overflow"
android:enabled="true"
android:icon="#drawable/ic_menu_moreoverflow_mtrl_alpha"
android:title=""
app:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/action_city"
android:icon="#drawable/ic_change_city"
android:title="Change City"
app:showAsAction="never" />
<item
android:id="#+id/action_language"
android:icon="#drawable/ic_change_language"
android:title="Change Language"
app:showAsAction="never" />
<item
android:id="#+id/action_change_theme"
android:icon="#drawable/ic_change_theme"
android:title="Change Theme"
app:showAsAction="never" />
</menu>
</item>
</menu>
Now How can I expand Menu Item -> menu_overflow programmatically after clicking a button?
My code is:
activity.openOptionsMenu();
---------
toolbar.showOverflowMenu();
---------
menu_overflow.expandActionView();
---------
menu_overflow.getSubMenu().getItem().expandActionView();
But those above codes are not working.
Here is design how it looks like.
Create one xml file names as menu_main. 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="example.javatpoint.com.optionmenu.MainActivity">
<item android:id="#+id/item1"
android:title="Item 1"
android:icon="#drawable/ic_action_accept" />
<item android:id="#+id/item2"
android:title="Item 2"/>
<item android:id="#+id/item3"
android:title="Item 3"
app:showAsAction="withText"/>
</menu>
Then add these code in your Activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.item1:
Toast.makeText(getApplicationContext(),"Item
Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(),"Item 2
Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3
Selected",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try this
YourActivity.this.openOptionsMenu();
Use Activity.openOptionsMenu(), 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 automatically show sub menu. 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/
Related
how to go to main menu onclick on sub menu in android. i am trying this but unable to do that.
this is java
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
sub = menu.getItem(0).getSubMenu();
sub.setHeaderTitle("sub menu...");
sub.setHeaderIcon(R.drawable.ic_voice);
//Toast.makeText(this, "submenu label=", Toast.LENGTH_SHORT).show();
MenuItem item = menu.findItem(R.id.action_settings);
TextView iv= (TextView) item.getActionView().findViewById(R.id.action_settings);
sub.getItem(0).setActionView(iv);
Toast.makeText(this, "before onclick listener=", Toast.LENGTH_SHORT).show();
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "-submenu header title clicked", Toast.LENGTH_SHORT).show();
}
});
return super.onPrepareOptionsMenu(menu);
}
this is my 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">
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:title="Refresh" />
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_voice"
android:title="Settings"
app:actionViewClass="android.widget.TextView">
<menu>
<item
android:id="#+id/change_password"
android:title="change password" />
<item
android:id="#+id/user_details"
android:title="user details" />
</menu>
</item>
</menu>
code
showing is submenu when we click on submenu text it should go back to mainmenu
Android doesn't provide some mechanism to set onClickListener to SubMenu header, but you can use a hack.
First of all, get rid of SubMenu header:
SubMenu sub = menu.findItem(R.id.action_settings).getSubMenu();
sub.clearHeader();
It's not secure to use getItem(0), because the order of your items may change, so I used findItem(R.id.action_settings) instead.
Next, you should add one more item to your SubMenu:
<menu>
<item
android:id="#+id/sub_title"
android:title="sub menu..."/>
<item
android:id="#+id/change_password"
android:title="change password" />
<item
android:id="#+id/user_details"
android:title="user details" />
</menu>
But now all three items will looks the same, so you need to change color of first MenuItem
MenuItem headItem = sub.findItem(R.id.sub_title);
SpannableString s = new SpannableString(headItem.getTitle());
s.setSpan(new ForegroundColorSpan(Color.GRAY), 0, s.length(), 0);
headItem.setTitle(s);
Also, don't use onPrepareOptionsMenu() for that purposes, because it calls every time when you open menu, you can put all that code inside onCreateOptionsMenu():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SubMenu sub = menu.findItem(R.id.action_settings).getSubMenu();
sub.clearHeader();
MenuItem headItem = sub.findItem(R.id.sub_title);
SpannableString s = new SpannableString(headItem.getTitle());
s.setSpan(new ForegroundColorSpan(Color.GRAY), 0, s.length(), 0);
headItem.setTitle(s);
return true;
}
And the last step - listen to clicks on that MenuItem:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.sub_title:
Toast.makeText(this, "-submenu header title clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
I have a list of menu items in my actionbar. Each item click should trigger a different method. But onOptionsItemSelected is never called.
This is how the actionbar is defined in MainActivity:
public class MainActivity extends AppCompatActivity {
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings_1) {
//do something
return true;
} else if (id == R.id.action_settings_2) {
//do something
return true;
} else if (id == R.id.action_settings_1) {
//do something
return true;
}
return super.onOptionsItemSelected(item);
}
...
}
This is the actionbar menu layout menu_main:
<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_settings_1"
android:orderInCategory="1"
android:title="Item 1"
app:showAsAction="never" />
<item
android:id="#+id/action_settings_2"
android:orderInCategory="2"
android:title="Item 2"
app:showAsAction="never" />
<item
android:id="#+id/action_settings_3"
android:orderInCategory="3"
android:title="Item 3"
app:showAsAction="never" />
</menu>
How can I set up the actionbar so that onOptionsItemSelected is called when an actionbar item is clicked?
Inside your onCreateOptionsMenu, return true instead of calling super. That should do it
In the onCreate(), call setSupportActionbar(), like so
toolbar = (Toolbar)findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
Just do the change as below :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
This problem can potentially also happen if using the menu attribute in XML:
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:menu="#menu/your_menu"
app:navigationIcon="#drawable/..."
app:title="#string/..." />
So app:menu="#menu/your_menu" doesn't work with the overridden fragment method onOptionsItemSelected. The menu item click listener has to be set to the Toolbar
private fun setMenuClickListener(toolbar: MaterialToolbar) = with(toolbar) {
setOnMenuItemClickListener { menuItem ->
if (menuItem.itemId == R.id.yourId) {
//do something
return#setOnMenuItemClickListener true
}
//this is a lambda so it can be just false,
//added return to make it explicit
return#setOnMenuItemClickListener false
}
}
So what we are looking for is the setOnMenuItemClickListener from MaterialToolbar
Try this:
Instead of implement
#Override
public boolean onCreateOptionsMenu
...
In activity_main.xml add:
<androidx.appcompat.widget.Toolbar
android:id="#+id/myToolbar"
app:title="#string/myTitle"
app:menu="#menu/menu_main"
>
</androidx.appcompat.widget.Toolbar>
Next, in menu_main.xml add in each item:
android:onClick="onOptionsItemSelected"
Like this:
<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_settings_1"
android:orderInCategory="1"
android:title="Item 1"
app:showAsAction="never"
android:onClick="onOptionsItemSelected" />
<item
android:id="#+id/action_settings_2"
android:orderInCategory="2"
android:title="Item 2"
app:showAsAction="never"
android:onClick="onOptionsItemSelected" />
<item
android:id="#+id/action_settings_3"
android:orderInCategory="3"
android:title="Item 3"
app:showAsAction="never"
android:onClick="onOptionsItemSelected" />
</menu>
I tried
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.v(
this.getClass().getName() + "!!!",
new Exception().getStackTrace()[0].getMethodName()
);
MenuItem m_item = (MenuItem)menu.findItem(R.id.action_settings);
if(m_item != null)
m_item.setTitle("Back to test");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
but it always gets null.And also,onCreate seems to have it as null as well. Is there a function that i can modify the text on it during runtime??? And if so, is there an easy way to find it?
You should add items in the menu xml. You can find it on the folder res/menu. There you have all the menus available for inflation.
I suppose that you have only one. This is how it could look with added options:
<?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/searchMain"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|withText"
android:title="Search"/>
<item
android:id="#+id/searchBarcodeScan"
android:icon="#drawable/ic_launcher"
app:showAsAction="always|withText"
android:title="Scanner"/>
<item
android:id="#+id/seeList"
android:icon="#drawable/ic_launcher"
app:showAsAction="ifRoom|withText"
android:title="See list"/>
<item
android:id="#+id/settings"
android:icon="#drawable/ic_settings"
app:showAsAction="ifRoom|withText"
android:title="Settings"/>
</menu>
Then in your activity you can react to the option selected by overriding this method and doing things inside it:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchMain:
doSomething();
return true;
case R.id.searchBarcodeScan:
doSomething2();
return true;
case R.id.seeList:
doSomething3();
return true;
case R.id.settings:
doSomething4();
return true;
}
return super.onOptionsItemSelected(item);
}
EDIT
In order to change menus in runtime, you should call the method invalidateOptionsMenu(). This method will force the recreation of the menu and this time onPrepareOptionsMenu method will be called. You should override it in your Activity this way:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(someCondition){
getMenuInflater().inflate(R.menu.main_menu, menu);
}
else if(someOtherCondition){
getMenuInflater().inflate(R.menu.other_menu, menu);
}
return true;
}
Instead of
getMenuInflater().inflate(R.menu.settings, menu);
do something like:
getMenuInflater().inflate(R.menu.menu_custom, menu);
where res/menu/menu_custom.xml is something 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">
<item android:id="#+id/menu_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"/>
<item android:id="#+id/menu_settings"
android:icon="#drawable/ic_action_settings"
android:title="#string/settings" />
</menu>
This will give you two items in the dropdown. It looks like your res/menu/settings.xml only has one item in it.
For some reason, I cannot get my button to appear on the Action Bar. I have defined it in an XML file in /res/menu, along with inflating it and listening for an action. The icon is present in /res/drawable-hdpi. And nothing of interest shows in logcat. :(
XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/logout"
android:icon="#drawable/logout"
android:title="Logout"
android:orderInCategory="100"
android:showAsAction="always" />
</menu>
Code in main activity:
public class MainActivity extends ActionBarActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.logout:
//logout code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//rest of app
}
I followed this question for my initial problem, and it didn't help. How to add button in ActionBar(Android)?
Try with this change:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/logout"
android:icon="#drawable/logout"
android:title="Logout"
android:orderInCategory="100"
yourapp:showAsAction="always" />
</menu>
I am new to android and stuck at the point where i have to detect clicks on submenus that are defined in XML file
my XML file is :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ccard_menu1"
android:title="Select from Profile?"
></item>
<item android:id="#+id/ccard_menu2"
android:title="Add Field"
>
<menu >
<item android:id="#+id/submenu1"
android:title="Add Products"
></item>
<item android:id="#+id/submenu2"
android:title="Add Clients"
></item>
<item android:id="#+id/submenu3"
android:title="Add a Custom Field">s</item>
</menu>
</item>
</menu>
how do i detect clicks on "submenu 1,2,3" in onOptionsItemSelected method?
how do i have to structure the switch case?
I you are looking for something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.submenu1:
// do something
return true;
case R.id.submenu2:
//do something else
return true;
// etc..
default:
return super.onOptionsItemSelected(item);
}
}
Please correct me if I am mistaken.