How to set ListView style in the resource? - android

I want to set the ListView style in the Theme, so that all the ListView of the Application will inherit the style. My code looks like this:
style.xml
<!-- Light Theme of the Application -->
<style name="AppTheme.Light" parent="Theme.Sherlock.Light">
<item name="actionBarStyle">#style/ActionBar.Green</item>
<item name="android:absListViewStyle">#style/AbsListViewStyle.Light</item>
</style>
<style name="AbsListViewStyle.Light" parent="android:style/Widget.AbsListView">
<item name="background">#color/background_light</item>
<item name="android:divider">#color/light_gray</item>
<item name="android:dividerHeight">1dp</item>
<item name="android:listSelector">#drawable/actionbar_background_green</item>
<item name="android:drawSelectorOnTop">false</item>
<item name="android:cacheColorHint">#00000000</item>
</style>
I set the theme of the Activity in the AndroidManifest.xml.
But it seems that the style is not applied to the ListView.
Why is that?

Finally I got the reason, I should use android:listViewStyle attribute instead of absListViewStyle:
style.xml
<!-- Light Theme of the Application -->
<style name="AppTheme.Light" parent="Theme.Sherlock.Light">
<item name="actionBarStyle">#style/ActionBar.Green</item>
<item name="android:listViewStyle">#style/ListViewStyle.Light</item>
</style>
<style name="ListViewStyle.Light" parent="android:style/Widget.ListView">
<item name="background">#color/background_light</item>
<item name="android:divider">#color/light_gray</item>
<item name="android:dividerHeight">1dp</item>
<item name="android:listSelector">#drawable/actionbar_background_green</item>
<item name="android:drawSelectorOnTop">false</item>
<item name="android:cacheColorHint">#00000000</item>
</style>
In this way I can apply ListView style in the Theme xml.

You need to set it on your ListView in xml for list view add:
style="#style/AbsListViewStyle.Light"

Related

Changing Color of Tab Text in ActionBarActivity

I have an app using the support ActionBarActivity (android.support.v7.app.ActionBarActivity), with tabs. I used the Action Bar Style Generator to change the look of the app, but now I am having a hard time getting the tab text color to change.
The style is being applied through the manifest, and shows up just fine except for the tab text color remains black no matter what I try. Here's what I have in my custom styles xml file right now:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.StyledActionBar" parent="#style/Theme.AppCompat.Light">
<item name="actionBarItemBackground">#drawable/selectable_background_StyledActionBar</item>
<item name="popupMenuStyle">#style/PopupMenu.StyledActionBar</item>
<item name="dropDownListViewStyle">#style/DropDownListView.StyledActionBar</item>
<item name="actionBarTabStyle">#style/ActionBarTabStyle.StyledActionBar</item>
<item name="actionDropDownStyle">#style/DropDownNav.StyledActionBar</item>
<item name="actionBarStyle">#style/ActionBar.Solid.StyledActionBar</item>
<item name="actionModeBackground">#drawable/cab_background_top_StyledActionBar</item>
<item name="actionModeSplitBackground">#drawable/cab_background_bottom_StyledActionBar</item>
<item name="actionModeCloseButtonStyle">#style/ActionButton.CloseMode.StyledActionBar</item>
<item name="actionBarTabTextStyle">#style/TabTextStyle</item>
<item name="android:actionBarTabTextStyle">#style/TabTextStyle</item>
</style>
<style name="TabTextStyle" parent="#style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="ActionBar.Solid.StyledActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="background">#drawable/ab_solid_StyledActionBar</item>
<item name="backgroundStacked">#drawable/ab_stacked_solid_StyledActionBar</item>
<item name="backgroundSplit">#drawable/ab_bottom_solid_StyledActionBar</item>
<item name="progressBarStyle">#style/ProgressBar.StyledActionBar</item>
</style>
<style name="ActionBar.Transparent.StyledActionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="background">#drawable/ab_transparent_StyledActionBar</item>
<item name="progressBarStyle">#style/ProgressBar.StyledActionBar</item>
</style>
<style name="PopupMenu.StyledActionBar" parent="#style/Widget.AppCompat.Light.PopupMenu">
<item name="android:popupBackground">#drawable/menu_dropdown_panel_StyledActionBar</item>
</style>
<style name="DropDownListView.StyledActionBar" parent="#style/Widget.AppCompat.Light.ListView.DropDown">
<item name="android:listSelector">#drawable/selectable_background_StyledActionBar</item>
</style>
<style name="ActionBarTabStyle.StyledActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.TabView">
<item name="android:background">#drawable/tab_indicator_ab_StyledActionBar</item>
</style>
<style name="DropDownNav.StyledActionBar" parent="#style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar">
<item name="android:background">#drawable/spinner_background_ab_StyledActionBar</item>
<item name="android:popupBackground">#drawable/menu_dropdown_panel_StyledActionBar</item>
<item name="android:dropDownSelector">#drawable/selectable_background_StyledActionBar</item>
</style>
<style name="ProgressBar.StyledActionBar" parent="#style/Widget.AppCompat.ProgressBar.Horizontal">
<item name="android:progressDrawable">#drawable/progress_horizontal_StyledActionBar</item>
</style>
<style name="ActionButton.CloseMode.StyledActionBar" parent="#style/Widget.AppCompat.Light.ActionButton.CloseMode">
<item name="android:background">#drawable/btn_cab_done_StyledActionBar</item>
</style>
<!-- this style is only referenced in a Light.DarkActionBar based theme -->
<style name="Theme.StyledActionBar.Widget" parent="#style/Theme.AppCompat">
<item name="popupMenuStyle">#style/PopupMenu.StyledActionBar</item>
<item name="dropDownListViewStyle">#style/DropDownListView.StyledActionBar</item>
</style>
</resources>
As you can see, I tried to change the color to white, but to no effect. I also was trying to change the Action Bar title color, but gave up because I found a java workaround.
ActionBarActivity is deprecated.
And you are using Theme.AppCompat : ActionBarActivity is deprecated
Try to use AppCompatActivity in your java codes(Activity).

Cant change colorButtonNormal value to all buttons

I am trying to make all buttons in my app have default color using colorButtonNormal in my style.
It works nice on API 21 and above but under API 21 it only changes the background of some buttons and i dont know what is going wrong.
styles.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowContentOverlay">#null</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="colorPrimary">#color/btn_login</item>
<item name="colorPrimaryDark">#color/bg_login</item>
<item name="colorAccent">#color/btn_login</item>
<item name="colorButtonNormal">#color/btn_login</item>
</style>
</resources>
v21/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowContentOverlay">#null</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="android:alertDialogTheme">#style/AlertDialogCustom</item>
<item name="android:colorButtonNormal">#color/btn_login</item>
<item name="colorPrimary">#color/btn_login</item>
<item name="colorPrimaryDark">#color/bg_login</item>
<item name="colorAccent">#color/btn_login</item>
</style>
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorPrimary">#color/btn_login</item>
<item name="android:colorAccent">#color/btn_login</item>
<item name="colorAccent">#color/btn_login</item>
<item name="colorPrimary">#color/btn_login</item>
<item name="colorPrimaryDark">#color/bg_login</item>
</style>
<style name="Preference" parent="Theme.AppCompat.Light">
<item name="android:textColorPrimary">#color/black</item>
<item name="android:colorPrimary">#color/btn_login</item>
<item name="android:colorAccent">#color/btn_login</item>
<item name="android:editTextColor">#color/black</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:alertDialogTheme">#style/AlertDialogCustom</item>
<item name="colorAccent">#color/btn_login</item>
</style>
<style name="EditTextThemeCustom" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="android:editTextColor">#color/black</item>
<item name="android:textColor">#color/black</item>
<item name="colorAccent">#color/btn_login</item>
</style>
</resources>
Result:
Lollipop
and
Kitkat
Any suggestions?
Add to your styles.xml
<style name="ColoredButton" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/btn_login</item>
</style>
and then use
android:theme="#style/ColoredButton"
as one of your buttons' attributes
The buttons you inflate will get automatically translated to AppCompatButton.
Wherever you have new Button(context) you need to use new AppCompatButton(context) instead for Material theme colors to apply.
I was getting this issue in old devices(<21) because, my activity was extending just activity when I make in it extend AppCompatActivity, in old devices also it is working perfect.

buttonStyle not working for 22.1.1

I´m Using com.android.support:appcompat-v7:22.1.1
This attribute android:buttonStyle is not setting that style for every button, I need to set android:theme="#style/AppTheme.Button" manually on each button.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:buttonStyle">#style/AppTheme.Button</item>
</style>
What do I need to do to set a general style for it?
Use buttonStyle insteadof android:buttonStyle in your theme
Similar to #danielgomezrico answer but without having two style files:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- android:buttonStyle for v21+ and buttonStyle for the rest -->
<item name="buttonStyle">#style/MyCustomButton</item>
<item name="android:buttonStyle">#style/MyCustomButton</item>
<item name="colorButtonNormal">#color/my_color_accent</item>
</style>
<style name="MyCustomButton" parent="Base.Widget.AppCompat.Button">
<item name="android:textColor">#ffffff</item>
</style>
I ended up setting background color with colorButtonNormal attribute and other style attributes with buttonStyle.
values/style.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/my_color_primary</item>
<item name="colorPrimaryDark">#color/my_color_primary_dark</item>
<item name="colorAccent">#color/my_color_accent</item>
</style>
<style name="AppTheme.Base">
<item name="colorButtonNormal">#color/my_color_accent</item>
<item name="buttonStyle">#style/Button</item>
</style>
<style name="Button" parent="Base.Widget.AppCompat.Button">
<item name="android:textColor">#color/my_color_white</item>
</style>
values-v21/style.xml
<style name="AppTheme.Base">
<item name="android:colorButtonNormal">#color/my_color_accent</item>
<item name="android:buttonStyle">#style/Button</item>
</style>
And then all the buttons have the background/text color I wanted without setting style on each one.

insert a custom style inside two different theme

I want to add a custom style that is for example purple in one theme and red in one other theme.
<style name="subtitle_layout" parent="#android:style/Theme.Holo.Light">
<item name="android:background">#color/purple</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">40sp</item>
</style>
<style name="subtitle_layout2" parent="#android:style/Theme.Holo.Light">
<item name="android:background">#color/black</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">80sp</item>
</style>
<style name="Theme1" parent="android:Theme.Holo.Light">
<item name="android:textColor">#color/black</item>
<item name="#style/subtitle_layout">#style/subtitle_layout</item>
</style>
<style name="Theme2" parent="android:Theme.Holo.Light">
<item name="android:textColor">#color/white</item>
<item name="#style/subtitle_layout">#style/subtitle_layout2</item>
</style>
The change of theme works because i see the change of text color, but instead of android:textColor i would like to put a style instead. But it doesn't work.
I guess you are trying to inherit existing styles to your theme, am I right? You can just set the style as your parent theme:
<style name="Theme1" parent="#style/subtitle_layout">
<item name="android:textColor">#color/black</item>
</style>

ActionBarSherlock - How to set the padding of each actionbar's icon?

How can I make the icons of the actionbar using ActionBarSherlock closer or further away from each other? Something like android:layout_marginLeftor android:paddingRight?
If I just add the icons on the actionbar they are further away compare to the screenshot of Soundhound below:
my themes.xml:
<style name="Theme.AstraTheme_Purple" parent="#style/Theme.Sherlock">
<item name="actionBarStyle">#style/Widget.AstraTheme_Purple.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.AstraTheme_Purple.ActionBar</item>
<item name="actionButtonStyle">#style/Widget.AstraTheme_Purple.ActionButton</item>
<item name="android:actionButtonStyle">#style/Widget.AstraTheme_Purple.ActionButton</item>
</style>
<style name="Widget.AstraTheme_Purple.ActionBar" parent="Widget.Sherlock.ActionBar">
<item name="android:background">#drawable/purple_bar</item>
<item name="background">#drawable/purple_bar</item>
</style>
<style name="Widget.AstraTheme_Purple.ActionButton" parent="Widget.Sherlock.ActionButton">
<item name="android:minWidth">50dip</item>
</style>
<style name="AppTheme" parent="Theme.Sherlock">
<item name="actionButtonStyle">#style/MyActionButtonStyle</item>
<item name="android:actionButtonStyle">#style/MyActionButtonStyle</item>
</style>
<style name="MyActionButtonStyle" parent="Widget.Sherlock.ActionButton">
<item name="android:minWidth">32dip</item>
<item name="android:padding">0dip</item>
</style>
The default value of android:minWidth for the ActionBar buttons is 56dip.
Don't forget to set #style/AppTheme as the Activity's theme.

Categories

Resources