listview Popupmenu displays false value - android

Im trying to display the item name of the listview when its popupmenu is being clicked but i'm have trouble with it because it always show or returns "false" value. Any ideas how to solve this? thanks
public void toast(View v){
showPopupMenu(v);
}
private void showPopupMenu(View v){
PopupMenu pop = new PopupMenu(CompanyActivity.this, v);
pop.getMenuInflater().inflate(R.menu.menu,pop.getMenu());
pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId() == R.id.view){
Toast.makeText(getApplicationContext(),item.getItemId(),Toast.LENGTH_SHORT)
.show();
return true;
}
return false;
}
});
pop.show();
}
Edited: I want to get the value of the list item in the listview being clicked but it shows the menu item that is being clicked.
public void toast(View v){
showPopupMenu(v);
}
private void showPopupMenu(View v){
PopupMenu pop = new PopupMenu(CompanyActivity.this, v);
pop.getMenuInflater().inflate(R.menu.menu,pop.getMenu());
pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(CompanyActivity.this,"text: "+item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
pop.show();
}

The "name" of your ListView items depends entirely on the adapter you're using for the ListView. Keep in mind that the menu item id is unrelated to the listview view id, which is unrelated to the adapter item id. In order for the popup menu to know which item was clicked, you'll need to use the list item position to look it up from the adapter. It'll work something like this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showPopupMenu(view, position);
}
private void showPopupMenu(View anchor, final int position) {
PopupMenu popupMenu = new PopupMenu(
this,
anchor);
popupMenu.inflate(R.menu.menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Depending on what type of object backs your adapter, you may have
// to do something different here.
String title = String.valueOf(mListView.getAdapter().getItem(position));
Toast.makeText(MyActivity.this, "text: " + title, Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
}

Related

Popup Menu not working as expected in Android

I have a recycler view and card view. The card layout has 3 dots which on clicked, shows popup menu. Everything is working fine, but there is a small problem. If I clicked the top card, popup menu is displayed at the bottom of recycler view. If I clicked the middle card the popup menu is displayed at the top-left of the activity and like so on. I want that, if card_1's popup menu is pressed, it should show at card_1 and similarly, popup menu should displayed at their respective cards. I don't know where the problem is. Please help!!!
Here is the Menu Layout menu_options.xml
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/menu1"
android:title="Edit " />
<item
android:id="#+id/menu2"
android:title="Delete " />
Here is the method by which menu shows up. It is in CardViewAdapter used for my Recycler View.
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
...
threeDots = (TextView)cardView.findViewById(R.id.options);
threeDots.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(context, threeDots);
popupMenu.inflate(R.menu.menu_options);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.menu1: int pos = holder.getAdapterPosition();
onEdit(pos);
break;
case R.id.menu2: Toast.makeText(context, "Swipe LEFT to delete the card", Toast.LENGTH_LONG)
.show();
break;
}
return false;
}
});
popupMenu.show();
}
});
}
Use this instead:
public void onBindViewHolder(final ViewHolder holder, final int position) {
...
holder.threeDots.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(context, holder.threeDots);
popupMenu.inflate(R.menu.menu_options);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.menu1: int pos = holder.getAdapterPosition();
onEdit(pos);
break;
case R.id.menu2: Toast.makeText(context, "Swipe LEFT to delete the card", Toast.LENGTH_LONG)
.show();
break;
}
return false;
}
});
popupMenu.show();
}
});
}

how to update menu item title in onMenuItemClick()

I have created cardview as single item of RecyclerView
and in my cardview, I have created a popup menu programmatically..
I want to change the title of menu item at run-time
is it possible to update menu item title in onMenuItemClick() method?
My HomeFragment.java looks like-
private void showPopupMenu(View view, int position,String post_key) {
// inflate menu
PopupMenu popup = new PopupMenu(view.getContext(),view );
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.card_view_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new MyMenuItemClickListener(position,post_key));
popup.show();
}
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
private int position;
DatabaseReference mPostDatabase;
private String post_key;
Menu menu;
MyMenuItemClickListener(int positon,String post_key) {
this.position = positon;
this.post_key=post_key;
mPostDatabase=FirebaseDatabase.getInstance().getReference().child("Posts");
}
#Override
public boolean onMenuItemClick(final MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.delete_post:
mPostDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(post_key)){
String from_id= String.valueOf(dataSnapshot.child(post_key).child("from").getValue());
if(from_id.equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){
mPostDatabase.child(post_key).removeValue();
}
else {
//here i want to change the title
item.setTitle("Save Post");//it doesn't work
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
return false;
}
}
after this line :
//here i want to change the title
item.setTitle("Save Post");
call below function
invalidateOptionsMenu();

How to get listitem id when I click on PopUp menu item?

In my application I have attached popup menu with each item in listview. The popup menu has further two items when we click on popup menu icon.I have implemented OnMenuItemClickListener in my activity to listen for popup menu item clicks which is working fine.But the problem is that How do I get to know the listitem id (not popup menu item id) when I click on popup menu icon for any listview item.The popup menu code is below:
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_play:
return true;
default:
return false;
}
}
Please tell me what is "listitem id" that you want to know? I doubt that it's a "listitem view's id". Probably you're thinking about "position", right?
I don't know where do you call showPopup(View v) from, but you also need to pass the position there:
public void showPopup(View v, int listItemPosition) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
Your goal is to know this position in the onMenuItemClick(MenuItem item) callback.
The simplest way to achieve this is to create variable "listItemPositionForPopupMenu", store this position there and read it in the onMenuItemClick callback:
private int listItemPositionForPopupMenu;
public void showPopup(View v, int listItemPosition) {
listItemPositionForPopupMenu = listItemPosition;
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_play:
// read the listItemPositionForPopupMenu here
return true;
default:
return false;
}
}
You can also do it in many other ways, like creating you own OnMenuItemClickListener listener with listItemPosition variable in constructor and create custom interface with onMenuItemClick(MenuItem item, int listItemPosition). Or you can just create an anonymous class, then you don't need to have the listItemPositionForPopupMenu member variable:
public void showPopup(View v, final int listItemPosition) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_play:
// read the listItemPosition here
return true;
default:
return false;
}
}
});
popup.inflate(R.menu.actions);
popup.show();
}

Show popup menu on `ActionBar` item click

I have an ActionBar with an action item on it. After clicking on the action item, I want to show a popup menu.
I implemented this method, but I want to anchor it to the action item or to the ActionBar, not to any view from layout. How to get some kind of view to anchor it from MenuItem?
public boolean onOptionsItemSelected(MenuItem item) {
PopupMenu popupMenu = new PopupMenu(this, ??????); // What view goes here?
popupMenu.inflate(R.menu.counters_overflow);
popupMenu.show();
// ...
return true;
}
So finally I found solution. When you want to anchor popupmenu to ActionItem in ActionBar you need to find view that renders ActionItem.
Simple find view with findViewById() where id is same as id of your menu item in xml.
DISPLAYING POPUP:
public boolean onOptionsItemSelected(MenuItem item) {
// ...
View menuItemView = findViewById(R.id.menu_overflow); // SAME ID AS MENU ID
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.counters_overflow);
// ...
popupMenu.show();
// ...
return true;
}
MENU:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
....
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/ic_overflow"
android:showAsAction="ifRoom"
android:title="#string/menu_overflow"/>
....
</menu>
If menu item is not visible (is in overflow) it does not work. findViewById returns null so you have to check for this situation and anchor to another view.
The accepted answer wasn't working for me, so I found the problem by trial and error.
public boolean onOptionsItemSelected(MenuItem item)
{
View menuItemView = findViewById(item.getItemId());
showPopupMenu(menuItemView)
return true;
}
private void showPopupMenu(View anchor)
{
PopupMenu popup = new PopupMenu(this, anchor);
popup.getMenuInflater().inflate(R.menu.my_popup_menu, popup.getMenu());
popup.show();
}
The key here is that, the item in onOptionsItemSelected(MenuItem item) must be shown on ActionBar. If the item is one of the items that appear when you press the 3 vertical dots on top right of ActionBar, then your app will crash.
plz try this ..
#Override
public boolean onOptionsItemSelected(MenuItem item){
String str=item.getTitle().toString();
Toast.makeText(getBaseContext(), str,Toast.LENGTH_LONG). show();
View view=findViewById(item.getItemId());
switch(view.getId()){
case Menu.FIRST:
showPopup(view); // calling method
}
return super.onOptionsItemSelected(item);
}
// custom method
private void showPopup(final View view) {
PopupMenu popupMenu = new PopupMenu(view.getContext(), view);
popupMenu.getMenu().add(0, 0, Menu.NONE, "Item 1");
popupMenu.getMenu().add(0, 1, Menu.NONE, "Item 2");
popupMenu.getMenu().add(0, 2, Menu.NONE, "Item 3");
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(view.getContext(), item.getTitle() + "clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
}
In addition to the Accepted Answer, the issue of re-inflating the popup on each call to onOptionsItemSelected() method can be simplified by doing it only once and just showing it as many times as we want.
(this works even for custom toolbar inflated via Menu Layout Inflater at runtime. Just keep in mind that the findViewById() to get the Menu item's view can return non-null value only when the view is actually present on the screen, ie, should be visible on the toolbar/actionbar.
Note: If the view is preset in the overflow menu of toolbar/actionBar there might be a chance that the view might get inflated only after the overflow menu was invoked at least once - using 3 dots?)
public class SomeActivity{
private PopupMenu popup;
.... // some code of the activity
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
int menuItem = item.getItemId();
switch (menuItem) {
case R.id.tb_menu_plus:
View menuItemView = findViewById(R.id.tb_menu_plus);
if(popup == null) {
popup = new PopupMenu(this, menuItemView);
popup.inflate(R.menu.dropdown_popup_menu);
}
popup.show();
return true;
}
return super.onOptionsItemSelected(item);
}
If you don't want the menu to be gone when you click specific item return false on that item.
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId())
{
case R.id.itemShare_:
onShareClicked();
return true; // the menu will be gone
case R.id.itemCopy_:
onCopyClicked();
return true; // the menu will be gone
case R.id.itemSelectAll_:
onSelectAllClicked();
return false; // the menu will stay
// ...
}
public boolean onOptionsItemSelected(MenuItem item) {
final View addView = getLayoutInflater().inflate(R.layout.add, null);
new AlertDialog.Builder(this).setTitle("Add a Word").setView(addView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
addWord((TextView) addView.findViewById(R.id.title));
}
}).setNegativeButton("Cancel", null).show();
return (super.onOptionsItemSelected(item));
}
get full source form here..
http://vimaltuts.com/android-tutorial-for-beginners/android-action-bar-tab-menu-example

How to add new items to popup menu after click

I have a button:
<Button
android:id="#+id/bot_button1"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="24sp"
android:text="#string/bot_button1_tx" />
This is my main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.bot_button1);
button1.setOnClickListener(onClickListener);
Log.d(className, "onCreate");
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.bot_button1:
showPopupMenu(v, 1);
break;
}
}
};
private void showPopupMenu(final View v, Integer i){
PopupMenu popupMenu = new PopupMenu(MainActivity.this, v);
switch (i) {
case 1:
popupMenu.getMenuInflater().inflate(R.menu.menu1, popupMenu.getMenu());
break;
}
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
new AlertDialog.Builder(activity).
setTitle("TITLE").
setMessage("MESSAGE").
setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO: user specific action
dialog.dismiss();
}
}).create().show();
return true;
}
});
popupMenu.show();
}
How can I display more items when an item from the PopupMenu is selected (clicked)? Ideal case scenario: PopupMenu remains visible after selection, and a new PopupMenu appears right next to the selected item. OR Popup Menu expands to show the subitems.
I tried to introduce a second PopupMenu onMenuItemClick(), but it only replaces the first PopupMenu.
I used a new popup menu to solve this issue
public OnMenuItemClickListener listener = new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu2_3_3:
popupMenu2.getMenuInflater().inflate(R.menu.menu2_3_3, popupMenu2.getMenu());
popupMenu2.show();
popupMenu2.setOnMenuItemClickListener(listener2);

Categories

Resources