I am trying to style the items of my popup so they look like a list of buttons below eachother. The only problem is that I can't get to change anything of the popup items. I have tried to set a global popupMenuStyle in my app style but that didn't to anything. I tried to set an actionLayout on the menu items but still no change.
How can I change the styling of my popup menu items?
My menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/test1"
android:title="Test" />
<item android:id="#+id/test2"
android:title="Test 2" />
</menu>
How I open the popup menu:
PopupMenu popupMenu = new PopupMenu(getContext(), mButton);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.show();
Try this as part of your styles.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:popupMenuStyle">#style/PopupMenu</item>
<item name="android:textAppearanceLargePopupMenu">#style/myPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceSmallPopupMenu">#style/myPopupMenuTextAppearanceSmall</item>
</style>
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#FFFFFF</item>
<item name="android:divider">#444444</item>
<item name="android:dividerHeight">1px</item>
<item name="android:background">#FFFFFF</item>
</style>
<style name="myPopupMenuTextAppearanceSmall" parent="#android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Small">
<item name="android:textColor">#000000</item>
<item name="android:textSize">12sp</item>
<item name="android:background">#FFFFFF</item>
</style>
<style name="myPopupMenuTextAppearanceLarge" parent="#android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Large">
<item name="android:textColor">#000000</item>
<item name="android:textSize">18sp</item>
<item name="android:background">#FFFFFF</item>
</style>
</resources>
and then in your xml layout file for the activity add this line:
style="#style/AppTheme"
or in your AndroidManifest.xml file, add this to the application tag:
android:theme="#style/AppTheme"
This will affect how Android renders a popup menu in your app.
Related
i try change background color option menu from black to white
but my code not work.i see this link too How to change background color popup menu android but its not work for me.
styles
<style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo">
</style>
<style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/ColorPrimary</item>
<item name="popupMenuStyle">#style/PopupMenu</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
</style>
<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">#android:color/white</item>
</style>
</resources>
popup_menu.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">
<!--///showAsAction="always" ///-->
<item android:id="#+id/action_settings" android:title="Share" showAsAction="always" />
<item android:id="#+id/action_settings1" android:title="Bluetooth" showAsAction="always" />
<item android:id="#+id/action_settings2" android:title="Exit" showAsAction="always" />
<!--/android:showAsAction="ifRoom"/-->
<item android:id="#+id/action_settings3" android:title="Share" android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings4" android:title="Bluetooth" android:showAsAction="ifRoom" />
</menu>
How to change background color option menu
Have you controlled the context you passed to the constructor? When you create a PopupMenu class, please use the Activity context like this :
PopupMenu popup = new PopupMenu(this, anchorView);//Not BaseContext
Whether you use android.widget.PopupMenu or android.support.v7.widget.PopupMenu? They governed by different theme attributes :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">#style/PopupMenu</item>
<!-- if using android.support.v7.widget.PopupMenu -->
<item name="popupMenuStyle">#style/PopupMenu</item>
</style/>
<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">#android:color/white</item>
</style>
Effect like this.
How to change the text color of submenu in Android? I customize the application theme and override the relative attributes, but it still doesn't work. My menu has two submenus, which are initially hidden, when clicked, it shows. However, the style of submenus can't be modified, while the title of action bar can. This question bothers me all the day, I almost try every ways I find.
This is my code!
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.classsignin.MainActivity" >
<item
android:id="#+id/action_overflow"
android:title="分享"
android:icon="#drawable/drop_select"
android:showAsAction="always"
>
<menu >
<item
android:id="#+id/absent"
android:title="请假"
android:icon="#drawable/absent"
/>
<item
android:id="#+id/refresh"
android:title="刷新课程"
android:icon="#drawable/refresh"
/>
</menu>
</item>
styles.xml
<style name="CustomTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBarTheme</item>
<item name="android:actionMenuTextAppearance">#style/MyActionBarMenu</item>
<item name="android:actionMenuTextColor">#color/blue</item>
<item name="android:homeAsUpIndicator">#drawable/back</item>
<item name="android:spinnerItemStyle">#style/MySpinnerItem</item>
</style>
<style name="MyActionBarTheme" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/white</item>
<item name="android:titleTextStyle">#style/MyActionBarTitle</item>
</style>
<style name="MyActionBarTitle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/blue</item>
</style>
<style name="MyActionBarMenu" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/blue</item>
</style>
<style name="MySpinnerItem" parent="#android:style/Widget.Holo.TextView.SpinnerItem">
<item name="android:textAppearance">#style/MyTextAppearance</item>
</style>
<style name="MyTextAppearance" parent="#android:style/TextAppearance.Holo.Widget.TextView.SpinnerItem">
<item name="android:textColor">#color/blue</item>
</style>
Essentially you will want to customise your MenuItem's style by having the android:itemBackground attribute overridden with your custom color/drawable selector in your style.xml and as well a vtextColor` attribute to override.
In case your menu has also submenu's, then you will want as well to style the header title of the submenu, which usually is automatically with White background by overriding the attribute actionBarPopupTheme with your custom style.
style.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="android:itemBackground">#drawable/menu_popup_selector</item>
<item name="actionBarPopupTheme">#style/SubmenuHeaderStyle</item>
</style>
<style name="SubmenuHeaderStyle" parent="ThemeOverlay.AppCompat.Light">
<item name="android:colorBackground">#color/colorPrimary</item>
<item name="android:textColor">#color/colorAccent</item>
</style>
</resources>
menu_popup_selector.xml
<?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>
And you will have something like those screenshots
(1st menu and then the submenu after clicking one item on the 1st menu - the pink header is my submenu header).
Just for you to know when I have the normal menu without submenu would look like this hierarchy views:
<menu>
<item/>
<item/>
<item/>
</menu>
and a menu with submenu as this example of hierarchy views:
<menu>
<item/>
<item/>
<group>
<item>
<menu>
item
item
item
</menu>
</item>
</group>
<item/>
<item/>
</menu>
I want to change default divider color of my option menu, so can give me some suggestion or technical?
Its working for me in Navigationmenu.
This below line is mandatory.
<item name="android:listDivider">#color/white</item>
//This is ur style
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:listDivider">#color/white</item>
</style>
//This is ur menu item.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_profile"
android:icon="#drawable/m_profile"
android:title="#string/my_profile" />
<group android:id="#+id/my_id">
<item
android:id="#+id/nav_coming"
android:icon="#drawable/m_coming"
android:title="#string/comingsoon" />
</group>
</group>
</menu>
you can do it by setting theme to your popup menu
use below code in your style.xml file in res folder
<style name="MyThemePopup" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:textColor">#color/white</item>
<item name="android:dividerHeight">2px</item>
<item name="android:divider">#F00</item>
<item name="android:popupMenuStyle">#style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#color/opaque_black_color_dark</item>
</style>
above code is for creating theme where <item name="android:divider">#F00</item> is use for setting color to divider
after that you can set created theme to your activity in which popup menu is present through manifest file
<activity
android:name="com.android.MainActivity"
android:theme="#style/MyThemePopup" />
hope this code will help you to achieve your requirement happy coding :)
Does nothing for me, maybe because of the appCompat version?
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="android:dividerHeight">2dp</item>
<item name="android:divider">#color/ucc_blue</item>
</style>
It doesn't matter if i put it in the MyActionBar style or in the AppTheme style both won't work. But setting the divider height does make my OptionMenu scrollable....
Maybe it doesn't work because they are subMenuItems?
add this item to the app theme:
<style name="theme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:dropDownListViewStyle">#style/MyDropDownListView</item>
</style>
<style name="MyDropDownListView" parent="#android:style/Widget.ListView.DropDown">
<item name="android:divider">#color/transparent</item>
</style>
For those who have not found the answer yet for this problem, this is how it worked for me and hope it will work for you as well:
<style name="PopupMenu">
<item name="android:itemBackground">#color/background_medium_gray</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:colorBackground">#color/BackgroundGray</item>
<item name="android:dividerHeight">1dp</item>
</style>
Context context = new ContextThemeWrapper(getActivity(), R.style.PopupMenu);
final PopupMenu popupMenu = new PopupMenu(context, view);
final MenuInflater menuInflater = popupMenu.getMenuInflater();
I want to modify the menu item's font color in ActionBarSherlock. I try few solution: Action Bar menu item text color, How to style the menu items on an Android action bar and so on, but doesn't work for my app.
here is my style code:
<style name="onTime.ActionBar.Root" parent="Widget.Sherlock.ActionBar">
<item name="android:logo">#drawable/ic_actionbar</item>
<item name="android:titleTextStyle">#style/onTime.Title.Root</item>
</style>
<style name="onTime.Title.Root" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
<item name="android:textColor">#color/font_testing</item>
</style>
<style name="onTime.ActionBar.Default" parent="onTime.ActionBar.Root">
<item name="android:background">#color/action_bar_grey</item>
</style>
<style name="onTime.Theme.Default" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/onTime.ActionBar.Default</item>
</style>
my menu xml (inflated in onCreateOptionsMenu() ):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_update"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/menu_update" />
</menu>
How can I modify menu font's color?
Try
<style name="YOURTHEME.ActionBar.TitleTextStyle" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
<item name="android:textColor">#color/YOUR_COLOR</item>
<item name="textColor">#color/YOUR_COLOR</item>
<item name="android:textSize">#dimension/MEDIUM_TEXT</item>
<item name="textSize">#dimension/MEDIUM_TEXT</item>
</style>
I am using a .png for both my logo and action Bar menu items. I am using the android.Holo.light theme.
Even though I've made the background transparent for both the logo and the menu items, a grey shaded area is still showing behind the .png. How do I change this?
Take a look at my menu XML and my style override XML below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:selectableItemBackground">#color/transparent</item>
<item name="android:background">#00000000</item>
</style>
<style name="ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar" >
<item name="android:selectableItemBackground">#color/transparent</item>
<item name="android:background">#00000000</item>
</style>
<style name="textTitle"
parent="#android:style/TextAppearance">
<item name="android:textSize">20dp</item>
<item name="android:textColor">#DC0451</item>
<item name="android:textStyle">bold</item>
</style>
<style name="textDescription"
parent="#android:style/TextAppearance">
<item name="android:textSize">15dp</item>
<item name="android:textColor">#666666</item>
</style>
</resources>
Menu xml
<item
android:id="#+id/Category"
android:title="Category"
android:showAsAction="always|withText"
android:icon="#drawable/menuitemselect"
style="#style/ActionBar"
>
<menu>
<item
android:id="#+id/catalog"
android:title="Main Catalog"
android:icon="#drawable/menuitemselect"
android:onClick="catalogClick"
/>
<item android:id="#+id/newvideos"
android:title="New"
android:icon="#drawable/menuitemselect"
android:onClick="newVideoClick"
style="#style/ActionBar"
/>
<item
android:id="#+id/popularvideos"
android:title="Popular"
android:icon="#drawable/menuitemselect"
android:onClick="popularVideoClick"
style="#style/ActionBar"
/>
</menu>
</item>
<item android:id="#+id/backButton"
android:title="Back"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_launcher"
/>
</menu>
Thank you
I found that the issue was with :
<item name="android:selectableItemBackground">#color/transparent</item>.
I added it in an attempt to get rid of default highlight feature. It seems when I have this set, the grey background shows up. When I remove it, it acts as it should.