How can I make a menu option expand in android? - android

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;
}
}

Related

Back Button in the Navigation Bar not Working?

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);
}
}

How to get onPrepareOptionsMenu to "toggle" my preferences class

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.

option menu not clicked at first time in sherlockfragment

I have used option menu in my fragment.The problem is that when i go first time to fragment ,the option menu click event is not called.But when i go to another fragment.and again revist that fragment then the option menu click event is called...
following is the code
//Creating the option menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.newcarmenu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
//super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.menuNewCar:
_menuClickCallback.onMenuSelected();
break;
}
return super.onOptionsItemSelected(item);
}
please tell me why this happen?
Right now, you are returning super.onOptionsItemSelected(item) every time, so the selection is being passed on. You need to return true when your MenuItem has been selected. Try this instead:
switch(item.getItemId())
{
case R.id.menuNewCar:
_menuClickCallback.onMenuSelected();
return true;
default:
return super.onOptionsItemSelected(item);
}

Action Bar's onClick listener for the Home button

How can I implement a custom onClickListener for the Home button of the Action Bar?
I already did a getSupportActionBar().setDisplayHomeAsUpEnabled(true); and now I want to redirect the user to a certain activity in case the Home button is clicked.
I tried with:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Intent i = new Intent();
i.setClass(BestemmingActivity.this, StartActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
return true;
}
});
default:
return super.onOptionsItemSelected(item);
}
}
but it never enters in the onMenuItemClick.
Basically, it's done just like in this link but still it doesn't enter in the listener.
if anyone else need the solution
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed(); return true;
}
return super.onOptionsItemSelected(item);
}
I use the actionBarSherlock,
after we set supportActionBar.setHomeButtonEnabled(true);
we can override the onMenuItemSelected method:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
toggle();
// Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
break;
}
return true;
}
if we use the system given action bar following code works fine
getActionBar().setHomeButtonEnabled(true);
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
//do your action here.
break;
}
return true;
}
Fixed: no need to use a setOnMenuItemClickListener.
Just pressing the button, it creates and launches the activity through the intent.
Thanks a lot everybody for your help!
answers in half part of what is happening. if onOptionsItemSelected not control homeAsUp button when parent activity sets in manifest.xml system goes to parent activity. use like this in activity tag:
<activity ... >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.activities.MainActivity" />
</activity>
You need to explicitly enable the home action if running on ICS. From the docs:
Note: If you're using the icon to navigate to the home activity, beware that
beginning with Android 4.0 (API level 14), you must
explicitly enable the icon as an action item by calling
setHomeButtonEnabled(true) (in previous versions, the icon was enabled
as an action item by default).
Best way to customize Action bar onClickListener is onSupportNavigateUp()
This code will be helpful link for helping code
you should to delete your the Override onOptionsItemSelected and replate your onCreateOptionsMenu with this code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_action_bar_finish_order_stop, menu);
menu.getItem(0).setOnMenuItemClickListener(new FinishOrderStopListener(this, getApplication(), selectedChild));
return true;
}

Android access menu from onOptionsItemSelected

I have the following in my activity (sorry new to Java/Android):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.selectItem:
// menu.add(...) --> how to get the menu instance?
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I am wondering, how can I access a menu object in onOptionsItemSelected? For example, how would I go about adding a new view to the options menu based on the selection of an existing menu item? Is the answer related to "onPrepareOptionsMenu"?
you should use SubMenu for such things ... remeber that you cant add submenu to another submenu ... so only Menu->Submenu is possible you can't do stuff like this Menu->Submenu->Submenu (while Submenu is Dialog with choices)

Categories

Resources