I have a problem with changing of popupmenu title.
My goal is that there is a join menu in popupmenu list.
After user join the app by using the popupmenu button , I want to change the "join" title to "User profile".
But I don't know how to change the title of popupmenu.
If there has a solution, let me know how to change.
Here is the code
<item android:id="#+id/menu6"
android:title="join"/>
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_menu://popupbutton
PopupMenu popup = new PopupMenu(getApplicationContext(), v);
getMenuInflater().inflate(R.menu.main_menu, popup.getMenu());
popup.setOnMenuItemClickListener(popupClick);
popup.show();
}
PopupMenu.OnMenuItemClickListener popupClick = new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuitem) {
switch (menuitem.getItemId()) {
case R.id.menu6: // here is a code of join
break;
}
You can follow this answer
Change PopupMenu items' title programatically
.
First create a boolean variable
private boolean menu6;
Create a menu object to check which popup item is clicked
Menu menuOpts = popup.getMenu();
if (menu6) {
menuOpts.getItem(1).setTitle("User profile");
}
Modify onMenuItemClick to this
switch (menuitem.getItemId()) {
case R.id.menu6: // here is a code of join
menu6 = true
break;
}
Related
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.
I have created multiple buttons in my main activity, on click of each button a toast message would be displayed and on Long-click of each button, a popup-menu would be displayed.
As normal popup-menu displays the contents in drop-down format, but I want the popup menu contents to be displayed on Top of the button once the button is Long pressed. I have used menu.XML file to store the items for the popup menu.
Here is the CODE:
public boolean onLongClick(View v) {
#SuppressLint("ResourceType")
PopupMenu popup = new PopupMenu(this, v,Gravity.TOP);
popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
switch (v.getId()) {
case R.id.btn1:
popup.show();
break;
case R.id.btn2:
popup.show();
break;
case R.id.btn3:
popup.show();
break;
case R.id.btn4:
popup.show();
break;
}
return false;
}
}
I have gone through many sites but most of them all are customized only by adding icons to the popup menu.
So Please, do let me know some of the ways to find the solution to it.
Thank you
I want to open the menu, the problem is my game uses full screen so there is no toolbar, no room for a FAB and no menu button. I have tried to use
openOptionsMenu();
in an onClickListener from a button on the screen but it does nothing. If you have any suggestions please reply here.
The answer I found was to use the PopupMenu, see the example and comments below.
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
public void onClick(View view) {
switch (view.getId()) {
case R.id.new_game_button:
//start new game
break;
case R.id.menu_button:
/** Instantiating PopupMenu class */
PopupMenu popup = new PopupMenu(getBaseContext(), view);
/** Adding menu items to the popumenu */
popup.getMenuInflater().inflate(R.menu.game_menu, popup.getMenu());
/** Defining menu item click listener for the popup menu */
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_more_apps:
//do something
return true;
case R.id.menu_about:
// do something
return true;
case R.id.menu_like_us:
//do something
return true;
break;
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
I am using the following code for displaying pop up menus
PopupMenu popupMenu = new PopupMenu(MainActivity.this, v);
popupMenu.getMenuInflater().inflate(R.menu.popupmenu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,
item.toString(),
Toast.LENGTH_LONG).show();
return true;
}
});
popupMenu.show();
}
But the menu is covering a lot of my screen area. How to specify the size of the popup menu ?
You'll have to create a custom Dialog. See the official docs on how to do this here:
https://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog