set a custom font for a button text in Xamarin Android - android

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>

Related

How to apply letter spacing in custom button?

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"
/>

Android override predefined parent style

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" />

Android Button Theme not applying

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>

How to change style of all the buttons in android studio?

Style.xml File(AppTheme is the theme of my app and it is inheriting materiallightdarkactionbar theme)
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorForeground">#color/holo_orange_light</item>
<item name="android:buttonStyle">#style/newbuttonstyle</item>
</style>
<style name="newbuttonstyle" parent="#android:style/Widget.Button">
<item name="android:backgroundTint">#acd9e6</item>
</style>
</resources>
I am designing a calculator and this is the keypad for it.....rather than individually setting background tint for all the buttons I want to change the style of all my buttons in one go using style.xml....can i do so?
In this I am using buttonStyle and still it is not working
<item name="android:buttonStyle">#style/newbuttonstyle</item>
in your XML file
<Button
style="#style/MyButtonStyle"
android:id="#+id/btnSignIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="my button"/>
in style.xml file add this code.
<style name="ButtonStyle" parent="#android:style/Widget.Button">
<item name="android:layout_width">fill_parent</item>
<item name="android:textColor">#color/black</item>
<item name="android:textSize">15sp</item>
<item name="android:padding">10dp</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#color/colorPrimary</item>
<item name="android:gravity">center</item>
</style>
change Style items as per your requirements.
<style name="newbuttonstyle" parent="#android:style/Widget.Button">
<item name="android:background">#acd9e6</item>
</style>
I am using above code in my application and its working fine.i think set background instead of backgroundTint.
May be its working .
check this link:
ReferenceLink
and its some issue in tintbackground in lolipop.so you have to check in below lolipop.
You can follow this question, hope it will be work for you and clear your style portion:
Android Material Design Button Styles

Can't inherit button color on style

I am trying to define button properties on style.
My code is
<style name="button">
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:textAllCaps">false</item>
<item name="colorButtonNormal">#f37022</item>
<item name="android:textColor">#ffffff</item>
</style>
And using as
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Prepaid Package"
style="#style/button"/>
But the color of the button's normal state is not changing.
This button style is only for some buttons. Not for all butons.
What to do now?
edit after question clarification
Save you style.xml in res/values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_button" parent="#android:style/Widget.Button">
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:textAllCaps">false</item>
<item name="colorButtonNormal">#f37022</item>
<item name="android:textColor">#ffffff</item>
</style>
Apply it like so:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Prepaid Package"
style="#style/custom_button"/>
first answer
You need to inherit the parent style.
<style name="Button" parent="#android:style/Widget.Button">
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:textAllCaps">false</item>
<item name="colorButtonNormal">#f37022</item>
<item name="android:textColor">#ffffff</item>
</style>
That way all your Button's that are not styled will have your custom style. i.e. no need to put in the style value.
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Prepaid Package"/>

Categories

Resources