Change Popupmenu Items' Title Programmatically
I have a PopupMenu with 4 options: add as friend, as neighbour, as workmate and as fan. What I want to do is if you click, lets say, on 'Add as friend', then that option changes in
Solution 1:
I've found a solution. I added 4 boolean variables and modified the showPopup and addContact methods like this:
private boolean friend, workmate, neighbour, fan;
publicvoidshowPopup(View v){
Context wrapper = newContextThemeWrapper(this, R.style.PopupPerfilAjeno);
PopupMenu popup = newPopupMenu(wrapper, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.menu);
Menu menuOpts = popup.getMenu();
if (friend) {
menuOpts.getItem(0).setTitle(R.string.remove_friends);
}
if (workmate) {
menuOpts.getItem(1).setTitle(R.string.remove_workamtes);
}
if (neighbour) {
menuOpts.getItem(2).setTitle(R.string.remove_neighbours);
}
if (fan) {
menuOpts.getItem(3).setTitle(R.string.remove_fans);
}
popup.show();
}
privatevoidaddContact(finalint circle){
switch (circle) {
case1:
friend = true;
workmate = false;
neighbour = false;
fan = false;
break;
case2:
friend = false;
workmate = true;
neighbour = false;
fan = false;
break;
case3:
friend = false;
workmate = false;
neighbour = true;
fan = false;
break;
case4:
friend = false;
workmate = false;
neighbour = false;
fan = true;
break;
}
}
Solution 2:
Inflate another menu when showing popup again.
publicvoidshowPopup(View v) {
Contextwrapper=newContextThemeWrapper(this, R.style.PopupPerfilAjeno);
PopupMenupopup=newPopupMenu(wrapper, v);
popup.setOnMenuItemClickListener(this);
if (condition) popup.inflate(R.menu.menu);
else popup.inflate(R.menu.menu2);
popup.show();
menuOpts = popup.getMenu();
}
Post a Comment for "Change Popupmenu Items' Title Programmatically"