how to keep check in a menu in android studio - android

I would like to be able to keep the check in a menu when selecting an item, but when clicking it and then reopening the menu, the check disappeared.
image of the menu where I select an item
image where I reopen the menu but it is visually as if I had not clicked
here is the menu in java code
public void buttoninit(){
button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Para agregar un Popup Menu a un Button
PopupMenu popupMenu = new PopupMenu(mostrarActivity.this, button1);
popupMenu.getMenuInflater().inflate(R.menu.menu_filter_pa, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_pa_rut:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
a = 1;
break;
case R.id.menu_pa_nombre:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
a = 2;
break;
case R.id.menu_pa_apellido:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
a = 3;
break;
default:
return false;
}
return true;
}
});
popupMenu.show();
}
});
}
here is the Menu in XML
<group android:checkableBehavior="single">
<item
android:id="#+id/menu_pa_rut"
android:icon="#drawable/ic_filter_list"
android:title="Rut"
app:showAsAction="ifRoom">
</item>
<item
android:id="#+id/menu_pa_nombre"
android:icon="#drawable/ic_filter_list"
android:title="Nombre"
app:showAsAction="ifRoom">
</item>
<item
android:id="#+id/menu_pa_apellido"
android:icon="#drawable/ic_filter_list"
android:title="Apellido"
app:showAsAction="ifRoom">
</item>
</group>

Whenever you will click on button to populate option menu it will reinitialize every time and default value and checks will be applied.
You will have to store click position for the clicked radio button of menu and set checked accordingly.
Or initialize your popup option menu only once by declaring it globally.

you have to move your code out of setOnClickListener just keep popupMenu.show()
and if you want to save your ui state or menu, the better way is to use a live data and viewModel. the live data keeps value even if configuration changes.

Related

open popup on long click of action bar

I want to show the popup menu on longer click of action bar but exactly below where I click.
toolbar.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
PopupMenu popupMenu = new PopupMenu(HomeActivity.this,toolbar);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.device_setting:
Toast.makeText(HomeActivity.this, "dev settings", Toast.LENGTH_SHORT).show();
break;
case R.id.app_update:
break;
case R.id.wireless_setting:
break;
case R.id.hide_footer:
break;
}
return true;
}
});
popupMenu.show();
return true;
}
});
and popup_menu.xml is below
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/device_setting"
android:title="Device Setting"></item>
<item android:id="#+id/app_update"
android:title="App Update"></item>
<item android:id="#+id/wireless_setting"
android:title="Wireless Setting"></item>
<item android:id="#+id/hide_footer"
android:title="Full Screen"></item>
</menu>
screen shot is given below.
I want to open popup exactly below where I click (longer click)on ActionBar. Please help
Image Screenshot
According to your requirement, you just need to use the popup window because is very flexible, I prefer this GitHub library
https://github.com/kakajika/RelativePopupWindow
Here, just need to change the position of the window for example
popup.showOnAnchor(toolbar, VerticalPosition.BELOW, HorizontalPosition.LEFT);

How to match popup Menu on button click to that of createOptionsMenu (or remove shadow)

I have generated a popUp Menu on button click (as the layout is custom layout so couldn't use onCreateOptionsMenu). Everything is working fine except that the menu looks odd. It has got a shadow behind it which doesn't seem to go well with the app. Is there any way in which I can remove the shadow or make the popup menu look like the menu generated using onCreateOptionsMenu.
Below is the image of my popup Menu
Code: (options_menu.xml)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/option1" android:title="#string/option1"
android:orderInCategory="101" android:showAsAction="always|withText" />
<item android:id="#+id/option2" android:title="#string/option2"
android:orderInCategory="102" android:showAsAction="always|withText" />
</menu>
Activity:
popMenuBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context wrapper = new ContextThemeWrapper(MenuActivity.this, myPopupMenuTextAppearance);
mPopupMenu = new PopupMenu(wrapper, v);
MenuInflater menuInflater = mPopupMenu.getMenuInflater();
menuInflater.inflate(R.menu.options_menu, mPopupMenu.getMenu());
mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.option1:
startActivity(new Intent(this, XYZ.class));
return true;
case R.id.option2:
someFunction();
return true;
default:
Toast.makeText(this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
}
});
mPopupMenu.show();
}
});

How to add a menu to a button in Android

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

onCreateOptionsMenu for imageview

i am doing a android application for tablet in that
i have a gridView in a view pager
what i want to do is,
i want to add menu to imageview in the gridView like google books
Please see this image for reference
http://i.stack.imgur.com/cf4Or.jpg
i tried placing this in the gridView imageadaptor , no luck
public boolean onOptionsItemSelected(MenuItem item) {
// my code
}
Thanks for your time
You need to use PopupMenu:
http://developer.android.com/reference/android/widget/PopupMenu.html
You need to setup a PopupMenu giving a view as an anchor + specify the menu recourse that will be used here. Of course this should be done inside a OnClickListener for your view.
anchorView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(context, anchorView);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_one:
// item one clicked
return true;
case R.id.item_two:
// item two clicked
return true;
}
return false;
}
);
popupMenu.inflate(R.menu.popup_menu);
popupMenu.show();
}
});
Define a standard menu resource - here is your popup_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item_one"
android:title="Item one"/>
<item
android:id="#+id/item_two"
android:title="Item two"/>
</menu>
Here is a nice tutorial that you can look at:
http://javatechig.com/android/popup-menu-in-android-example
yourImageViewObject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(activity, v);
/** Adding menu items to the popumenu */
popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.spam:
Toast.makeText(activity, "Spam clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.blockuser:
Toast.makeText(activity, " Block user clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.remove:
Toast.makeText(activity, "Remove clicked",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
}
});
popup.show();
}
});
menu file main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/spam"
android:title="Spam"/>
<item
android:id="#+id/blockuser"
android:title="Block User"/>
<item
android:id="#+id/remove"
android:title="Remove"/>
</menu>
put above main.xml in menu folder inside res folder
What you need is a PopupMenu. Create a view that can act as the anchor for the menu and add the PopupMenu as follows:
PopupMenu myPopup = new PopupMenu(context, myAnchor);
myAnchor.setOnTouchListener(myPopup.getDragToOpenListener());
You can follow the below steps... I hope this may help you
Step 1 : Create a menu items in menu resource folder
Somewhat like this :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menuitemone"
android:title="Cut"/>
<item android:id="#+id/menuitemtwo"
android:title="Copy" />
<item android:id="#+id/menuitemthree"
android:title="Paste"/>
</menu>
Step 2: Define an Image view for your individual griditem layout(according to your choice of placement)
Step 3: Define ImageView click listner in your BaseAdaptor class
Step 4: In onclick listner paste this code to show drop down menu
PopupMenu popup = new PopupMenu(MainActivity.this, arg1);
popup.getMenuInflater().inflate(R.menu.menuname, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Log.i("TEST",item.getTitle());
return true;
}
});
popup.show();
You may need to see if you want a custom design for the same.
Google is your friend.

Android PopupMenu checkable item does not check

I have this simple PopupMenu, but when I click on an item, the item doesn't get checked. Why?
In documentation is:
Menu items in the Icon Menu (from the options menu) cannot display a checkbox or radio button.
Radio button is showing, but only state is not changing...
Java
public void showSortPopup() {
View menuItemView = findViewById(R.id.action_sort);
PopupMenu popup = new PopupMenu(this, menuItemView);
popup.inflate(R.menu.sort);
popup.getMenu().findItem(R.id.sort_def).setChecked(true);
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.sort_def:
sortOrder = CardCursorContract.CardCursor.DEFAULT_SORT;
mCardsFragment.setSortOrder(sortOrder);
savePref();
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
break;
case R.id.sort_asc:
sortOrder = CardCursorContract.CardCursor.ALPHABETICAL_ASC_SORT;
mCardsFragment.setSortOrder(sortOrder);
savePref();
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
break;
case R.id.sort_desc:
sortOrder = CardCursorContract.CardCursor.ALPHABETICAL_DESC_SORT;
mCardsFragment.setSortOrder(sortOrder);
savePref();
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
break;
default:
break;
}
return false;
}
});
popup.show();
}
XML-File
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="#+id/sort_def"
android:title="#string/action_sort_def"
android:orderInCategory="1"
android:showAsAction="always" />
<item android:id="#+id/sort_asc"
android:title="#string/action_sort_name"
android:orderInCategory="2"
android:showAsAction="always" />
<item
android:id="#+id/sort_desc"
android:title="#string/action_sort_name_desc"
android:orderInCategory="3"
android:showAsAction="always"/>
</group>
Screen
First of all you can simplify the if checked else statement to
item.setChecked(!item.isChecked())
That way it will always toggle it from true → false and the other way around.
But the problem lies in the fact that you have radiobuttons, what the statement above does is makes the group checked, but what you want is to have the item checked.
To get the behaviour youre looking for you can use item.getSubmMenu() and then use the setChecked method on the particular subMenuItem you want.
For example:
//This will refer to the default, ascending or descending item.
MenuItem subMenuItem = item.getSubMenu().getItem(INDEX_OF_ITEM);
//Check or uncheck it.
subMenuItem.setChecked(!subMenuItem.isChecked());
**Swap These Lines in every case **
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
To
if (item.isChecked()) item.setChecked(true);
else item.setChecked(false);

Categories

Resources