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();
}
});
}
Related
i have a recycle view and on it item click i want to popup menu but it have too much width and going out side of the recycle view item width how to set it any body help me out of this problem thanks in advance.
here is image of problem
My menu Code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<item android:id="#+id/edit"
android:title="Edit"/>
<item android:id="#+id/delete"
android:title="Delete"/>
code of recycler view itemclick
holder.img_menu.setOnClickListener(new View.OnClickListener() {
#SuppressLint("ResourceType")
#Override
public void onClick(final View v) {
PopupMenu popupMenu = new PopupMenu(v.getContext(), holder.img_menu);
popupMenu.inflate(R.menu.feedoptions);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.edit:
Toast.makeText(v.getContext(), "Edit will Available Soon", Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
Toast.makeText(v.getContext(), "Delete Option Will Available Soon", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
});
popupMenu.show();
}
});
In Recycler View I click on a button add to cart a dialog is open.
In Recycler view adapter
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.addtocart.setOnClickListener((Mp3HindiLandingActivity) context);
}
In My Activity(Mp3HindiLandingActivity)
#Override
public void onClick(final View v) {
switch (v.getId()) { case R.id.addtocart:
PopupMenu popup = new PopupMenu(ctx, v);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
Toast.makeText(v.getContext(), "FriendRequest", Toast.LENGTH_LONG).show();
return true;
case R.id.menu2:
Toast.makeText(v.getContext(), "Block | Hide ", Toast.LENGTH_LONG).show();
return true;
default:
return false;
}
}
});
popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());
popup.show();
break;`
}}
It Shows the the menu below addtocart icon but I need the menu items having different background as per the image attached?
Please let me know how this will be implemented
you can try this.
paletteViewHolder.btncart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
PopupMenu popup = new PopupMenu(paletteViewHolder.imgpopup.getContext(), v);
// This activity implements OnMenuItemClickListener .
//popup.setOnMenuItemClickListener ((OnMenuItemClickListener) this);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.friendrequest:
Toast.makeText(v.getContext(), "FriendRequest", Toast.LENGTH_LONG).show();
return true;
case R.id.blockhide:
Toast.makeText(v.getContext(), "Block | Hide ", Toast.LENGTH_LONG).show();
return true;
case R.id.followunfollow:
Toast.makeText(v.getContext(), "Follow/UnFollow", Toast.LENGTH_LONG).show();
return true;
default:
return false;
}
//return false;
}
});
popup.inflate(R.menu.menu);
popup.show();
}
});
//create below menu.xml file on menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu1"
android:title="menu1"/>
<item
android:id="#+id/menu2"
android:title="menu2"/>
<item
android:id="#+id/menu3"
android:title="menu3"/>
</menu>
its helpfull for you.
First create an inner class ViewHolder likes below:
private class MyViewHolder extends RecyclerView.ViewHolder {
public Button btnCart;
public MyViewHolder(View view) {
super(view);
btnCart = (Button) view.findViewById(R.id.btnCart);
}
}
Then override onBindViewHolder in your adapter (your adapter extends RecyclerView.Adapter).
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.btnCart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// call the opening of your dialog here
}
});
}
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();
}
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();
}
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);