My code:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(settingsActivity);
return true;
}
This works to open the settings menu (Preferences.class) but I would like it to close the menu if it's already open when this system button is pressed. What can I add or change to make that happen?
Here is how you should be implementing what you want:
First thing first, you want to supply a "Settings" item that can selected by the user to get to your Preferences class. You do that by adding an item to your menu/main.xml and inflating it in onCreateOptionsMenu().
menu/main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Settings"/>
...
</menu>
Inside your Main Activity, override this to inflate your main.xml file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
...
return super.onCreateOptionsMenu(menu);
}
And then override this to handle what happens when a Menu item is selected, in this case the Settings item:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_settings:
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(settingsActivity);
return true;
...
default:
return super.onOptionsItemSelected(item);
}
}
I was able to get the menu to close by adding this to my customized preferences menu class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
onBackPressed();
return false;
}
which calls this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
which takes the user back to the screen to play another round. I don't know if this is an acceptable solution for every case but it worked for me.
Related
I have created an android project, with a couple of activities.
I have also added the back button feature into the activities, however when I click it
nothing happens.
Have I done something wrong?
I've put this code into each of the activities, and none of them work
Any opinion/input is greatly appreciated.
Code for trying to go back from my Gallery activity to the main:
//GalleryActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
// return true;
MenuInflater mif = getMenuInflater();
mif.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
and.. in res/menu/
<item
android:id="#+id/back_icon"
android:icon="#drawable/ic_action_back"
android:title="#string/back_title"
android:showAsAction="always"
/>
I downloaded the proper android design icons, and have added them to the drawable folders too.
EDIT:
Main Activity
button4= (Button) findViewById(R.id.button4);//find the button
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(), GalleryActivity.class);
startActivity(i);
finish();//close main activity after start info activity
}
});// links to gallery page
you have to override onOptionsItemSelected and check for the id. For instance:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back_icon:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
You have to specify the action of the options, included the home/back action as follows:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.back_icon:
onBackPressed(); //Or whatever you want to do when back_icon is pressed!
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have the problem that my application doesn't show the options menu when I hit the menu button. Debugging shows that the onCreateOptionsMenu(Menu menu) method is not called after hitting the menu button. I have another application with the same code for the menu and there it works. So now my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.app_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
Intent intent = new Intent(this, OptionsActivity.class);
startActivityForResult(intent, 1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In res -> menu -> app_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/options" android:title="#string/options" />
</menu>
I have no idea why the onCreateOptionsMenu is not called after hitting the menu button. I hope you guys can help me.
Edit: I'm not using Fragments and the onCreateOptionsMenu is really never called. Not at the start of the app and not when i'm hitting the menu button on my device.
Not sure from your post that you're using Fragments. If so, you must set menu options on by
setHasOptionMenu(true);
call this method from Fragment's onCreate() and the options menu will then be shown.
Try to add these item attributes in your app_menu.xml file:
<item
android:id="#+id/options"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/options"/>
You must declare a string for options in Strings.xml
It will call when app starts for the first time not after menu item selected.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getItemId() == R.id.menuitem_id) {
}
return super.onMenuItemSelected(featureId, item);
}
this method will be called after selection
In Manifest file just set target versions as below:
android:minSdkVersion="8" android:targetSdkVersion="10"
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
I wrote this code in mainactivty.java
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("About");
return true;
}
It creates a menu, but I want it to open a new window when I click on the menu.
I'm not sure if this is what you want (cause it seems too obvious) but it sounds like you're asking for the following
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.someItem: //this is your menu item in the menu.xml
startActivity(new Intent(this, MyNewActivity.class)); //here you start your new activity
break;
}
}
Hi i'm trying to show the menu when the user presses the menu button. I'm using the code from the Documentation but the options menu won't show up. I guess i should have a listener for this menu button, but how?? This is my class so far:
public class AppMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.appmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.hello:
sayHello();
return true;
case R.id.bye:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/hello"
android:title="Hello"
android:icon="#drawable/icon"/>
<item android:id="#+id/bye"
android:title="Bye" />
</menu>
Thanks !
This answer is in response to the comment discussion on the question.
You can't have to menu appear outside of your Activity. That means you have to start your Activity and then from inside your Activity you'll be able to get the menu appear on a menu button press.