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
}
});
}
Related
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();
}
});
}
I have implemented popup menu in button onClick, with AppCompact theme I can get the menu overflow with empty items in menu. please help me on this
OptionMenuBtn = (ImageButton) v.findViewById(R.id.three_dot);
OptionMenuBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated m// openOptionsMenu();
showPopup(v);
}
});
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.action_menu, popup.getMenu());
popup.show();
}
this should be the Xml file :
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_edit"
android:title="#string/menu_edit" />
<item
android:id="#+id/menu_block"
android:title="#string/menu_deactivate" /></menu>
and the code in activity on your onclick
PopupMenu popupMenu = new PopupMenu(YourActivity.this, view);
popupMenu.setOnMenuItemClickListener(YourActivity.this);
popupMenu.inflate(R.menu.menu_import_export);
popupMenu.show();
and implements :
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
//TODO
return true;
case R.id.menu_block:
//TODO
return true;
}
return false;
}
I am trying to play a sound file through a menu item which exists in a fragment.
Here is the full code of my fragment,
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_one, container,false);
ImageButton popupCTelevision = (ImageButton)rootview.findViewById(R.id.chineseTelevision);
ImageButton popupCBathe = (ImageButton)rootview.findViewById(R.id.chineseBathe);
ImageButton popupCSick = (ImageButton)rootview.findViewById(R.id.chineseSick);
popupCTelevision.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getActivity().getApplicationContext(), v);
popupMenu.inflate(R.menu.menu_chinese_television);
popupMenu.show();
}
});
popupCBathe.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getActivity().getApplicationContext(), v);
popupMenu.inflate(R.menu.menu_chinese_bathe);
popupMenu.show();
}
});
popupCSick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getActivity().getApplicationContext(), v);
popupMenu.inflate(R.menu.menu_chinese_sick);
popupMenu.show();
}
});
return rootview;
}
public boolean onMenuItemClick(MenuItem item){
switch(item.getItemId()) {
case R.id.c_bathe:
final MediaPlayer CSick = MediaPlayer.create(getActivity(), R.raw.c_bathe);
MenuItem playCSick = (MenuItem) getView().findViewById(R.id.c_sick);
CSick.start();
playCSick.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
CSick.start();
return true;
}
});
default:
return false;
}
}
}
I think the issue is that the method onMenuItemClick is not used at all. How do I implement this method for the code?
How do I do it similarly to pressing a button to play the sound file?
final MediaPlayer englishSoundMP = MediaPlayer.create(this, R.raw.english_bath);
Button playEnglishBath = (Button) this.findViewById(R.id.play_english);
playEnglishBath.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
englishSoundMP.start();
}
});
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();
}
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);