I added an option menu in my app.
I want activity's whole background appear dark when menu key is touched so the user can see option menu well.(like when a dialog is displayed)
Should I use animation to do this or is there any other way?
As you're using OptionMenu, you could do:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.your_option_menu:
mRootLayout.setAlpha(0.5f);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
mRootLayout would be the root layout from the xml you set the view in your activity. Just use findViewById to set it.
After you select a final option in your menu, make sure to return the alpha property of the root layout to 1.
Related
Right now I have a couple of buttons that do different things when clicked, but now I want one of them to display a menu when clicked, but I am not sure how to do this.
My code is something like this for the main buttons:
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.button1:
//do stuff
break;
case R.id.button2:
//display menu
break;
}
If button2 is pressed, I want to display a list of options and see what menu item the user selects, but how can I do this?
Displaying icon in menu
XML
<item
android:id="#+id/item_1"
android:icon="#drawable/settings"
android:showAsAction="always"
android:title="Add item1" />
You could use a PopupMenu In your onOptionsItemSelected() which will then show a different menu when one of your menu buttons is clicked. Modify this piece of code according to your needs:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.button1:
// DO SOMETHING HERE
break;
case R.id.button2:
// THE R.id.button2 has to be the same as the item that will trigger the popup menu.
View v = findViewById(R.id.button2);
PopupMenu pm = new PopupMenu(LoginActivity.this, v);
pm.getMenuInflater().inflate(R.menu.pm_accounts_item, pm.getMenu());
pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(), String.valueOf(item.getTitle()), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.menuEdit:
break;
case R.id.menuDetails:
break;
case R.id.menuDelete:
break;
default:
break;
}
return true;
}
}); pm.show();
break;
default:
break;
}
return false;
}
You will notice that a new menu XML has been inflated at this line:
pm.getMenuInflater().inflate(R.menu.pm_accounts_item, pm.getMenu());
You will have to create a second menu XML with the list of options that you need to display when one of the buttons is clicked. This is similar to your current menu XML with the difference being, a different set of options.
IMPORTANT!
Do not forget to include this View v = findViewById(R.id.button2); before the PopupMenu pm..... The PopupMenu requires a View to anchor itself to. But the onOptionsItemSelected() method does not provide this at all. Hence the extra statement.
The above example illustrates the example in an Activity. To use this in a Fragment, change the View v = findViewById(R.id.button2); to View v = getActivity().findViewById(R.id.button2);
This is the final result:
If you are talking about Opening option menu, Then there is a method openOptionMenu() in Activity class.
case R.id.button2:
openOptionsMenu();
break;
If you are talking about opening contextMenu
You have to call openContextMenu() method, But don't forget to add registerForContextMenu(view) and other methods for taking action
Can i make my Action Bar app icon clickable without displaying the back icon?
This is my code, it works, I have only layout problem:
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is my activity layout, what I want is remove back icon, is it possible?
Try to use setHomeButtonEnabled(boolean enabled) instead of setDisplayHomeAsUpEnabled(boolean enabled). The latter do exactly the same thing as former which is enabling home button but additionally put up affordance sign which you want to get rid off.
What is the correct way to change background color of a MenuItem? I would like to change it to sinalize that MenuItem is active but i didn't find how to do it.
By now, i'm changing icon for another one with different color but i don't need it, i just need to put some color on background when it is clicked.
Here is what i'm doing to change icon:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mnNow:
item.setIcon(getResources().getDrawable(R.drawable.now_icon_active);
this.displayFragment(getNowFragment());
break;
default:
super.onOptionsItemSelected(item);
break;
}
return false;
}
How can i change background color when it's clicked and keep the color until the user choose a new option?
Thank in advance.
i want to toggle the map view on my android map app - I have been told to use this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapview :
EVENT HERE TO TOGGLE MAP VIEW TO AND FROM SATELLITE
default :
return super.onOptionsItemSelected(item);
}
}
However, I added this code to the end of my map page code -- but i don't know what to put in the middle to make this menu item actually toggle from the different maps available. Help???
If I get you right, you should do something like that:
case R.id.mapview :
myMapView.setSatellite(!myMapView.isSatellite());
break;
I'm trying to make an Options "Menu" Screen. When the menu button is pressed the menu comes up with a "button" that says Options when that is clicked, the function clicks a button called optionsButton. When that button is pressed I want one TableLayout to become Invisible and one to become visible.
Here is the code I have to hide the layouts.
public void optionButton(View view)
{
TableLayout mainTable = (TableLayout)findViewById(R.id.tableMain);
TableLayout optionTable = (TableLayout)findViewById(R.id.tableOptions);
mainTable.setVisibility(TableLayout.INVISIBLE);
optionTable.setVisibility(TableLayout.VISIBLE);
}
And here is the XML that handles my optionButton <Button android:onClick="optionButton" android:id="#+id/optionsButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:visibility="invisible"></Button>
And here is the code that handles my "Menu Button"
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.options:
Button optbtn = (Button)findViewById(R.id.optionsButton);
optbtn.performClick();
break;
default:
break;
}
return true;
}
The problem is that when the menu button is clicked, nothing happens. Any help on this issue would be greatly appreciated.
I fixed the problem by deleting the optionsTable and all of it's contents. Remaking it and putting it below the mainTable in the Graphical Editor of main.xml