styles.xml
<style name="Red" parent="android:TextAppearance.DeviceDefault.Medium.Inverse">
<item name="android:textColor">#f00</item>
</style>
<style name="Red.LARGE">
<item name="android:textSize">30sp</item>
</style>
When i use this;
<Button
android:id="#+id/save"
android:text="#string/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/save_marginTop"
android:gravity="center"
style="#style/Red.LARGE"/>
This button attribute only Large and doest painting to Red. Why?
Your Red.LARGE style needs to use the parent attribute. Red.LARGE will inherit Red if it is changed to:
<style name="Red.LARGE" parent="Red">
<item name="android:textSize">30sp</item>
</style>
Related
I have one button and letterspacing is not working with it. Please check my following snippet code
<style name="ButtonText_v2" parent="TextAppearance.AppCompat">
<item name="fontFamily">#font/silka_regular</item>
<item name="android:letterSpacing">0.1</item>
<item name="android:textSize">#dimen/dimen_14dp</item>
</style>
<style name="PrimaryButton_v2" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/button_selector_primary</item>
<item name="android:textColor">#color/white</item>
<item name="textAllCaps">true</item>
<item name="textAppearance">#style/ButtonText_v2</item>
</style>
layout.xml File
<Button
android:text="Button"
android:layout_height="56dp"
android:layout_width="200dp"
style="#style/PrimaryButton_v2"
android:layout_margin="15dp"
/>
You can't apply letter spacing in button/custom button because their is some limitation in button component in which you can't make text appearance and background customization and more limitation
the solution for this is instead of button you can use text view and make custom text view it will also work similar to button and customize it according to requirement.
for example :-
https://stackoverflow.com/questions/9477336/how-to-make-a-custom-textview#:~:text=Create%20a%20Custom%20View%20for%20Textview.%20Make%20the,values%20Make%20entries%20of%20all%20fonts%20in%20strings.xml
Add android:letterSpacing in PrimaryButton_v2 it will work perfectly.
<style name="ButtonText_v2" parent="TextAppearance.AppCompat">
<item name="fontFamily">#font/silka_regular</item>
<item name="android:textSize">#dimen/dimen_14dp</item>
</style>
<style name="PrimaryButton_v2" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/button_selector_primary</item>
<item name="android:textColor">#color/white</item>
<item name="textAllCaps">true</item>
<item name="textAppearance">#style/ButtonText_v2</item>
<item name="android:letterSpacing">0.1</item>
</style>
layout.xml File
<Button
android:text="Button"
android:layout_height="56dp"
android:layout_width="200dp"
style="#style/PrimaryButton_v2"
android:layout_margin="15dp"
/>
I use one of the predefined styles for MaterialButton.
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
And this is the style predefined style which i used.
</style>
<style name="Widget.MaterialComponents.Button.OutlinedButton" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:paddingLeft">#dimen/mtrl_btn_padding_left</item>
<item name="android:paddingRight">#dimen/mtrl_btn_padding_right</item>
<item name="strokeColor">#color/mtrl_btn_stroke_color_selector</item>
<item name="strokeWidth">#dimen/mtrl_btn_stroke_size</item>
</style>
And this is parent style of Widget.MaterialComponents.Button.OutlinedButton
<style name="Widget.MaterialComponents.Button.TextButton" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:textColor">#color/mtrl_text_btn_text_color_selector</item>
<item name="android:paddingLeft">#dimen/mtrl_btn_text_btn_padding_left</item>
<item name="android:paddingRight">#dimen/mtrl_btn_text_btn_padding_right</item>
<item name="iconTint">#color/mtrl_text_btn_text_color_selector</item>
<item name="iconPadding">#dimen/mtrl_btn_text_btn_icon_padding</item>
<item name="backgroundTint">#color/mtrl_btn_text_btn_bg_color_selector</item>
<item name="rippleColor">#color/mtrl_btn_text_btn_ripple_color</item>
</style>
Now i tried to override one of the attributes of Widget.MaterialComponents.Button.TextButton in my styles.xml
<style name="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">#color/colorAccent</item>
</style>
I wonder whether this is the right approach or not to override a predefined style and will style Widget.MaterialComponents.Button.OutlinedButton use #color/colorAccent as text color. I think its working but i can not be sure is there any side effect of this approach or is there a better way ?
You can use one of the following:
<style name="MyCustomStyle" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:textColor">#color/colorAccent</item>
</style>
<style name="Widget.MaterialComponents.Button.OutlinedButton.MyCustomStyle">
<item name="android:textColor">#color/colorAccent</item>
</style>
With these approach, you would inherit all the properties of your parent style and you would be declare only those properties that you like to change/override.
And use this style in your layout file as follow:
<com.google.android.material.button.MaterialButton
style="#style/MyCustomStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
or
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton.MyCustomStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
I created really simple view. I would like to create default layout for my button and import it via AppTheme, which i added in manifest file.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="buttonStyle">#style/ButtonStyle</item>
<item name="android:buttonStyle">#style/ButtonStyle</item>
<item name="android:button">#style/ButtonStyle</item>
</style>
<style name="ButtonStyle" >
<item name="android:textSize">20dp</item>
<item name="android:gravity">center</item>
<item name="android:layout_width">150dp</item>
<item name="android:layout_gravity">center</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:background">#drawable/defaultbutton</item>
</style>
So what I expect is to have all buttons in my app be styled which this settings. Only way i can accomplishe it is to import style inside my button using
style="#style/ButtonStyle"
Button 'Start Test' is styled via inside import.
Funny thing is that some styles are imported for buttons 'Settings' and 'Check Sensors' ( like drawable/deefaultbutton or centered text)
<Button
style="#style/ButtonStyle"
android:id="#+id/test"
android:layout_height="50dp"
android:text="Start Test" />
<Button
android:id="#+id/settings"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="Settings" />
<Button
android:id="#+id/checkSensors"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="Check Sensors" />
My question is, why is this happening and i can't set it as default without importing it via 'Theme' ?
Tried this and read this but it doesn't work for me.
Custom style should extend Widget styles to use with buttonStyle attribute.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="buttonStyle">#style/Widget.AppCompat.Button.ButtonStyle</item>
</style>
<style name="Widget.AppCompat.Button.ButtonStyle">
<item name="android:textSize">20dp</item>
<item name="android:gravity">center</item>
<item name="android:layout_width">150dp</item>
<item name="android:layout_gravity">center</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:background">#drawable/defaultbutton</item>
</style>
I have a button and I want to set a custom font to the text but I have trouble doing that.
The code is the following :
In .xml file :
<Button
android:id="#+id/button"
style="#style/ButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Sample Text"/>
and in styles.xml
<style name="ButtonStyle" parent="#style/Widget.AppCompat.Button.Borderless">
<item name="android:textSize">#dimen/button_text_size</item>
<item name="android:background">#drawable/button_background</item>
<item name="android:textAppearanceButton">#style/ButtonTextStyle</item>
<item name="android:textColor">?android:textColorPrimaryInverse</item>
</style>
<style name="ButtonTextStyle" parent="TextAppearance.AppCompat">
<item name="android:fontFamily">#font/my_custom_font</item>
</style>
Instead of of creating a different style fontFamily should be used in the button style and the code should look like this.
<style name="ButtonStyle" parent="#style/Widget.AppCompat.Button.Borderless">
<item name="android:textSize">#dimen/button_text_size</item>
<item name="android:background">#drawable/button_background</item>
<item name="android:fontFamily">#font/my_custom_font</item>
<item name="android:textColor">?android:textColorPrimaryInverse</item>
</style>
I have so specific problem. I'm applying theme for all textviews. It appears on preview but when I run this app on phone I face with unstyled textview. The styles I set for other views are working. only textviewstyle is not working.
In this sample textview color is green in preview but when i run it on phone its black. Why?
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textViewStyle">#style/myTextViewStyle</item>
<item name="android:buttonStyle">#style/myButtonStyle</item>
<item name="android:editTextStyle">#style/myEditTextStyle</item>
<!-- Customize your theme here. -->
</style>
<style name="myTextViewStyle" parent="#android:style/Widget.TextView">
<item name="android:textColor">#00ff00</item>
<item name="android:textSize">24sp</item>
</style>
<style name="myButtonStyle" parent="#android:style/Widget.Button">
<item name="android:textColor">#00ff00</item>
<item name="android:textSize">24sp</item>
</style>
<style name="myEditTextStyle" parent="#android:style/Widget.EditText">
<item name="android:textColor">#00ff00</item>
<item name="android:textSize">24sp</item>
</style>
activity_main.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="edit text" />
"textViewStyle" is not defined in v22.1.1. You can find more detail from the link:
https://code.google.com/p/android/issues/detail?id=170476