Reputation: 131
I have created a popup-menu
and want to change the text of each item in the menu
to blue. I have not created a style for the popup-menu
.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/Meaning"
android:title="TOOL 29 - MEANING"/>
<item
android:id="@+id/ShiftState"
android:title="TOOL 30 - SHIFT STATE"/>
<item
android:id="@+id/Recreate"
android:title="TOOL 31 - RECREATE"/>
<item
android:id="@+id/Rules"
android:title="TOOL 32 - RULES"/>
</menu>
btnEmotionTools = (ImageView) findViewById(R.id.btnEmotionTools);
btnEmotionTools.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(Screen2_Witnessing.this,btnEmotionTools);
popup.getMenuInflater().inflate(R.menu.popup_menu_emotion_tools, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
itemId = item.getItemId();
if ( itemId == R.id.Meaning ){
screen = "2";
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Screen,screen);
editor.commit();
calculate();
Intent i = new Intent(getApplicationContext(), Screen109_Meaning.class);
startActivity(i);
}
else if ( itemId == R.id.ShiftState ) {
screen = "2";
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Screen,screen);
editor.commit();
calculate();
Intent i = new Intent(getApplicationContext(), Screen125b_Shift_State.class);
startActivity(i);
}
else if ( itemId == R.id.Recreate ) {
screen = "2";
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Screen,screen);
editor.commit();
calculate();
Intent i = new Intent(getApplicationContext(), Screen138_Recreate.class);
startActivity(i);
}
else if ( itemId == R.id.Rules ) {
screen = "2";
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Screen,screen);
editor.commit();
calculate();
Intent i = new Intent(getApplicationContext(), Screen122_Rules.class);
startActivity(i);
}
return true;
}
});
popup.show();
}
});
Upvotes: 1
Views: 573
Reputation: 820
Try like this:
PopupMenu popup = new PopupMenu(getActivity(), btnEmotionTools);
popup.getMenuInflater().inflate(R.menu.popup_menu_emotion_tools, popup.getMenu());
for (int i = 0; i < popup.getMenu().size(); i++) {
MenuItem item = popup.getMenu().getItem(i);
SpannableString spanString = new SpannableString(popup.getMenu().getItem(i).getTitle().toString());
spanString.setSpan(new ForegroundColorSpan(Color.RED), 0, spanString.length(), 0);
item.setTitle(spanString);
}
popup.show();
Upvotes: 1
Reputation: 1802
Give Style in style.xml
<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<item name="android:popupBackground">@color/yourcolor</item>
</style>
Java Code
Context colorContext= new ContextThemeWrapper(getContext(), R.style.PopupMenu);
PopupMenu popup = new PopupMenu(colorContext,btnEmotionTools);
Upvotes: 0