So I have a spinner that I'm trying to apply a style to. I have a activity theme
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
And a spinner
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/entries"
I want the text of the spinner items to appear as white and to do so with xml styling. If I set android:textColor in AppTheme, everything works fine.
However this applies to other elements as well.
I have tried the following attribues in AppTheme. None of them have worked.
<item name="android:spinnerDropDownItemStyle">#style/SpinnerItem</item>
<item name="android:dropDownItemStyle">#style/SpinnerItem</item>
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
<style name="SpinnerItem">
<item name="android:textColor">#android:color/white</item>
</style>
Any help is appreciated.
You can change your AppTheme like this:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:spinnerStyle">#style/AppTheme.Spinner</item>
<item name="android:spinnerDropDownItemStyle">#style/AppTheme.DropDownSpinner</item>
</style>
<style name="AppTheme.Spinner" parent="Widget.AppCompat.Spinner">
<item name="android:textColor">#color/white</item>
</style>
<style name="AppTheme.DropDownSpinner" parent="Widget.AppCompat.DropDownItem.Spinner">
<item name="android:textColor">#color/white</item>
</style>
Related
My problem is that I don't know how to change underline color of EditText - I have LG Nexus 5 (Android 6).
I would like to change normal underline color to white. I used all params like (style):
colorControlNormal
colorControlActivated
colorControlHighlight
textColorSecondary
colorPrimary
colorPrimaryDark
e.g.
<style name="FormFont" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#color/white</item>
<item name="android:textSize">#dimen/form_text_size</item>
<item name="colorControlNormal">#c5c5c5</item>
</style>
but nothing seems to work (it only changes color when EditText us focused like in above pic).
<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#c5c5c5</item>
<item name="colorControlActivated">#color/accent</item>
<item name="colorControlHighlight">#color/accent</item>
</style>
The most efficient thing to do is to add the colorAccent attribute in your AppTheme style like this:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">#color/colorAccent</item>
<item name="android:editTextStyle">#style/EditTextStyle</item>
</style>
<style name="EditTextStyle" parent="Widget.AppCompat.EditText"/>
The colorAccent attribute is used for widget tinting throughout the app and thus should be used for consistency
Try using android:backgroundTint="#yourUnderlineColor".
Only available for API 21 and higher.
To make it work in Android 6, create your custom theme in
res/values-v21/styles.xml .
<style name="FormFont" parent="#android:style/TextAppearance.Medium">
<item name="colorControlNormal">#c5c5c5</item>
</style>
<style name="FormFontWhite" parent="#android:style/TextAppearance.Medium">
<item name="colorControlNormal">#ffffff</item>
</style>
and apply then in your EditText
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/FormFont"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/FormFontWhite"/>
For more info check #reVerse answer .
Hope this helps!
<android.support.v7.widget.AppCompatEditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cux_label_password"
android:inputType="textPassword"
app:theme="#style/editTextStyle" />
<style name="editTextStyle" parent="TextAppearance.AppCompat">
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlActivated">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
</style>
Is there a way to change a style based on what theme is is set on an activity? for example, suppose my activity has a textview with a style set to TextViewStyle
<TextView
style="#style/TextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text"/>
This is my TextViewStyle
<style name="TextViewStyle" parent="android:style/Widget.TextView">
<item name="android:textStyle">bold</item>
<item name="android:textSize">8sp</item>
<item name="android:textColor">#android:color/holo_blue_dark</item>
</style>
In my Styles.xml I have two themes
<style name="AppThemeLight" parent="android:Theme.Material.Light">
</style>
<style name="AppThemeDark" parent="android:Theme.Material">
</style>
Now, when I have my theme set to Dark, I want this theme to override some values I set in my TextViewStyle. How could I do this? I tried the following, but It does not work
<!-- Override TextViewStyle style for AppThemeLight-->
<style name ="AppThemeLight.TextBlockStyle" parent="#style/TextBlockStyle">
<item name="android:textColor">#color/my_purple</item>
<item name="android:textSize">20sp</item>
</style>
<!-- Override TextViewStyle style for AppThemeDark-->
<style name ="AppThemeDark.TextBlockStyle" parent="#style/TextBlockStyle">
<item name="android:textColor">#color/my_green</item>
<item name="android:textSize">40sp</item>
</style>
Ok here is how I achieved this, 1 hour after posting the question! Might be helpful for someone else!
First in attrs.xml I defined the following :
<attr name="textBlockStyle" format="reference"/>
Then in the style.xml
<style name="TextBlockStyle" parent="android:style/Widget.TextView">
<item name="android:textStyle">bold</item>
<item name="android:textSize">8sp</item>
<item name="android:textColor">#android:color/holo_blue_dark</item>
</style>
<style name="AppThemeLight" parent="android:Theme.Material.Light">
<item name="android:windowNoTitle">true</item>
<item name="textBlockStyle">#style/AppThemeLightTextBlockStyle</item>
</style>
<style name="AppThemeDark" parent="android:Theme.Material">
<item name="android:windowNoTitle">true</item>
<item name="textBlockStyle">#style/AppThemeDarkTextBlockStyle</item>
</style>
<!-- Override TextBlockStyle style for AppThemeLight -->
<style name ="AppThemeLightTextBlockStyle" parent="#style/TextBlockStyle">
<item name="android:textColor">#color/my_purple</item>
<item name="android:textSize">20sp</item>
</style>
<!-- Override MyButton style for AppThemeDark -->
<style name ="AppThemeDarkTextBlockStyle" parent="#style/TextBlockStyle">
<item name="android:textColor">#color/my_green</item>
<item name="android:textSize">40sp</item>
</style>
Notice how I use the attribute I defined to reference a style
<item name="textBlockStyle">#style/AppThemeLightTextBlockStyle</item>
Finally, on my element I set the style like this "style="?textBlockStyle" :
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?textBlockStyle"
android:text="...."
android:gravity="center" />
When I set my activity theme to Dark or Light, the correct textButtenStyle will be used
Add a textStyle element to your activity style and make it equal to the desired style, then when the activity is loaded all textViews inside will have the style specified inside its theme unless you override it in your textView
Try something like
<item name="android:textAppearance">#style/yourTextViewStyle</item>
Inside your activity style
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>
I would like to change all color texts in my application.
So I wrote this code and I set my theme in the manifest:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:textColor">#color/white</item>
</style>
But only TextView texts are in white. I would like Button and EditText to be in white too.
Can anyone help me? Thank you in advance.
Your Theme:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:textColor">#color/white</item>
<item name="android:buttonStyle">#style/ButtonTheme</item>
</style>
And this is how you can set your Button Theme:
<style name="ButtonTheme" parent="android:style/Widget.Button">
<item name="android:textColor">#color/white</item>
</style>
And also do this with EditTexts etc.
I think most of the widget styles build off of these:
<item name="android:textColorPrimary">#android:color/primary_text_dark</item>
<item name="android:textColorSecondary">#android:color/secondary_text_dark</item>
<item name="android:textColorTertiary">#android:color/tertiary_text_dark</item>
<item name="android:textColorPrimaryInverse">#android:color/primary_text_light</item>
<item name="android:textColorSecondaryInverse">#android:color/secondary_text_light</item>
<item name="android:textColorTertiaryInverse">#android:color/tertiary_text_light</item>
<item name="android:textColorPrimaryDisableOnly">#android:color/primary_text_dark_disable_only</item>
<item name="android:textColorPrimaryInverseDisableOnly">#android:color/primary_text_light_disable_only</item>
<item name="android:textColorPrimaryNoDisable">#android:color/primary_text_dark_nodisable</item>
<item name="android:textColorSecondaryNoDisable">#android:color/secondary_text_dark_nodisable</item>
<item name="android:textColorPrimaryInverseNoDisable">#android:color/primary_text_light_nodisable</item>
<item name="android:textColorSecondaryInverseNoDisable">#android:color/secondary_text_light_nodisable</item>
<item name="android:textColorHint">#android:color/hint_foreground_dark</item>
<item name="android:textColorHintInverse">#android:color/hint_foreground_light</item>
<item name="android:textColorSearchUrl">#android:color/search_url_text</item>
Override the values as needed.
Assign color to button text
<Button android:textColor="#FFFFFF"></Button>
I am able to change the background of the navigation spinner:
<style name="MyTheme" parent="android:style/Theme.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionDropDownStyle">#style/MyDropDownNav</item>
</style>
<style name="MyDropDownNav" parent="android:style/Widget.Spinner">
<item name="android:background">#drawable/spinner_white</item>
<item name="android:textColor">#color/red</item>
</style>
Nevertheless the textColor is not changed. I also tried other ways to change the textColor:
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">?color_actionbar</item>
<item name="android:titleTextStyle">#style/myTheme.ActionBar.Text</item>
</style>
<style name="myTheme.ActionBar.Text" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/violet</item>
</style>
Any other idea?
I was looking for the same answer but didn't find anything, so I tried this solution. I works for me at least.
In the themes file, you can set the style for the spinnerDropdownItemStyle:
<style name="YourTheme" parent="YourParentTheme">
<item name="android:spinnerDropDownItemStyle">#style/YourCustomDropDownItemStyle</item>
</style>
Now, set the textappearance for your style:
<style name="YourCustomDropDownItemStyle" parent="#android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textAppearance">#style/YourCustomDropDownItemTextStyle</item>
</style>
And in your custom textappearance you can set the text details:
<style name="YourCustomDropDownItemTextStyle" parent="#android:style/Widget">
<item name="android:textColor">#color/white</item>
<!-- Here you can set the color and other text attributes -->
</style>
Hope this helps!