I want to add some buttons next to the system option menu , how can I do that?
try
action bar
Icon of Action bar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/save_button"
android:title="i"
android:showAsAction="always" />
</menu>
Related
I want to add an action to the action bar, but it appears in my action bar as a drop down list.
How can I add button to the action bar?
My code is:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="Add"
showAsAction="ifRoom"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
If you are using android.app.Activity simply change showAsAction="always" to "android:showAsAction="always".
If you are using android.support.v7.app.Activity change the code as follows:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="Add"
app:showAsAction="ifRoom"/>
</menu>
You just simply add the one more item into menu and inflate that menu into your activity ....
like below you will add your button and access that button into your activity...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/refresh"
android:icon="#android:drawable/stat_notify_sync"
showAsAction="ifRoom"/>
<item
android:id="#+id/action_bar_button_cancel"
android:focusableInTouchMode="true"
android:icon="#android:drawable/ic_menu_close_clear_cancel"
showAsAction="ifRoom"/>
</menu>
Inflating the menu like below.....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Accessing method in action bar item is....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
//logic
return true;
case R.id.action_bar_button_cancel:
//logic
return true;
default:
return super.onOptionsItemSelected(item);
}
}
that's if you have any doubt comment
I have this code to create the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tip_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MNU_PREV:
animateTextViewsPrev();
break;
case MNU_NEXT:
animateTextViewsNext();
break;
}
return true;
}
And the XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/prev_tip" android:title="#string/prevTip"></item>
<item android:id="#+id/next_tip" android:title="#string/nextTip"></item>
</menu>
In a smartphone with Android 2.1 the menu is visible but in other mobile whit 4.1.1 is invisible.
Somebody now how to solve it?
What is you target Android, good to know, in android 4.0 them has redesign the menu layout.
I think you is missing super.onCreateOptionsMenu(menu); in the call onCreateOptionsMenu
In my code I has,
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
I was dealing with the same problem.. read some queries and documentation.. Hope this might help you.
Here's my XML file for a menu..
<item
android:id="#+id/action_send_feedback"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_send_feedback"/>
<item
android:id="#+id/action_share_app"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="#string/action_share_app"
android:icon="#drawable/ic_action_share" />
<item
android:id="#+id/action_rate_app"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_rate_app"/>
JAVA Code goes here..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
For android phones which have option button (at the bottom of the phone) the menu item which are showAsAction="never" comes when the button is pressed.. or else they will be shown normally on the action bar options menu..
Ref: http://developer.android.com/guide/topics/ui/menus.html#options-menu
You can simply change the "targetSdkVersion" to 10 in manifest file
It needs the ID in the java! :)
I've got a SherlockListActivity, and am trying to have a button in the action bar. Here's my menu.xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=">http://schemas.android.com/apk/res/android">
<item
android:id="#+id/add"
android:title="Add"
android:showAsAction="ifRoom" />
</menu>
Here's my java code to show the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
return false;
}
But in my activity, the action bar shows, but with no button, and the menu button on my device does nothing. My device is running 2.3.3.
Thanks.
Try to return true from onCreateOptionsMenu().
I have been trying to add a button to the SherlockActionBar but I can't get it working.
This is the code that I have:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, (android.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
This is my menu.xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/save_button"
android:title="i"
android:showAsAction="always" />
</menu>
This doesn't work, as even if I press the menu button, nothing shows up.
Is there any other way? Am I making any mistake?
You are using Android's Menu and MenuInflater, but should be using the classes that come with ActionBarSherlock:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, (com.actionbarsherlock.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
It seems like you are intermingling the two right now. Make sure that you import only com.actionbarsherlock.view.Menu and com.actionbarsherlock.view.MenuInflater, and not its Android counterparts. I recommend you to do something like the following:
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
I think in the menu.xml. Your item does not declare android:showAsAction attribute completely.
You must declare it like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/save_button"
android:title="i"
android:showAsAction="always|withText" />
</menu>
Since you did not specify any icon for the item action bar cannot display any item. By default icon are display than text.
The text doesn't show up in my android menu, only the icon. What can I do to fix this?
Thanks
XML menu file
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/context_menu_favourites"
android:icon="#drawable/dash_fav_btn"
android:title="Favourites"
android:titleCondensed="Fav"
/>
</menu>
The inflater code in my activity
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
This sometimes happens when the icon is too big... as you just confirmed, that's the case. And no, if you want to use a bigger icon, you will have to implement a menu yourself.