简体   繁体   中英

How to change a title of popupmenu

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM