You can obviously customize the PopupMenu in your own way. Its quite simple. But first you need to take care of one very important thing.
The PopupMenu is created like this,
PopupMenu popupMenu = new PopupMenu(context, anchorView);
Now the "context" here plays a very important role in styling. The style of the PopupMenu depends on the style of the context that you pass. So be very careful in this. I wasted almost half-and-hour figuring out this.
If you are in a fragment just pass "getActivity()" and you are done.
**Styling of the PopupMenu items**
Just override the following items in your style,
<item name="textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
<item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
<item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
And customize the text appearance as you want,
<style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
<item name="android:textColor">@color/text_hint_color</item>
</style>
<style name="myPopupMenuTextAppearanceLarge" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">
<item name="android:textColor">@color/text_hint_color</item>
</style>
If you want to change the background of the PopupMenu, do this
<item name="popupMenuStyle">@style/myPopupMenuStyle</item>
<item name="android:popupMenuStyle">@style/myPopupMenuStyle</item>
**Advanced Styling**
The PopupMenu items do not support showing the icons by default. But showing the icons can make it look so much better. I recommend trying it.
To implement this just put the following code in your activity and you are good to go,
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (NoSuchMethodException e) {
Log.e("tag", "onMenuOpened", e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
Hope it helps you all. Thanks.
You can obviously customize the PopupMenu in your own way. Its quite simple. But first you need to take care of one very important thing.
The PopupMenu is created like this,
PopupMenu popupMenu = new PopupMenu(context, anchorView);
Now the "context" here plays a very important role in styling. The style of the PopupMenu depends on the style of the context that you pass. So be very careful in this. I wasted almost half-and-hour figuring out this.
If you are in a fragment just pass "getActivity()" and you are done.
**Styling of the PopupMenu items**
Just override the following items in your style,
<item name="textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
<item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
<item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
And customize the text appearance as you want,
<style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
<item name="android:textColor">@color/text_hint_color</item>
</style>
<style name="myPopupMenuTextAppearanceLarge" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">
<item name="android:textColor">@color/text_hint_color</item>
</style>
If you want to change the background of the PopupMenu, do this
<item name="popupMenuStyle">@style/myPopupMenuStyle</item>
<item name="android:popupMenuStyle">@style/myPopupMenuStyle</item>
**Advanced Styling**
The PopupMenu items do not support showing the icons by default. But showing the icons can make it look so much better. I recommend trying it.
To implement this just put the following code in your activity and you are good to go,
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (NoSuchMethodException e) {
Log.e("tag", "onMenuOpened", e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
Hope it helps.