i'm trying apply style android popup menu. menu shown on button click. in example want set black menu background.
so, menu layout:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="item 1" android:id="@+id/menu1"> </item> <item android:title="item 2" android:id="@+id/menu2"> </item> </menu>
next, activity layout:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.michal.popupmenu.mainactivity"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new button" android:id="@+id/button" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:onclick="btnclick" android:nestedscrollingenabled="true"/> </relativelayout>
code show menu on button click:
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void btnclick(view view) { showmenu(view); } public void showmenu(view v) { popupmenu popup = new popupmenu(this, v); popup.inflate(r.menu.popup_menu); popup.show(); } }
styles xmle generated automatically. have added menu style set black menu background, here is:
<style name="popupmenu" parent="widget.appcompat.popupmenu"> <item name="android:popupbackground">@android:color/black</item> </style> <!-- base application theme. --> <style name="apptheme" parent="theme.appcompat.light.darkactionbar"> <!-- customize theme here. --> <item name="colorprimary">@color/colorprimary</item> <item name="colorprimarydark">@color/colorprimarydark</item> <item name="coloraccent">@color/coloraccent</item> <item name="popupmenustyle">@style/popupmenu</item> </style>
but still menu background white , should black. ideas what's wrong?
[edit] according comments, updated code:
<resources> <style name="popupmenu" parent="@style/widget.appcompat.light.popupmenu"> <item name="android:popupbackground">#ff4081</item> </style> <!-- base application theme. --> <style name="apptheme" parent="theme.appcompat.light.darkactionbar"> <!-- customize theme here. --> <item name="colorprimary">@color/colorprimary</item> <item name="colorprimarydark">@color/colorprimarydark</item> <item name="coloraccent">@color/coloraccent</item> </style> </resources>
main activity:
public void showmenu(view v) { context wrapper = new contextthemewrapper(getapplicationcontext(), r.style.popupmenu); popupmenu popup = new popupmenu(wrapper, v); // activity implements onmenuitemclicklistener //popup.setonmenuitemclicklistener((popupmenu.onmenuitemclicklistener) this); popup.inflate(r.menu.popup_menu); popup.show(); }
result not expect
menu background still black, not color want set.
i tried solution mentioned above, popupmenu color not changed, ended doing follows:
- created custom drawable required color follows:
popup_color_drawable
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <color android:color="@color/colorprimary"/> </item> <item> <color android:color="#655611"/> </item> </selector>
added state_pressed select effect
2. in styles.xml added following code
<style name="mypopupmenu" parent="widget.appcompat.popupmenu"> <item name="android:itembackground">@drawable/popup_color_drawable</item> </style>
i using theme.appcompat.light.darkactionbar
base theme app.
- then in activity
public void showpopup(view view){ context wrapper = new contextthemewrapper(this, r.style.mypopupmenu); popupmenu popup = new popupmenu(wrapper, view); popup.inflate(r.menu.popup_menu); popup.show(); }
hopes helps you.
ps. getting code formatting issue stackoverflow need use blockquote on code formatter.
Comments
Post a Comment