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.
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 tried my level of best but I couldnt find it.I am working in navigation drawer activty where I can see a overflow Icon in top right when I click it, a Settings button like thing pops out when I click it(Settings) nothing happens
I dont no how to assing an XML to this so that when it is clicked a new activty should open
I know to create an xml and also to assign a onClickListner to the button but i am unable to proceed further since I dont no where to call the setting activty when that button(Settings button in overflow Icon) is pressed
Try this -
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
Intent intent = new Intent(this, YourSettingfActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
menu_main.xml -
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
</menu>
You can use find these below methods in activity and there you can inflate the menu and do want you want
Menu xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
<item android:id="#+id/action_search"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
<item android:id="#+id/action_logout"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
</menu>
and you have to write the code like below in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_search:
Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
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'm using this code to display a menu, but it isn't doing anything when I press the menu button.
This is in a view flipper, I don't know if that has anything to do with the problem.
I used this before and with no problems at all:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.new_game:
return true;
case R.id.help:
finish();
return true;
}
return false;
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:title="new_game" />
<item android:id="#+id/help"
android:title="clear" />
</menu>
You are not calling the superclass methods.
Dont you have to have some icon attached to it?
android:icon="#drawable/ic_menu_add"
for example