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.
Related
I am adding icons to ActionBar, but instead of getting them on ActionBar, I get them in the menu option..
My Java Code-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.report) {
Toast.makeText(getApplicationContext(),"Report",Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
My dashboard.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/share"
android:orderInCategory="100"
android:title="Share on Whatsapp"
app:showAsAction="never"/>
<item
android:id="#+id/medication"
android:title="My Medications"
android:icon="#drawable/medication"
app:showAsAction="always"/>
<item
android:id="#+id/coc"
android:title="Add Circle of care"
android:icon="#drawable/add_icon"
app:showAsAction="always"/>
<item
android:id="#+id/report"
android:title="Report"
android:icon="#drawable/report"
app:showAsAction="always"/>
</menu>
Please tell what am I missing..
I have appcompat as library.
Thank you
Extended ActionBar or AppCompactActivity
First menu item has app:showAsAction="never", so others menu items will always be shown only in overflow 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.
I would like to create a menu to appear on the screen when the Menu button is pressed.I've implemented it and it appears in the bottom of the page but not the one i would like... Hereunder my code and my goal picture to achieve
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) {
switch (item.getItemId()) {
case R.id.collapseall:
// Single menu item is selected do something
collapseAll();
return true;
case R.id.expandall:
expandAll();
return true;
}
}
My desired Menu:
My current Menu:
Edit:
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/expandall"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Expand All"/>
<item
android:id="#+id/collapseall"
android:orderInCategory="100"
android:showAsAction="withText"
android:title="Collapse All"/>
<item
android:id="#+id/myprofile"
android:orderInCategory="100"
android:showAsAction="withText"
android:title="My Profile"/>
<item
android:id="#+id/signout"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Sign Out"/>
</menu>
I can't get the Action Bar to display my action items. They all show up in the overflow menu. I have pasted all the relevant code below. Can anyone see my problem?
From Activity:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.viewer_menu, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_download:
return true;
case R.id.menu_star:
return true;
case R.id.menu_report:
return true;
case android.R.id.home:
// app icon in action bar clicked; go home
finish();
return true;
}
return false;
}
From Manifest:
<activity android:name=".CustomActivity"
android:label="">
From values-v11 folder (themes.xml)
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo">
</style>
from menu folder (viewer_menu.xml)
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_download"
android:title="Download" showAsAction="withText"
android:orderInCategory="2"/>
<item android:id="#+id/menu_star"
android:icon="#android:drawable/ic_menu_upload"
android:title="Star"
showAsAction="always"
android:orderInCategory="1"/>
<item android:id="#+id/menu_report"
android:title="Report Problem" showAsAction="always"
android:orderInCategory="0"/>
</menu>
It is android:showAsAction, not just showAsAction.
If you are using support package (android.support.v7.app.ActionBarActivity), you have to use something 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_download"
android:title="Download"
app:showAsAction="withText"
android:orderInCategory="2"/>
<item android:id="#+id/menu_star"
android:icon="#android:drawable/ic_menu_upload"
android:title="Star"
app:showAsAction="always"
android:orderInCategory="1"/>
<item android:id="#+id/menu_report"
android:title="Report Problem"
app:showAsAction="always"
android:orderInCategory="0"/>
</menu>
what version is the android emulator you are running?
Also, have you tried the http://actionbarsherlock.com/ version yet?
I feel this may be a stupid question but I have no idea what to do.
I have a menu which works correctly. One of the items in the menu: "Search" brings up a submenu with different items like "Restaurant", "Cafe", etc.
Below is the XML code for creating the menu and sub menu:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_entry"
android:title="#string/new_entry"
android:icon="#drawable/add_new">
</item>
<item android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/search">
<menu>
<item android:id="#+id/five"
android:title="#string/five">
</item>
<item android:id="#+id/ten"
android:title="#string/ten">
</item>
<item android:id="#+id/fifteen"
android:title="#string/fifteen">
</item>
<item android:id="#+id/restaurant"
android:title="#string/restaurant">
</item>
<item android:id="#+id/cafe"
android:title="#string/cafe">
</item>
<item android:id="#+id/sandwich"
android:title="#string/sandwich">
</item>
</menu>
</item>
<item android:id="#+id/info"
android:title="#string/info_short"
android:icon="#drawable/info">
</item>
Then in my Activity Class I have:
// Initialise Menu.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_services, menu);
return true;
}
and:
// Create cases for each menu selection.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_entry:
createFood();
return true;
case R.id.search:
return true;
case R.id.cafe:
Log.d(TAG, "In Cafe SubMenu");
Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT);
return true;
case R.id.info:
info();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Each case in "onOptionsItemSelected" does what it is supposed to do, except "case R.id.cafe:" which is the submenu. Here it should post to the LogCat and a Toast for testing but does neither.
What am I missing?
Thanks
I have tried your code and it is working perfectly. Only the Toast is not displaying when I select 'cafe'. That is because, you did not call show() in your code. Your code is
Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT);
to show the toast on run time it should call as follows
Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT).show();
:)
I think you should remove following lines:
case R.id.search:
return true;