How do we add styles programmatically?

I wrote one style for TextView, and I set it for my TextViewhow style="@style/popup_window_text_style", working fine, no problem.

Now I want the same style to be executed programmatically for my text view

TextView textView = new TextView(mContext);

Is there a way to do this programmatically in Java?

My style in styles.xml

<style name="popup_window_text_style">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:minWidth">60dp</item>
        <item name="android:padding">10dp</item>
        <item name="android:layout_marginLeft">1dp</item>
        <item name="android:layout_marginRight">1dp</item>
        <item name="android:background">@drawable/ctxmenu_btn_selector</item>
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">16sp</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:singleLine">true</item>
    </style>
+1
source share
1 answer

This should work for you:

textView.setTextAppearance(getApplicationContext(), R.style.popup_window_text_style);

But note that some properties cannot be affected and may need to be set manually as indicated in the Android developer docs .

Unfortunately, the current SDK does not have any form of the setStyle () method.

+3
source

All Articles