Amrita Thakur
Amrita Thakur

Reputation: 23

How to set padding and text type face using style.xml in android

 val textView = TextView(context)
        textView.text = "This is my message for title text  "
        context?.let {
            textView.setPadding(
                convertDpToPx(it, 24f),
                convertDpToPx(it, 24f),
                convertDpToPx(it, 24f),
                0
            )
            textView.setTypeface(textView.typeface, Typeface.BOLD);
        }

private fun convertDpToPx(context: Context, dp: Float): Int {
        return (dp * context.resources.displayMetrics.density).toInt()
    }
        

I want to be set padding using a style that I need to define in style.xml please suggest it possible that can we set padding and typeface bold style and we can read that style in class

Upvotes: 0

Views: 49

Answers (1)

che10
che10

Reputation: 2308

Assuming you are setting it to a TextView, you can simply create a style like this

<style name="TextViewStyle" parent="Widget.AppCompat.TextView">
        <item name="android:paddingTop">24dp</item>
        <item name="android:paddingBottom">24dp</item>
        <item name="android:paddingEnd">24dp</item>
        <item name="android:paddingStart">24dp</item>
        <item name="android:textStyle">bold</item>
    </style>

And assign it as easily as this

style="@style/TextViewStyle"

Edit: Since you do not have a TextView in XML as such, this is the only way beside that

val textView = TextView(yourContext, null, 0, R.style.your_style);

Upvotes: 1

Related Questions