I've got settings button at the right of my actionbar.
main.xml from rsc/menu:
<item
android:id="#+id/action_settings"
android:icon="#drawable/settings"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="Settings" />
<item
android:id="#+id/action_help"
android:orderInCategory="2"
android:showAsAction="withText"
android:title="Help" />
I want menu with other items opens after pressing this button in actionbar.
So I've added getActivity().openOptionsMenu() to my fragment in onOptionsItemSelected() function:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
public boolean onOptionsItemSelected( MenuItem item) {
switch (item.getItemId()) {
case R.id.action_help:{
//make help activity
}
return true;
case R.id.action_settings:{
getActivity().openOptionsMenu();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
As I understood we can't use openOptionsMenu() in onCreateOptionsMenu()
But how to solve this issue by another way?
Related
I created a first phase in sorting and try to take at it little by little but I failed at very start when Toast is not appearing
this is my code
`#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
inflater.inflate(R.menu.sort,menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.sort_date:
Toast.makeText(getContext(),"Sort by Date",Toast.LENGTH_SHORT).show();
return true;
case R.id.sort_category:
Toast.makeText(getContext(),"Sort by Category",Toast.LENGTH_SHORT).show();
return true;
case R.id.sort_highINC:
Toast.makeText(getContext(),"Sort by Highest Income",Toast.LENGTH_SHORT).show();
return true;
case R.id.sort_lowINC:
Toast.makeText(getContext(),"Sort by Lowest Income",Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}`
and this one is from the Menu.xml
`
<item android:id="#+id/sort_date"
android:title="Date"
app:showAsAction="never"/>
<item android:id="#+id/sort_category"
android:title="Category"
app:showAsAction="never"/>
<item android:id="#+id/sort_highINC"
android:title="Highest Income"
app:showAsAction="never"/>
<item android:id="#+id/sort_lowINC"
android:title="Lowest Income"
app:showAsAction="never"/>
`
I was expecting a toast message but it wont appear here
enter image description here
I am running this code on my android phone and i am unable to see menu items on my android phone but when i run this on emulator, i can see menu items.
Please help . Here is the screenShot of.
<item android:title="About Us"
android:id="#+id/aboutUs_ID"/>
<item android:title="Preferences"
android:id="#+id/preferences_ID"/>
java code in activity
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater myMenu = getMenuInflater();
myMenu.inflate(R.menu.menu_file, menu);
return true;
}
Try to use below solution;
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_settings"
android:orderInCategory="100"
android:title="Color Theam"
app:showAsAction="never" />
<item
android:id="#+id/action_appMahiti"
android:orderInCategory="100"
android:title="#string/tab_refrence"
app:showAsAction="never" />
</menu>
Here is the code snippiest you can add in your activity class
Activity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
// perform operation or display message
Toast.makeText(this, "CLick me", Toast.LENGTH_SHORT).show();
break;
case R.id.action_appMahiti:
// perform operation
startActivity(new Intent(this, XYZ.class));
break;
}
return super.onOptionsItemSelected(item);
}
I will suggest to try to use above solution.
![Layout Look like this][1]
[1]strong text: http://i.stack.imgur.com/CIsz8.png
I am trying to create a menu, but I am unable to see the menu.i am able to see menu in fragments properly. but cannot see in the sherlockfragmentactivity that is my main frame for fragment.
when i click on menu they doesn't take any action.
menu code
<group android:id="#+id/dashbardmenu">
<item
android:id="#+id/abc"
android:title="ABC">
</item>
<item
android:id="#+id/setting"
android:title="Setting">
</item>
</group>
<group android:id="#+id/fragmentmenu" >
<item
android:id="#+id/form"
android:icon="#drawable/ic_send"
android:showAsAction="ifRoom"
android:title="Form">
</item>
<item
android:id="#+id/resetform"
android:title="Reset Form">
</item>
</group>
im using visibility & invisibility for particular file.
java code
1st way
public boolean onCreateOptionaMenu(Menu menu ){
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.menu.activity, menu);
menu.setGroupVisible(R.id.fragmentmenu, false);
return super.onCreateOptionsMenu(menu);
}
2nd way
public boolean onCreateOptionaMenu(Menu menu,MenuInflater inflater ){
inflater.inflate(R.menu.activity, menu);
menu.setGroupVisible(R.id.fragmentmenu, false);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected((MenuItem)item);
switch (item.getItemId()){
case R.id.abc:
//Some action
case R.id.setting:
//Some action
default:
return false;
}
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected((MenuItem)item);
switch (item.getItemId()){
case R.id.abc:
//Some action
case R.id.setting:
//Some action
default:
return true;
}
}
Where is my Mistake Anyone help me???
Did you call this?
setHasOptionsMenu(true);
Also you have a typo - method name is onCreateOptionsMenu but you have twice onCreateOptionaMenu
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 have defined a menu as laid out by http://developer.android.com/guide/topics/ui/menus.html
Here is my menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_home"
android:icon="#drawable/ic_menu_home"
android:title="Main Menu" />
<item android:id="#+id/menu_signout"
android:icon="#drawable/ic_menu_signout"
android:title="Sign Out" />
</menu>
and my code, placed in my activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_home:
startActivity(new Intent(ManageUsersActivity.this, MainMenuActivity.class));
finish();
return true;
case R.id.menu_signout:
//TODO: issue signout to clear the cookie
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Unfortunately, when I click the menu button, nothing happens.
Try chaining to the superclass in onCreateOptionsMenu(). Here is a sample project demonstrating the use of MenuInflater for options and context menus.