45

I'm creating a textView programmatically. Is there a way that i can set the style of this textView? Something similar to

style="@android:style/TextAppearance.DeviceDefault.Small"

which I would use if I had a layout.xml file.

2

8 Answers 8

73

You can't programmatically set the style of a View, but you might be able to do something like textView.setTextAppearance(context, android.R.style.TextAppearance_Small);.

2
  • 16
    Use if (Build.VERSION.SDK_INT < 23) { textView.setTextAppearance(context, android.R.style.TextAppearance_Small); } else { textView.setTextAppearance(android.R.style.TextAppearance_Small); }
    – Nedko
    Commented Feb 9, 2016 at 15:38
  • 1
    To simplify @Nedko's solution you can use: TextViewCompat.setTextAppearance(tv, styleResource);
    – ezefire
    Commented Apr 6, 2023 at 8:12
34

It is not currently possible to set the style of a View programatically.

To get around this you can create a template layout xml file with the style assigned, for example in res/layout create tvtemplate.xml as with the following content:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is a template"
        style="@android:style/TextAppearance.DeviceDefault.Small" />

then inflate this to instantiate your new TextView:

TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
1
  • 1
    I like this solution better than the accepted answer since it supports style as the OP requested (and I wanted). Besides, it always feels so....wrong.... to use an android.widget constructor. Commented Aug 26, 2015 at 12:45
16

Actually, this is possible as of API level 21.

TextView has a 4 parameter constructor:

TextView (Context context, 
          AttributeSet attrs, 
          int defStyleAttr, 
          int defStyleRes)

The middle two parameters are not necessary in this case. The following code creates a TextView directly in an activity and only defines its style resource:

TextView myStyledTextView = new TextView(this, null, 0, R.style.my_style);
8

Compat version (because of deprecation in api 23):

TextViewCompat.setTextAppearance(textView, android.R.style.TextAppearance_DeviceDefault_Small);
6

Try this

textview.setTextAppearance(context, R.style.yourstyle);

this may not work try Creating an xml with the textview like this

textviewstyle.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@android:style/TextAppearance.DeviceDefault.Small" />

To get the desired style inflate the xml that contains textview

TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvstyle, null);
1

For Views generated at run time ( in code ) you may pass style to constructor: View(Context, AttributeSet, int)

In other case you have to call varios methods to change apperance

1
textView.setTextAppearance(context, android.R.style.TextAppearance_Small);

is deprecated in java so use it without context for kotlin

textView.setTextAppearance(android.R.style.TextAppearance_Small)
0
@Deprecated
public void setTextAppearance(Context context, @StyleRes int resId)

This method is deprecated as of Android SDK 23.

You can use safe version:

if (Build.VERSION.SDK_INT < 23) {
    super.setTextAppearance(context, resId);
} else {
    super.setTextAppearance(resId);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.