How do I set the style of a textView programmatically?
Asked Answered
C

8

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.

Cysto answered 22/2, 2013 at 8:50 Comment(2)
you can not set the style of any view pragmatically. but you can set individual properties of the viewTrefler
Possible duplicate of Android - set TextView TextStyle programmatically?Assent
L
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);.

Lashay answered 22/2, 2013 at 8:55 Comment(2)
Use if (Build.VERSION.SDK_INT < 23) { textView.setTextAppearance(context, android.R.style.TextAppearance_Small); } else { textView.setTextAppearance(android.R.style.TextAppearance_Small); }Adverb
To simplify @Nedko's solution you can use: TextViewCompat.setTextAppearance(tv, styleResource);Psychophysiology
W
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);
Westwardly answered 22/2, 2013 at 8:53 Comment(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.Discord
S
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);
Superficial answered 3/6, 2017 at 14:37 Comment(0)
B
8

Compat version (because of deprecation in api 23):

TextViewCompat.setTextAppearance(textView, android.R.style.TextAppearance_DeviceDefault_Small);
Bivalve answered 7/5, 2018 at 16:52 Comment(0)
A
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);
Aekerly answered 22/2, 2013 at 8:57 Comment(0)
D
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

Duppy answered 22/2, 2013 at 8:55 Comment(0)
E
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)
Explicate answered 16/5, 2021 at 15:31 Comment(0)
Z
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);
}
Zenithal answered 17/1, 2018 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.