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.
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.
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);
.
if (Build.VERSION.SDK_INT < 23) { textView.setTextAppearance(context, android.R.style.TextAppearance_Small); } else { textView.setTextAppearance(android.R.style.TextAppearance_Small); }
–
Adverb TextViewCompat.setTextAppearance(tv, styleResource);
–
Psychophysiology 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);
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);
Compat version (because of deprecation in api 23):
TextViewCompat.setTextAppearance(textView, android.R.style.TextAppearance_DeviceDefault_Small);
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);
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
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)
@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);
}
© 2022 - 2024 — McMap. All rights reserved.