@drawable/bg_button" /> @drawable/bg_button" /> @drawable/bg_button"/>

Android. Custom font not working when setting from style

632 Views Asked by At

I'm trying to set programatically style for my button. Here is my style:

   <style name="MainButton">
        <item name="android:background">@drawable/bg_button</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">70dp</item>
        <item name="android:padding">10dp</item>      
        <item name="android:textColor">@color/red</item>
        <item name="android:textSize">30sp</item>
        <item name="fontPath">fonts/Semibold.ttf</item>    

 
    </style>

And then in a fragment I try to do the following:

      val button = Button(ContextThemeWrapper(context, R.style.MainButton), null, R.style.MainButton).apply {
                    this.text = text
                }
llView.addView(button)

The button is added, but some of the settings specified in the style are not applied to it. For example, button height works like wrap_content, but expected 70 dp , no font is applied. However, if I use this style in .xml everything works well. Please, help me

P.S. I need to dynamically add buttons to view groups. Perhaps it is better to create a button in .xml with custom style and inflate it inside my code ? I need to dynamically add buttons on different screens, so I decided that it would be better to move the logic of creating buttons into a separate class, maybe it should be replaced with inflate, please help me.

0

There are 0 best solutions below