43

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
lokoko
  • 5,785
  • 5
  • 35
  • 68
  • you can not set the style of any view pragmatically. but you can set individual properties of the view – Kapil Vats Feb 22 '13 at 08:53
  • Possible duplicate of [Android - set TextView TextStyle programmatically?](http://stackoverflow.com/questions/7919173/android-set-textview-textstyle-programmatically) – Massimiliano Kraus Dec 02 '16 at 09:31

8 Answers8

72

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);.

Matthew
  • 6,356
  • 9
  • 47
  • 59
  • 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 Feb 09 '16 at 15:38
  • 1
    To simplify @Nedko's solution you can use: `TextViewCompat.setTextAppearance(tv, styleResource);` – ezefire Apr 06 '23 at 08: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);
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • 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. – James A Wilson Aug 26 '15 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);
John Rattz
  • 320
  • 1
  • 7
  • 10
8

Compat version (because of deprecation in api 23):

TextViewCompat.setTextAppearance(textView, android.R.style.TextAppearance_DeviceDefault_Small);
shmakova
  • 6,076
  • 3
  • 28
  • 44
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);
Pragnani
  • 20,075
  • 6
  • 49
  • 74
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

rds
  • 26,253
  • 19
  • 107
  • 134
outlying
  • 578
  • 1
  • 8
  • 19
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);
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
0
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)
raj varsani
  • 3
  • 1
  • 3