How to avoid a duplicate style item? - android

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

Related

How to change to text color of all the button in my app?

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.

How to inherit system defined style in my style file in android?

I customize a style named MyDialogWindowTitle in my style file, I hope this style inherited from system style Widget.TextView.ListSeparator,
butthe code parent="#style/Widget.TextView.ListSeparator" cause an error, how can I do ? Thanks!
<style name="MyDialogWindowTitle" parent="?android: style/Widget.TextView.ListSeparator">
<item name="android:textSize">30sp</item>
</style>
<style name="Widget.TextView.ListSeparator">
<item name="android:background">#android:drawable/dark_header_dither</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">25dip</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">?textColorSecondary</item>
<item name="android:textSize">14sp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:paddingLeft">5sp</item>
</style>
Instead of parent="?android: style/Widget.TextView.ListSeparator", use parent="#style/Widget.TextView.ListSeparator"
This is because Widget.TextView.ListSeparator is defined by you and is not defined in android's libraries

Android Style Inheritence

My app has text that can be either black or white; small, medium, or large; and bold or not bold. I can't figure out a way to make styles for covering all bases without resorting to just including a ton of styles and repeating everything. Is there a way to reduce this somehow or apply more than one at a time?
<style name="Text">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#000000</item>
<item name="android:textStyle">normal</item>
</style>
<style name="Text.Small">
<item name="android:textSize">8sp</item>
</style>
<style name="Text.Large">
<item name="android:textSize">18sp</item>
</style>
<style name="Text.Small.Bold">
<item name="android:textStyle">bold</item>
</style>
<style name="Text.Large.Bold">
<item name="android:textStyle">bold</item>
</style>
<style name="Text.White">
<item name="android:textColor">#ffffff</item>
</style>
<style name="Text.White.Small">
<item name="android:textSize">8sp</item>
</style>
<style name="Text.White.Large">
<item name="android:textSize">18sp</item>
</style>
<style name="Text.White.Small.Bold">
<item name="android:textStyle">bold</item>
</style>
<style name="Text.White.Large.Bold">
<item name="android:textStyle">bold</item>
</style>
Half of these are literally duplicates that inherit from a different parent style. I feel like this would be pretty trivial to accomplish in CSS without repeating everything like that. Am I stuck doing it this way in Android? Or is there something I am overlooking here?
To avoid code duplication you could declare the item only once, and refer it from the others styles using the parent attribute.
Example using the code you provided :
<style name="Text.Small.Bold">
<item name="android:textStyle">bold</item>
</style>
<style name="Text.Large.Bold">
<item name="android:textStyle">bold</item>
</style>
could be changed in :
<style name="Text.Small.Bold">
<item name="android:textStyle">bold</item>
</style>
<style name="Text.Large.Bold" parent="Text.Small.Bold" />
I have dug but there doesn't seem to be a way to do this native to Android. I am just going to leave it with the duplicated parts in each style and suffice it to say that this is the DRYest it can be in this context.

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>

How to hide the element style in child style element?

I need to 'hide' the textSize parameter in BigButton style, inherited from Button. How to do so?
<style name="Button">
<item name="android:background">#color/button_background</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:padding">5dp</item>
<item name="android:textSize">#dimen/button_text_size</item>
</style>
<style name="BigButton" parent="#style/Button">
<item name="android:textSize"></item>
</style>
Try
<style name="BigButton" parent="#style/Button">
<item name="android:textSize">#null</item>
</style>

Categories

Resources