Using A Custom Attribute That Is In A Style With A View - android

Lets say I have a custom attribute in a few styles like so:
<style name="Theme1" parent="android:Theme.Holo">
<item name="textViewBackground">#000022</item>
<item name="android:background">#000000</item>
</style>
<style name="Theme2" parent="android:Theme.Holo">
<item name="textViewBackground">#AA2200</item>
<item name="android:background">#000000</item>
</style>
Can I then reference that in a standard view like a TextView? Something like:
<TextView
android:id="#+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#CurrentTheme.textViewBackground"
android:text="Number" />

You need to define a TextView style, like this:
<style name="Theme1" parent="android:Theme.Holo">
<item name="android:textViewStyle">#style/TextViewStyle1</item>
<item name="android:background">#000000</item>
</style>
<style name="Theme2" parent="android:Theme.Holo">
<item name="android:textViewStyle">#style/TextViewStyle2</item>
<item name="android:background">#000000</item>
</style>
<style name="TextViewStyle1" parent="#android:style/Widget.TextView">
<item name="android:background">#000022</item>
</style>
<style name="TextViewStyle2" parent="#android:style/Widget.TextView">
<item name="android:background">#AA2200</item>
</style>
Then you won't even need to set the android:background attribute on each TextView in your xml, it will apply to all TextViews that do not specify a different background.

Related

Android: how to set spinnerItemStyle, spinnerDropDownItemStyle to an individual Spinner in XML?

This code allows to set application-wide spinnerItemStyle and spinnerDropDownItemStyle.
<application
...
android:theme="#style/AppTheme" >
<style name="AppTheme" parent="#android:style/Theme">
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">#style/SpinnerItem.DropDownItem</item>
</style>
<style name="SpinnerItem" parent="#android:style/Widget.TextView.SpinnerItem">
<item name="android:textColor">#00FF00</item>
</style>
<style name="SpinnerItem.DropDownItem" parent="#android:style/Widget.DropDownItem.Spinner">
<item name="android:textColor">#FF0000</item>
</style>
But when I try to apply a style to just one Spinner in XML layout file, nothing happens:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/SpinnerColoredText"/>
<style name="SpinnerColoredText">
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">#style/SpinnerItem.DropDownItem</item>
</style>
How can I set spinnerItemStyle without changing application-wide Theme?

How can I change font weight in my project which has a default fontFamily for all textviews?

I have a project and in this project I have defined fontFamily to all textviews in the style of AppTheme.
The fontFamily is montserrat but in text views I use montserrat_semibold, montserrat_regular and montserrat_medium.
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:textViewStyle">#style/MontserratTextViewStyle</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="MontserratTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">#font/montserrat</item>
</style>
How can I change their weight in the different XMLs of textviews?
Hmm the only way I can think of doing that is to create multiple styles in your styles.xml:
<style name="MontserratTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">#font/montserrat</item>
</style>
<style name="MontserratTextViewSemiBoldStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">#font/montserrat_semibold</item>
</style>
<style name="MontserratTextViewRegularStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">#font/montserrat_regualr</item>
</style>
<style name="MontserratTextViewMediumStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">#font/montserrat_medium</item>
</style>
Then for any TextView which doesn't use the default montserrat i.e. maybe you want montserrat_medium:
<TextView
style="#style/MontserratTextViewMediumStyle"
....
/>

Android - Overriding a Style per Theme

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

Several themes for view's custom styles

I have one app theme and many custom styles for different View.
For example, snippet of code:
<!-- styles.xml -->
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light" />
<style name="title">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">22sp</item>
<item name="android:padding">10sp</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:textColor">#color/black</item>
<item name="android:background">#color/background_all_screen</item>
</style>
<style name="label">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#color/black</item>
<item name="android:textSize">18sp</item>
<item name="android:layout_alignParentLeft">true</item>
<item name="android:layout_marginLeft">5dp</item>
</style>
<style name="button">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">2dp</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">#color/white</item>
<item name="android:background">#color/blue</item>
</style>
And now I want make many color themes for application. This is mean, that in different color themes custom View was in different colors.
For example, in one color theme button is blue, in another - red.
How can I implement this resources for easy change of theme?
There is a useful tutorial, but what about the custom styles for elements?
UPDATE:
I try it, but this is nor work:
<style name="Button.MyButton" parent="android:style/Widget.Button">
<item name="android:background">#drawable/shape</item>
</style>
<style name ="Button.MyButton.Theme1">
<item name="android:textColor">#000000</item>
</style>
<style name ="Button.MyButton.Theme2">
<item name="android:textColor">#FFFFFF</item>
</style>
<Button
android:id="#+id/save_button"
android:layout_width="0px"
style="#style/Button.MyButton"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/save"/>
This is answer. I just use attributes and after that I can use different colors in different themes.
<!-- values/attributes.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myCoolColor" format="color" />
</resources>
<!-- styles.xml, define myCoolColor for each theme -->
<style name="Theme.MyApp" parent="#style/Theme.Light">
<item name="myCoolColor">#123456</item>
</style>
<style name="Theme.MyApp.Dark" parent="#style/Theme.Dark">
<item name="myCoolColor">#654321</item>
</style>

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>

Categories

Resources