Home > Enterprise >  How to apply letter spacing in custom button?
How to apply letter spacing in custom button?

Time:11-16

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

CodePudding user response:

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 a Custom View for Textview. Make the,values Make entries of all fonts in strings.xml

CodePudding user response:

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