<style name="button" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:gravity">center</item>
<item name="android:layout_height">#dimen/button_height</item>
<item name="android:layout_width">wrap_content</item>
<item name="cornerRadius">#dimen/button_round_radius</item>
</style>
<style name="button.wide" parent="button">
<item name="android:fontFamily">#font/montserrat_regular</item>
<item name="android:layout_width">match_parent</item>
I do this in style file. but when I apply the style in layout. style="#style/button.wide"
the font doesn't work.
try using android:theme="#style/button.wide"
Change button style name and parent like, name="ButtonViewStyle"
and parent="android:Widget.Button"
<style name="ButtonViewStyle" parent="android:Widget.Button">
<item name="android:fontFamily">#font/montserrat_regular</item>
<item name="android:layout_width">match_parent</item>
</style>
Use <item name="android:buttonStyle">#style/ButtonViewStyle</item> line to your parent style
<style name="button" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:gravity">center</item>
<item name="android:layout_height">#dimen/button_height</item>
<item name="android:layout_width">wrap_content</item>
<item name="cornerRadius">#dimen/button_round_radius</item>
<item name="android:buttonStyle">#style/ButtonViewStyle</item>
</style>
Related
We are using material components 1.0.0 and wanted to alter the style of MaterialButton across the app. We created our own styles extending the ones from components library. Right now we have something like:
<resources>
<style name="MyApp.Button" parent="Widget.MaterialComponents.Button">
<item name="android:minHeight">#dimen/action_button_min_height</item>
<item name="android:minWidth">#dimen/action_button_min_width</item>
<item name="android:fontFamily">#fonts/customFont</item>
<item name="android:singleLine">true</item>
<item name="android:textAllCaps">false</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">8dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
</style>
<style name="MyApp.Button.Unelevated" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:minHeight">#dimen/action_button_min_height</item>
<item name="android:minWidth">#dimen/action_button_min_width</item>
<item name="android:fontFamily">#fonts/customFont</item>
<item name="android:singleLine">true</item>
<item name="android:textAllCaps">false</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">8dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
</style>
<style name="MyApp.Button.Text" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:minHeight">#dimen/action_button_min_height</item>
<item name="android:minWidth">#dimen/action_button_min_width</item>
<item name="android:fontFamily">#fonts/customFont</item>
<item name="android:singleLine">true</item>
<item name="android:textAllCaps">false</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">8dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
</style>
<style name="MyApp.Button.Outlined" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:minHeight">#dimen/action_button_min_height</item>
<item name="android:minWidth">#dimen/action_button_min_width</item>
<item name="android:fontFamily">#fonts/customFont</item>
<item name="android:singleLine">true</item>
<item name="android:textAllCaps">false</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">8dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
</style>
</resources>
Is there a way to avoid defining same values for every style?
Since you are using different parent styles you can only semplify some attributes, but not all.
For example you can define:
<style name="MyButtonTextAppearance" parent="#style/TextAppearance.MaterialComponents.Button>
<item name="fontFamily">.....</item>
<item name="android:fontFamily">...</item>
<item name="android:textStyle">...</item>
<item name="android:textAllCaps">...</item>
<item name="android:textSize">...</item>
<item name="android:letterSpacing">...</item>
</style>
And in your styles you can remove the same attributes using:
<style name="MyApp.Button" parent="Widget.MaterialComponents.Button">
....
<item name="android:textAppearance">#style/MyButtonTextAppearance</item>
</style>
What parent style should I use if I want to set global textViewStyle and maintain backward compatibility with AppCompat? I can't find something like Widget.AppCompat.TextView :
<style name="Base_AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textViewStyle">#style/GlobalTextViewStyle</item>
</style>
<style name="GlobalTextViewStyle" parent="?????">
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Medium</item>
</style>
You just need to extend TextAppearance.AppCompat style overriding necessary values:
<style name="Base_AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textAppearance">#style/MyTextAppearance</item>
</style>
<style name="MyTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">?android:textColorPrimary</item>
<item name="android:textColorHint">?android:textColorHint</item>
<item name="android:textColorHighlight">?android:textColorHighlight</item>
<item name="android:textColorLink">?android:textColorLink</item>
<item name="android:textSize">#dimen/abc_text_size_body_1_material</item>
<item name="textColor">?textColorPrimary</item>
<item name="textColorHighlight">?textColorHighlight</item>
<item name="textColorHint">?textColorHint</item>
<item name="textColorLink">?textColorLink</item>
<item name="textSize">16sp</item>
<item name="textStyle">normal</item>
</style>
If you want to set this style for all your TextView's in your app, you can do something like this:
<resources>
<style name="YourTheme" parent="android:YourParentThTheme">
<item name="android:textViewStyle">#style/GlobalTextViewStyle </item>
</style>
<style name="GlobalTextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">#FF000</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
<style name="normal_text_white" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#color/white_color</item>
<item name="android:textSize">#dimen/small_text_size</item>
<item name="android:textColorHint">#color/hint_color</item>
</style>
We can try this as well for AppCompat widget.
<style name="TitleStyle" parent="TextAppearance.AppCompat">
<item name="android:gravity">center_horizontal</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:textSize">26sp</item>
</style>
I am useing the following xml to syle my showcaseview, but for some reason the button is darker then the original image, any idea?
<style name="ShowcaseButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:paddingLeft">20dp</item>
<item name="android:paddingRight">20dp</item>
<item name="android:textColor">#000000</item>
<item name="android:background">#drawable/continueknop</item>
</style>
<style name="CustomShowcaseTheme" parent="ShowcaseView">
<item name="sv_backgroundColor">#AA010101</item>
<item name="sv_showcaseColor">#color/actionbar</item>
<item name="sv_buttonText">Continue</item>
<item name="sv_titleTextAppearance">#style/CustomTitle</item>
<item name="sv_detailTextAppearance">#style/Customdesc</item>
</style>
<style name="CustomTitle" parent="TextAppearance.ShowcaseView.Title">
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="Customdesc" parent="TextAppearance.ShowcaseView.Title">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">18sp</item>
</style>
Does anyone have an idea how to add the style "MarginButton" to the style "bluebutton" or "OrangeButton"? The reason I'm doing this is to prevent double code, is this the correct way to do or are there better ways to solve this?
<style name="OrangeButton" parent="android:Widget.Button">
<item name="android:textColor">#000</item>
<item name="android:background">#drawable/roundedbutton_orange</item>
</style>
<style name="BlueButton" parent="android:Widget.Button">
<item name="android:textColor">#FFF</item>
<item name="android:background">#drawable/roundedbutton_blue</item>
</style>
<style name="MarginButton" parent="android:Widget.Button">
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:layout_marginLeft">3dp</item>
<item name="android:layout_marginRight">3dp</item>
</style>
Thnx,
Joris
Update
Set your orange button parent to #style/MarginButton. I've just tried this and it's working,
<style name="orangeButton" parent="#style/MarginButton">
<item name="android:textColor">#000</item>
<item name="android:background">#drawable/roundedbutton_orange</item>
</style>
<style name="MarginButton" parent="#android:style/Widget.Button">
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:layout_marginLeft">3dp</item>
<item name="android:layout_marginRight">3dp</item>
</style>
I think that you can obtain it by doing:
Buttons.xml ->
<style name="OrangeButton" parent="MarginButton">
<item name="android:textColor">#000</item>
<item name="android:background">#drawable/roundedbutton_orange</item>
</style>
<style name="BlueButton" parent="MarginButton">
<item name="android:textColor">#FFF</item>
<item name="android:background">#drawable/roundedbutton_blue</item>
</style>
MarginButton.xml ->
<style name="MarginButton" parent="android:Widget.Button">
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:layout_marginLeft">3dp</item>
<item name="android:layout_marginRight">3dp</item>
</style>
If you guys want to use borderless button like me or use two style. Try it.
View ->
<com.google.android.material.button.MaterialButton
android:id="#+id/btnCreateNewAcc"
android:text="#string/or_create_a_new_account"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4"
style="#style/textButtonStyle"
/>
Style ->
<style name="textButtonStyle" parent="android:Widget.Material.Button.Borderless">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:minWidth">0dp</item>
<item name="android:minHeight">0dp</item>
<item name="android:padding">0dp</item>
<item name="android:fontFamily">#font/inter</item>
<item name="android:textAllCaps">false</item>
<item name="android:textColor">#color/grey</item>
<item name="android:textFontWeight">400</item>
<item name="android:textSize">15sp</item>
<item name="android:background">#android:color/transparent</item>
</style>
You can do it changing the parent attribute. For example:
<style name="BlueButton" parent="MarginButton">
<item name="android:textColor">#FFF</item>
<item name="android:background">#drawable/roundedbutton_blue</item>
</style>
How can I give bold and normal styles inside our styles.xml?
The code which I have given is:
<style name="textbold" parent="#android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textstyle">bold</item>
</style>
<style name="textregular" parent="#android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textstyle">normal</item>
</style>
But its showing error over here: <item name="android:textstyle">
That's because it's textStyle, not textstyle. The attributes are case-sensitive.
<item name="android:textStyle">normal</item>