I have a layout with many TextViews and a dark background, so I want to change the text color of all the TextViews to something light.
Is there a way to do it, beyond going to each one and setting android:textColor='white'?
change font of all textviews is asking a similar question, but it's from 2014, and there is no real answer there.
I found the answer as I was typing up the question.
You want to set the appearence of the TextViews via a style resource, not manually. Then, have the style value inherit from a different one, representing the entire layout.
<TextView ...
android:textAppearance="#style/item_name"/>
<TextView ...
android:textAppearance="#style/item_details"/>
in style.xml:
<style name="LayoutStyle">
<item name="android:textColor">#C0C0C0</item>
</style>
<style name="item_name" parent="LayoutStyle">
<item name="android:textStyle">bold</item>
<item name="android:textSize">20sp</item>
</style>
<style name="item_details" parent="LayoutStyle">
<item name="android:textStyle">bold</item>
<item name="android:textSize">20sp</item>
</style>
From my experience text size and color cannot be set as a linear layout attribute.
But you can set the theme. So define the theme in styles.xml:
<style name="HeaderTitleTextViewTheme" parent="android:Widget.TextView">
<item name="android:textSize">22sp</item>
<item name="android:textColor">#color/colorSelectedItem</item>
</style>
and set it as the style of your layout:
<style name="ViewHeaderTitle">
<item name="android:theme">#style/HeaderTitleTextViewTheme</item>
<item name="android:layout_height">48sp</item>
<item name="android:paddingLeft">4dp</item>
<item name="android:paddingRight">4dp</item>
<item name="android:background">#FF5722</item>
<item name="android:gravity">center</item>
</style>
This will save you setting the style for every single TextView in the Layout
Related
How can i change the text color of all the button in my app ? right now the only think that work is to do :
<style name="MyTheme" parent="#android:style/Theme.Material.Light.NoActionBar">
<item name="android:textColor">#ff0000</item>
</style>
but i m a little worried to use android:textColor who seam to not only impact the button
I try this but it's didn't work :
<style name="BBButtonColor" parent="#android:style/Widget.Button">
<item name="android:textColor">#ff0000</item>
</style>
<style name="BBButtonStyle" parent="#android:style/Widget.Button">
<item name="android:textColor">#ff0000</item>
</style>
<style name="BBBorderlessButtonStyle" parent="#android:style/Widget.Button">
<item name="android:textColor">#ff0000</item>
</style>
<style name="MyTheme" parent="#android:style/Theme.Material.Light.NoActionBar">
<item name="android:textAppearanceButton">#style/BBButtonColor</item>
<item name="android:buttonStyle">#style/BBButtonStyle</item>
<item name="android:borderlessButtonStyle">#style/BBBorderlessButtonStyle</item>
<item name="android:colorButtonNormal">#ff0000</item>
</style>
It's mostly the color of button inside popup dialog that i want to change
Loki, you can try this. This should change the color of all buttons in your app.
<style name="BBButtonColor" parent="android:Widget.Button">
<item name="android:textColor">#FF0000</item>
</style>
You were trying to use #android:style/Widget.Button, instead use android:Widget.Button and check. Let me know if this works.
You can try this :
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:buttonStyle">#style/ButtonColor</item>
<item name="colorButtonNormal">#color/colorname</item>
</style>
<style name="ButtonColor" parent="#android:style/Widget.Button">
<item name="android:textColor">#color/colorname</item>
</style>
and/change this line in Manifest file :
android:theme="#style/AppTheme.Base
You should create new style for buttons, something like that:
<style name="optionsButton">
<item name="android:layout_width">70dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#color/white</item>
<item name="android:textColor">#color/black</item>
<item name="android:textSize">15sp</item>
</style>
Now you can set this style for every button you want in layout xml, for example:
<Button
android:id="#+id/payCashBtn"
style="#style/optionsButton"
android:text="Cash" />
You can see I set layout_width and height in style, so all my buttons which use this style will have same parameters, but you can set different values for every button, or create multiple styles if you used it more than once. I hope it will help you.
I am wanting to create a custom ToggleButton style, mainly so I can change the minimum height and text size depending on the screen size.
I have already done this for Button:
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="buttonStyle">#style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="Widget.AppCompat.Button">
<item name="android:minHeight">#dimen/button_min_height</item>
<item name="android:textSize">#dimen/button_text_size</item>
</style>
And I am now wanting to do a similar thing for ToggleButton. I have tried this...
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="buttonStyle">#style/MyButtonStyle</item>
<item name="android:buttonStyleToggle">#style/MyToggleButtonStyle</item>
</style>
<style name="MyToggleButtonStyle" parent="Widget.AppCompat.ToggleButton">
<item name="android:minHeight">#dimen/button_min_height</item>
<item name="android:textSize">#dimen/button_text_size</item>
</style>
...but Widget.AppCompat.ToggleButton doesn't exist. So what changes do I need to make so I can control the minimum height and text size of my ToggleButtons like I already do for Buttons?
I would prefer an approach similar to the above, not only for consistency but so I don't have to go through all my layout files and update the ToggleButtons' xml individually.
This works for me.
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="buttonStyle">#style/MyButtonStyle</item>
<item name="android:buttonStyleToggle">#style/MyToggleButtonStyle</item>
</style>
<style name="MyToggleButtonStyle" parent="android:Widget.Button.Toggle">
<item name="android:minHeight">#dimen/button_min_height</item>
<item name="android:textSize">#dimen/button_text_size</item>
</style>
I want to make my app to have several styles.
The style changes button style, textview color, and layout background color.
I have my button style in xml files.
And this is my code: style.xml (v21)
<style name="ThemeIndigo" parent="#android:style/Theme.Material.NoActionBar">
<item name="android:colorPrimary">#color/indigo</item>
<item name="android:colorPrimaryDark">#color/indigo</item>
<item name="android:navigationBarColor">#color/indigo_dark</item>
<item name="android:buttonStyle">#drawable/indigo_button</item>
<item name="android:background">#drawable/indigo_button</item>
</style>
This changes background color of all things on this layout.
How do I make my style to only change color of the buttons?
Please help. Thanks.
(Sorry for the bad english ;)
Try this style to change for just buttons in the app
<style name="ThemeIndigo" parent="#android:style/Theme.Material.NoActionBar">
<item name="android:buttonStyle">#style/MyButton</item>
</style>
<style name="MyButton" parent="android:Widget.Material.Button">
<item name="android:textSize">19sp</item>
<item name="android:layout_margin">0dip</item>
<item name="android:background">#ff0000</item>
</style>
If I change the background of a child style, then it doesn't apply any other styles from the parent!
<style name="RemoteButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:textColor">#556699</item>
<item name="android:textSize">20dp</item>
<item name="android:layout_margin">3dp</item>
</style>
<style name="ControlButton" parent="RemoteButton">
<!-- THIS CAUSES IT TO IGNORE ALL THE PARENT STYLES SUCH AS MARGIN -->
<item name="android:background">#E87E04</item>
<item name="android:textColor">#ffffff</item>
</style>
And this doesn't work (it's ignored):
<item name="android:colorBackground">#E87E04</item>
How do I give the child style a different background color?
When you are creating a custom style or theme, and you inherit from it, DO NOT use the parent attribute, instead of this
<style name="ControlButton" parent="RemoteButton">
<!-- THIS CAUSES IT TO IGNORE ALL THE PARENT STYLES SUCH AS MARGIN -->
<item name="android:background">#E87E04</item>
<item name="android:textColor">#ffffff</item>
</style>
Do this, Notice the "." character.
<style name="RemoteButton.ControlButton">
<!-- THIS CAUSES IT TO IGNORE ALL THE PARENT STYLES SUCH AS MARGIN -->
<item name="android:background">#E87E04</item>
<item name="android:textColor">#ffffff</item>
</style>
You just keep using the "." character whenever you need to inherit your custom style.
How do I avoid duplicate style items in the below example?
I have textSize 30sp with the style - TextStyle.
<style name="TextStyle">
<item name="android:textSize">30sp</item>
</style>
The same textSize 30sp I am using in the below style. Is there any method apply the textsize- without duplicate writing of the style?
<style name="bottomText">
<item name="android:textSize">30sp</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">150dp</item>
</style>
Just let one Style inherit from the other:
<style name="bottomText" parent="TextStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">150dp</item>
</style>
the style "bottomText" then has all attributes defined in "TextStyle", but can still be overwritten within bottomText.
<style name="TextStyle">
<item name="android:textSize">30sp</item>
</style>
<style name="bottomText" parent="#style/TextStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">150dp</item>
</style>
<style name="boldText" parent="#style/bottomText">
<item name="android:textStyle">bold</item>
</style>
Another approach:
<style name="bottomText.BoldText">
<item name="android:textStyle">bold</item>
</style>
In last two cases boldText inherits from bottomText which defines android:layout_width and android:layout_height
<style name="TextStyle">
<item name="android:textSize">30sp</item>
</style>
<style name="bottomText" parent="#style/TextStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">150dp</item>
</style>
let style of bootomText Inheritance the TextStyle
http://developer.android.com/guide/topics/ui/themes.html#DefiningStyles
From what I can see, you don't need that second style since you need to set a layout_width and layout_height in the xml. You can just set that height and width when you create your layout and just use the style="#styles/TextStyle". If there's a reason you can't do it this way, please explain the problem a little better