listview item
Popup-menu :
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.edusols.HomeActivity" >
<item
app:showAsAction="never"
android:id="#+id/one"
android:title="abcd" />
<item
android:id="#+id/two"
android:title="Two"/>
<item
android:id="#+id/three"
android:title="Three"/>
</menu>
I have a listview, i have option like edit post and all that
I want popup-menu on particular position, i am getting pop-up menu but not at correct position
I have set pop-up menu in listview adapter
code:
ivOptionMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (post_user_id.equals(preferenceHelper.getID()))
{
PopupMenu popup = new PopupMenu(ctx, ivOptionMenu);
popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(ctx,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
else {
PopupMenu popup = new PopupMenu(ctx, ivOptionMenu);
popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(ctx,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
How menu appears on click
Create xml file in /res/menu/popup_menu.xml and add these code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action1"
android:title="delete" />
<item
android:id="#+id/action2"
android:title="properties" />
<item
android:id="#+id/action3"
android:title="add to queue" />
<item
android:id="#+id/action4"
android:title="play next" />
<item
android:id="#+id/action5"
android:title="add to favorite" />
</menu>
Use this code in java file:
mViewHolder.imagemore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final PopupMenu menu = new PopupMenu(context, v);
menu.getMenuInflater().inflate(R.menu.popup_menu, menu.getMenu());
menu.show();
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action1:
deleteSong(position);
break;
case R.id.action2:
Toast.makeText(context, "your desire action is " + item.toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.action3:
Toast.makeText(context, "your desire action is " + item.toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.action4:
Toast.makeText(context, "your desire action is " + item.toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.action5:
Toast.makeText(context, "your desire action is " + item.toString(), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
});
}
});
PopMenuConstructor(ctx,view)
view by which PopMenu will connected.
#Override
public void onClick(View v) {
if (post_user_id.equals(preferenceHelper.getID())) {
//PopupMenu popup = new PopupMenu(ctx, ivOptionMenu);
//change to this
PopupMenu popup = new PopupMenu(ctx, v);
.
.
}
}
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 my android app I have a menu option which works when the device has dedicated hardware menu option. for another device I don't have dedicated Menu button so I tried to add a button and on click of that a popup will display the menu, The popup is working but not displaying the options, and further to that how to work on the selected popup option.
My button layout is as follows:
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_overflow_holo_dark"
android:contentDescription="#string/descr_overflow_button"
android:onClick="showPopup" />
This is my code to show the popup:
public boolean showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.emailmenu, popup.getMenu());
popup.show();
return true;
}
And this my option code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/bluebutton" android:icon="#drawable/icon_blue_btn"
android:title="" />
<item android:id="#+id/zephyr" android:icon="#drawable/icon_zephyrme"
android:title="" />
<item android:id="#+id/skype" android:icon="#drawable/icon_skype"
android:title="" />
</menu>
I all ready have this onCreateOptionsMenu() for triggering the option from dedicated menu key. How to get it to work from popup.
If you want to show Icons instead of Title, then create your PopupMenu like below
The method setForceShowIcon(menu); will force the PopMenu to show icons.
you can have text with icons too.
private void showPopupMenu(){
PopupMenu menu=new PopupMenu(this,anchorView);
menu.getMenuInflater().inflate(R.menu.popup_menu,menu.getMenu());
setForceShowIcon(menu);
menu.setOnMenuItemClickListener(menuClickListner);
menu.show();
}
public static void setForceShowIcon(PopupMenu popupMenu) {
try {
Field[] fields = popupMenu.getClass().getDeclaredFields();
for (Field field : fields) {
if ("mPopup".equals(field.getName())) {
field.setAccessible(true);
Object menuPopupHelper = field.get(popupMenu);
Class<?> classPopupHelper = Class.forName(menuPopupHelper
.getClass().getName());
Method setForceIcons = classPopupHelper.getMethod(
"setForceShowIcon", boolean.class);
setForceIcons.invoke(menuPopupHelper, true);
break;
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
//This is Menu click listner
PopupMenu.OnMenuItemClickListener menuClickListner = new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.some_id1:
//actions here
break;
case R.id.some_id2:
//actions here..
break;
case R.id.some_id3:
break;
}
return false;
}
};
Hope this helps!
try like this
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity(new Intent(MainActivity.this, SecounActivity.class));
return true;
}
});
popup.show();
So I am having a problem with my popup menu. It is working fine except that nothing happens when a menu item is clicked on. Here is the onClick event for the menuButton:
menuButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, menuButton);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if(item.getTitle() == "Stats") {
Intent intent = new Intent(activity, StatsActivity.class);
activity.startActivity(intent);
} else if(item.getTitle() == "Reset") {
Intent intent = new Intent(activity, PrestiegeActivity.class);
activity.startActivity(intent);
}
return true;
}
});
popup.show();
}
});
popup_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/Stats"
android:title="Stats"/>
<item
android:id="#+id/Reset"
android:title="Reset"/>
<item
android:id="#+id/three"
android:title="Three"/>
</menu>
I am not really sure where problem occurring is.
Do not make the comparisons with the title strings. Instead compare the id.
switch (item.getItemId()) {
case R.id.Stats:
doStuff();
return true;
case R.id.Reset:
...
return true;
}
Also you cannot compare strings with the == operator. Instead you must use theString.equals(theOtherString)
I've implemented a popup menu to my android application. I've created a xml for popup menu and code also works fine. Now what i cant figure out is how to handle popup menu items click. I've tried using PopupMenu.OnMenuItemClickListener but was not successful. How can i do this?
My code for popup menu
ImageButton button = (ImageButton) view.findViewById(R.id.popUp_song);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(activity, v);
Menu m = popup.getMenu();
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.song_popup, popup.getMenu());
if (audio.getDownload().equals("0")) {
m.removeItem(R.id.add_download);
}
popup.show();
}
});
xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/ToolBarStyle">
<item
android:id="#+id/add_queue"
android:title="Add to queue" />
<item
android:id="#+id/play_next"
android:title="Add to favourite" />
<item
android:id="#+id/add_download"
android:title="Download" />
</menu>
Before showing the PopupMenu add a listener for PopupMenu for handling the click events.
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(),
item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
If you need to take a click of the popup menu on the basis of id, below is the right way to do this:
popup.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.miEmail) {
Toast.makeText(getApplicationContext(), "Email clicked", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == R.id.miCall) {
Toast.makeText(getApplicationContext(), "Call clicked", Toast.LENGTH_SHORT).show();
}
return true;
});
I want to show DropDown menu on MenuItem click just like this.
Like this
Note that this item was added like:
<item
android:id="#+id/menu_item_action_parameters"
android:title="#string/text_parameters"
android:icon="#drawable/ic_menu_parameter"
app:showAsAction="ifRoom|withText"/>
</item>
And in my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.menu_item_action_parameters:
// What to do here?
break;
}
return super.onOptionsItemSelected(item);
}
I have seen this link but I have came to know that ActionBar.setListNavigationCallbacks() has been deprecated.
Thanks!
Create your menu xml as follow
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_action_parameters"
android:title="#string/text_parameters"
android:icon="#drawable/ic_menu_parameter"
app:showAsAction="ifRoom|withText"/> >
<menu>
<item
android:id="#+id/action_dropdown1"
android:title="#string/dropdown_1" />
<item
android:id="#+id/action_dropdown2"
android:title="#string/dropdown2" />
<item
android:id="#+id/action_dropdown3"
android:title="#string/dropdown3" />
</menu>
</item>
<item
more item
</item>
</menu>
Then
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_dropdown1:
...
return true;
case R.id.action_dropdown2:
...
return true;
...
default:
return super.onOptionsItemSelected(item);
}
}
try custom popup menu
menu.Xml
<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/one"
android:title="One"/>
<item
android:id="#+id/two"
android:title="Two"/>
<item
android:id="#+id/three"
android:title="Three"/>
</menu>
call this code on buttonClick
button = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}
what about showing popup menu when clicking onthat item ?
here is the code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_notifi) {
// here we show the popup menu
popup();
}
return super.onOptionsItemSelected(item);
}
public void popup(){
PopupMenu popup = new PopupMenu(MainActivity.context, v); //the v is the view that you click replace it with your menuitem like : menu.getItem(1)
popup.getMenuInflater().inflate(R.menu.medecin_list_menu,
popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item2) {
switch (item2.getItemId()) {
case R.id.Appeler:
//do somehting
break;
case R.id.EnvoyerMsg:
//do somehting
break;
case R.id.AfficherDet:
//do somehting
break;
case R.id.Afficher:
//do somehting
break;
case R.id.AvoirRdv:
//do somehting
break;
default:
break;
}
return true;
}
});
}
});
}
and here is the medecin_list_menu (my menu)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/Appeler"
android:title="#string/Appeler" />
<item
android:id="#+id/EnvoyerMsg"
android:title="#string/envoyerMsg" />
<item
android:id="#+id/Afficher"
android:title="#string/Afficher" />
<item
android:id="#+id/AvoirRdv"
android:title="#string/avoirRdv" />
<item
android:id="#+id/AfficherDet"
android:title="#string/afficherDet" />
</menu>
Last Edit:
see this tutorial http://www.androidhive.info/2013/11/android-working-with-action-bar/