How to set the background colour of a Popup menu? - android

Hey everyone I am dealing with a small problem with Popup menu. Honestly I dont know where is the problem, I tried to search the internet if I can find any answers but unfortunatelly I cant. So..
I created a custom toolbar and set the activity to no actionbar. Then I created a popUp menu in this toolbar. First I tried to change the icon of a menu via styles and it worked. But then I wanted to change the background and text color of a menu items.
This is the code I have in my design tab of the menu item:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/fitItem1"
android:title="Item 1"
app:showAsAction="never" />
<item android:id="#+id/fitItem2"
android:title="Item 2"
app:showAsAction="never"/>
<item android:id="#+id/fitItem3"
android:title="Item 3"
app:showAsAction="never"/>
And even in the design menu it looked like this:
But when you start the app it looks by default (white background, black text)
So I thought that is it set somewhere in styles but there is nothing like that and every guide I tried to follow had no effect. Except style name="MyPopBackground" which actully changes the background.
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
<item name="android:itemTextAppearance">#style/MyApp.PopupMenu</item>
<item name="android:popupBackground">#style/MyPopBackground</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="MyActionButtonOverflow" parent="android:Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/triple_dot_icon</item>
</style>
<style name="MyApp.PopupMenu" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#color/colorLightGrey</item>
</style>
<style name="MyPopBackground" parent="android:style/Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">#color/colorGrey</item>
</style>
</resources>
There might be some useless styles but since I didnt know where is the prob. I left them there. My goal is to make that menu look like that one the image showed before.
I would be glad for any help, thanks for help.

Add popupMenu style to your AppTheme
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:popupMenuStyle">#style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#android:color/white</item>
</style>
then at mainifest.xml
<application
android:theme="#style/AppTheme" >
</application>

In style AppTheme, You add <item name="android:itemBackground">#color/colorMenu</item>

Related

Proper toolbar styling

I would like to avoid applying a style to the toolbars in all layouts, so I have a style I created for it:
<style name="DarkToolbar" >
<item name="android:textColorPrimary">#color/white</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:background">#color/blue</item>
<item name="android:elevation">4dp</item>
</style>
And I added it to my theme as follows:
<style name="Theme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/blue</item>
<item name="colorPrimaryDark">#color/blue</item>
<item name="colorAccent">#color/blue</item>
<item name="android:toolbarStyle">#style/DarkToolbar</item>
</style>
This is the resulting toolbar:
As you can see, the title and subtitle have the wrong formatting. If I remove the style from the theme and apply it directly to the toolbar in the layout, I get the following result instead:
Both images use the same style. One is applied through the app theme with
<item name="android:toolbarStyle">#style/DarkToolbar</item>
and the second one is applied directly to the toolbar with
style="#style/DarkToolbar"
Does anyone know the reason for the difference?
Also, does anyone know why the
<item name="android:textColorPrimary">#color/white</item>
<item name="android:textColorSecondary">#color/white</item>
Are not being applied?
Thanks
Add parent="Widget.AppCompat.Toolbar" to your toolbar style. It is default style for toolbar.
For example:
<style name="DarkToolbar" parent="Widget.AppCompat.Toolbar">
<item name="android:textColorPrimary">#color/white</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:background">#color/blue</item>
<item name="android:elevation">4dp</item>
</style>
I think this has to do with
< item name="android:theme">#style/ThemeOverlay.AppCompat.Dark.ActionBar< /item>
in your style
I personally find styling toolbar text quite complicated, so mostly end up with my own TextView. How about using your own TextView inside toolbar. It would be easy, though a bit lengthy.
You are setting parent as no action bar in your theme, as this will give you the run time exception and null point exception as there is no action bar or tool bar to set the title/sub-title. Try adding parent in your theme with toolbar or action bar likes this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DarkToolbar">
<item name="android:textColorPrimary">#color/white</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:background">#color/blue</item>
<item name="android:elevation">4dp</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/blue</item>
<item name="colorPrimaryDark">#color/blue</item>
<item name="colorAccent">#color/blue</item>
<item name="android:toolbarStyle">#style/DarkToolbar</item>
</style>
</resources>

how to make the toolbar option drop down menu item to have left side custom check mark

In the toolbar's dropdown option menu, does anyone know how to add left side check mark in the menu item?
It is similar behavior as to use <group>, but the <group> gives a default radio button in the right side of the menu item. Thanks!
You can use setGroupCheckable()
EDIT-1:
use the following code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="all">
<item android:id="#+id/red"
android:title="#string/red" />
<item android:id="#+id/blue"
android:title="#string/blue" />
</group>
</menu>
The checkableBehavior="all" converts it into checkboxes.
Edit-2:
I had posted the following code somewhere.. Refer to get started on how to customize action bar menu items :
in your style file, place the following: 
<style name="MyAppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:titleTextStyle">#style/MyActionBar.TitleTextStyle
<item name="android:actionMenuTextAppearance">#style/MyActionBar.MenuTextStyle</item>
<item name="android:actionMenuTextColor">#color/action_bar_red</item> </style>
<style name="MyActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title"> <item name="android:textColor">#F0F</item> <item name="android:textStyle">bold</item>
<item name="android:textSize">24dip</item> </style>
<style name="MyActionBar.MenuTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Menu"> <item name="android:textColor">#F0F</item> <item name="android:textStyle">bold</item>
<item name="android:textSize">24dip</item>
</style>
You can make changes in the above style items to suit your requirements. I have not added separate styling for color though. As you might observe, I am just assigning red color (which I have declared in my colors file) to it. You may change as required.

Styling radio buttons on ActionBarCompat dropdown menu

I am working on an app that uses a theme that extends #style/Theme.AppCompat.Light.DarkActionBar.
On one of my activities, there is an Action Bar icon that shows three options with radiobuttons (here's an extract from the menu XML file):
<item
android:icon="#drawable/ic_action_filter_white"
android:showAsAction="always"
android:title=""
preparatest:showAsAction="always">
<menu>
<group android:checkableBehavior="single" >
<item
android:id="#+id/menu_filter_all"
android:title="#string/history_list_filter_all"
android:checked="true"/>
<item
android:id="#+id/menu_filter_pending"
android:title="#string/history_list_filter_pending"/>
<item
android:id="#+id/menu_filter_finished"
android:title="#string/history_list_filter_finished"/>
</group>
</menu>
</item>
I want to give these radiobuttons a custom style, but I don't know where to do it. On my styles.xml file I have defined the custom style as
<style name="RadioButtonPTGreenTheme" parent="android:Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/ptgreentheme_btn_radio_holo_light</item>
</style>
and tried using all these four options, but to no avail:
<item name="android:radioButtonStyle">#style/RadioButtonPTGreenTheme</item>
<item name="android:listChoiceIndicatorSingle">#style/RadioButtonPTGreenTheme</item>
<item name="android:radioButtonStyle">#drawable/ptgreentheme_btn_radio_holo_light</item>
<item name="android:listChoiceIndicatorSingle">#drawable/ptgreentheme_btn_radio_holo_light</item>
Does anyone know how to do this? Thanks!
See this blog post for a way to do it.
Long story short, android:actionBarWidgetTheme is used to style elements that depend on the Action Bar, so the android:radioButtonStyle should be put inside a style for actionBarWidgetTheme:
<style name="MyTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarWidgetTheme">#style/MyActionBarWidgetTheme</item>
</style>
<style name="MyActionBarWidgetTheme" parent="#android:style/Theme.Holo">
<item name="android:radioButtonStyle">#style/MyRadioButtonStyle</item>
</style>

Remove the Padding for Action Bar Menu Item using AppCompat

I am Using Overlay Action Bar By Using the Following Style to my Activity
style.xml
<style name="CustomActionBarTheme" parent="#style/Theme.AppCompat" >
<item name="windowActionBarOverlay">true</item>
<item name="android:background">#00f3ead8</item>
<item name="android:windowBackground">#drawable/logo1024</item>
</style>
I have only one menu item that is to be displayed Always
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menusettings"
android:orderInCategory="100"
android:icon="#drawable/settings"
android:title="Menu"
yourapp:showAsAction="always"
/>
<menu>
The Icon i.e the settings.png is getting padded by 8dp
But I want 0 padding on the top and bottom so that it covers the Entire Actionbar
Please Help me out in deep trouble very close to the deadline of the project
Thanks in advance
One year later...
Removing padding on v7.Toolbar's action buttons is still challenging.
After a lot of experimentation here is a working solution, tested on API 15 and up (theoretically should be working > API 7).
First of all, toolbar style (theme) should be applied like so
<android.support.v7.widget.Toolbar
app:theme="#style/AppTheme.Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
Next, values/styles.xml
<style name="AppTheme.Toolbar" parent="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="actionButtonStyle">#style/AppTheme.Toolbar.ActionButton</item>
</style>
<!-- this will be applied regardless api version -->
<style name="Base.AppTheme.Toolbar.ActionButton" parent="#style/Widget.AppCompat.ActionButton">
<item name="android:minWidth">0dp</item>
<item name="android:minHeight">0dp</item>
</style>
<!-- this will be applied on < API 17 -->
<style name="AppTheme.Toolbar.ActionButton" parent="#style/Base.AppTheme.Toolbar.ActionButton">
<item name="android:padding">0dp</item>
</style>
And the final move, values-v17/styles.xml
<!-- this will be applied on >= API 17 -->
<style name="AppTheme.Toolbar.ActionButton" parent="#style/Base.AppTheme.Toolbar.ActionButton">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
</style>
Well your question was about a year ago, but it may help to other peoples.
Check the solution here :
how to add padding between menu items in android?
Setting the padding alone may not help. You must use minWidth attribute too.
I'm using AppCompat too, absolutely works.
<style name="Theme.Styled" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionButtonStyle">#style/Widget.Styled.Base.ActionButton</item>
</style>
<style name="Widget.Styled.Base.ActionButton" parent="#style/Widget.AppCompat.Base.ActionButton">
<item name="android:minWidth">20dp</item>
<item name="android:paddingLeft">0dp</item>
<item name="android:paddingRight">15dp</item>
</style>

To have a transluscent background for an android activity?

I wish to have a transluscent background for an activity, so that the previous activity can be seen beneath this activity. Something like a transluscent menu that pops up over a videa being played in the background.
Is this possible? Can you tell me how?
Note:I cannot use the default transluscent theme from android, since I am using my own customised background and theme for my application.
Pls help. Below is my style.xml wherein my_btn and my_list are selectors :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="btnstyle" parent="#android:style/Widget.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="android:background">#drawable/my_btn</item>
</style>
<style name="liststyle" parent="#android:style/Widget.ListView">
<item name="android:listSelector">#drawable/my_list</item>
</style>
<style name="theme" parent="android:Theme.Translucent">
<item name="android:windowBackground">#drawable/background</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:buttonStyle">#style/btnstyle</item>
<item name="android:listViewStyle">#style/liststyle</item>
</style>
</resources>
Apply Translucent theme to your activity in AndroidManifest.xml
<activity android:theme="#android:style/Theme.Translucent">

Categories

Resources