Multi Options Actionbar - android

I have an action bar in my application with one button that should be options button.
When you click it, it should open few options.
I made a String-Array in the Strings.xml file but I can't get it to work.
Any code samples?
I searched the internet but couldn't find anything.
Thanks!

Vogella provides good tutorials. Here is the ActionBar tutorial which shows how to do that: http://www.vogella.com/tutorials/AndroidActionBar/article.html
Make sure you have an xml document in your menu folder in resources called something like mainmenu.xml which should look like the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings">
</item>
</menu>
Then make sure you have the following functions in your Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_refresh:
Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
.show();
break;
// action with ID action_settings was selected
case R.id.action_settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}

Related

Menu items keeps on adding on every menu button press

I have added two menu item . Both of them works good but whenever i press menu button new menu items appear beside the old one . Like below u can see
below is my menu item xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Search"
android:icon="#drawable/search_white_24dp"
android:id="#+id/searchmenu"
app:showAsAction="always">
</item>
<item
android:icon="#drawable/settings_white_24dp"
android:title="Setting"
android:id="#+id/settingmenu"
app:showAsAction="always"
/>
</menu>
and here is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchmenu:
editText = DuwaManager.openSearchBox(this,
getSupportActionBar(), "DuwaListView");
break;
case R.id.settingmenu:
break;
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
}
please help where i am wrong ?
Sorry this will get to long but , I have second Question
2) My second question i have another menu. xml in which there is share icon in place of search icon , But when i am trying to show on action bar with same code menu item does not show on action bar . below is my another menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:title="Share"
android:icon="#drawable/share_white"
android:id="#+id/share_tarika"
app:showAsAction="always"
/>
<item android:title="Setting"
android:icon="#drawable/settings_white_24dp"
android:id="#id/settingmenu"
app:showAsAction="always"/>
</menu>
and its code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tarika_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share_tarika:
break;
case R.id.settingmenu:
break;
}
return super.onOptionsItemSelected(item);
}
Try this
#Override
public void onCreateOptionsMenu(Menu menu ) {
menu.clear();// use menu.clear
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu."your current activity name ", menu);
return true;
}
use menu.clear()
The best to do is as this :
#Override
public void onCreateOptionsMenu(Menu menu ) {
getMenuInflater().inflate(R.menu."your current activity name ", menu);
return true;
}
if you have just one menu. Then set menu from Activity page and return true then no need to clear menu. and no duplicate menu will appear.
But if you use two fragment and have to change activity menu as per fragment then only need to clear menu and reset it with new one.
And if you are using onCreateOptionsMenu(Menu menu) in fragment then remove them.

How to create an Overflow menu and combine it with an option menu?

I am new in Android development. I want to ask how can I create an overflow menu with two items. Also, how can I combine it with an option menu I already did, which looks like this:
public class MainActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater blabla = getMenuInflater();
blabla.inflate(R.menu.options_menu ,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.OurSponsors:
Intent i = new Intent("com.--.------.------");
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
return false;
}
xml
<item
android:title="Our Sponsors"
android:id="#+id/OurSponsors"
android:icon="#drawable/image"
/>
<item
android:title="Exit"
android:id="#+id/exit"
android:icon="#drawable/exit"
/>
I am a beginner.
Here is the developer page related to that http://developer.android.com/guide/topics/resources/menu-resource.html.
You will want to add android:showAsAction="collapseActionView" (or android:showAsAction="never" depending on your API level) to each menu item in the XML layout

Android - Menu Item cannot be resolved to a type in onOptionsItemSelected()

I created a simple activity (with a menu) and tried adding menu items but they don't show up when trying to actually give them some function in the onOptionsItemSelected() method. I'm not sure why it's not working, as I did the exact same thing in the menu for the main activity and it worked just fine. When typing in android.R.id.add_screen_submit_button for example, it is not recognized as existing. And if I forcefully just type it in and leave it the message "add_screen_submit_button cannot be resolved or is not a field" comes up. The menu is also in the correct folder (I actually just left it as is when creating the Activity). Thanks in advance.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/add_screen_submit_button"
android:orderInCategory="4"
android:showAsAction="always"
android:title="#string/add_screen_ok"
android:icon="#drawable/accept_icon" />
<item
android:id="#+id/add_screen_cancel_button"
android:orderInCategory="5"
android:showAsAction="always"
android:title="#string/add_screen_cancel"
android:icon="#drawable/cancel_icon" />
</menu>
Here's the code
public class AddActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// Show the Up button in the action bar.
//setupActionBar();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case android.R.id.add_screen_submit_button:
Toast.makeText(this, "Map Selected", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
Change
android.R.id.home:
to
R.id.home:
and the same with the other one.
android.R is for sdk resources and R.id.some_id is for ids you create

android - submenu click isn't selecting the right item

I have a menu with a submenu. The parent item is "Options" and its children are "Call", "Navigate", and "Edit". Here is my activity code that handles the menu click. The problem is that the first submenu always gets selected by the event. With this code if i click on "Call"(the first child item) the onOptionsItemSelected() method registers the R.id.record_edit item being selected. I don't know why this is happening since the "Call" menu item has the id of "record_call". I've tried switching the submenu items around in the xml but the first submenu item is the only one that registers and it always registers as R.id.record_edit. Not sure what's wrong with my code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.record_edit:
Intent intent = new Intent(this, EditRecordActivity.class);
myApp.setRecord(record);
Bundle b = new Bundle();
b.putBoolean("new", false);
b.putString("pageTitle", pageTitle);
b.putString("meta_universalid", meta_universalid);
intent.putExtras(b);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
xml menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/record_options"
android:showAsAction="ifRoom|withText"
android:title="..."
android:titleCondensed="Options"
android:enabled="true">
<menu>
<item
android:id="#+id/record_call"
android:showAsAction="ifRoom|withText"
android:title="#string/record_call"/>
<item
android:id="#+id/record_navigate"
android:showAsAction="ifRoom|withText"
android:title="#string/record_navigate"/>
<item
android:id="#+id/record_edit"
android:showAsAction="ifRoom|withText"
android:title="#string/record_edit"/>
</menu>
</item>
</menu>
just checked with your menu to be sure. It is working fine with me. Check whether your overall format is like the following code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.record_call:
Toast.makeText(this, "call", 1000).show();
return true;
case R.id.record_edit:
Toast.makeText(this, "edit", 1000).show();
return true;
case R.id.record_navigate:
Toast.makeText(this, "navigate", 1000).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Is using menuItem.getItemId() valid in finding which MenuItem is selected by user?

I have menu in my App. I need to check for the which menu item been selected by the user and take appropriate action. I did this as,
#Override
public boolean onOptionsItemSelected(MenuItem menuitem){
String title = menuitem.getTitle().toString();
int itemId = menuitem.getItemId();
switch(itemId){
case 2131296257:
-----------------;
break;
case 2131296258:
------------------;
break;
}
But these MenuItem Id's are getting changed each time I run my App. Now I thought to compare the menuTitle with hard coded string values like ,
String title = menuitem.getTitle().toString();
if(title.equals("Settings..")){
-------------
-------------
-------------
}
But I don't think it's a good practice. But I guess I can do the same using the menu titles defined in strings.xml. But I am not sure how to use this.... (like R.string.....).
Can someone suggest on this please.
Thanks
Here's an example menu named game_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Inflating a Menu Resource
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Responding to user action
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
for more information click here

Categories

Resources