I want popup menu like this
that show it after click on button
XML
<item
android:state_pressed="true"
android:id="@+id/fromFirstMonth"
android:title="از ابتدای سال"
android:drawable="@drawable/nav_item_background"/>
<item
android:state_pressed="true"
android:id="@+id/currentMonth"
android:title="این ماه"
android:drawable="@color/blueMenu"/>
<item
xmlns:showAsAction="always"
android:id="@+id/currentSession"
android:title="این فصل"
android:drawable="@color/white"/>
<item
xmlns:showAsAction="always"
android:id="@+id/selection"
android:title="تانتخابی"
android:drawable="@color/blueMenu"/>
</menu>
Java
hourglass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(Products.this, hourglass);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.hourglass_item, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(Products.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});
I want set different background color for each item. i set android:drawable
but this does not worked.
I can with this code can change text color but i does not know how can change background color item
Menu menu=popup.getMenu();
MenuItem item = menu.getItem(0);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
item.setTitle(s);