I have a Listview with a ContextMenu, but when I setIcon for ContextMenu look like it doesn't work
public void onCreateContextMenu(ContextMenu menu , View v,
ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.context_menu_favorite)
.setIcon(android.R.drawable.btn_star);
}
Android context menu appears when user press long click on the element. It is also known as floating menu. It affects the selected content while doing action on it. It doesn't support item shortcuts and icons.
Open the app -> Java -> Package -> Mainactivity.In this step, add the code to show the ContextMenu. Whenever the app will strat make a long click on a text and display the number of options to select of them for specific purposes. Comments are added inside the code to understand the code in more detail.
MainActivity.registerForContextMenu(listView); After a long press on the particular item and selecting the option we can return selected value and process it. Adding Listview and text view to the xml file. You can also customize the listview with icons and images based on your android context menu requirement.
Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.
Context menus do not support icons.
Note: Context menu items do not support icons or shortcut keys.
I did it by this way:
Reference screenshot:
menu_patient_language.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.PatientSelectionActivity">
<item
android:id="@+id/menuEnglish"
android:icon="@drawable/language_english"
android:title="@string/english" />
<item
android:id="@+id/menuFrench"
android:icon="@drawable/language_french"
android:title="@string/french" />
</menu>
style.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:popupMenuStyle">@style/popupMenuStyle</item>
</style>
<!--- Language selection popup -->
<style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:itemBackground">@android:color/white</item>
</style>
private void showPopup(View v) {
Context wrapper = new ContextThemeWrapper(this, R.style.popupMenuStyle);
PopupMenu mypopupmenu = new PopupMenu(wrapper, v);
setForceShowIcon(mypopupmenu);
MenuInflater inflater = mypopupmenu.getMenuInflater();
inflater.inflate(R.menu.menu_patient_language, mypopupmenu.getMenu());
mypopupmenu.show();
// mypopupmenu.getMenu().getItem(0).setIcon(getResources().getDrawable(R.mipmap.ic_launcher));
mypopupmenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
txtPreferredLanguage.setText(item.getTitle().toString());
switch (item.getItemId()) {
case R.id.menuEnglish:
// Your code goes here
break;
case R.id.menuFrench:
// Your code goes here
break;
}
return false;
}
});
}
private void setForceShowIcon(PopupMenu popupMenu) {
try {
Field[] mFields = popupMenu.getClass().getDeclaredFields();
for (Field field : mFields) {
if ("mPopup".equals(field.getName())) {
field.setAccessible(true);
Object menuPopupHelper = field.get(popupMenu);
Class<?> popupHelper = Class.forName(menuPopupHelper.getClass().getName());
Method mMethods = popupHelper.getMethod("setForceShowIcon", boolean.class);
mMethods.invoke(menuPopupHelper, true);
break;
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
Hope this would help you sure.
Done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With