How to change a TextView's style at runtime

JavaAndroidTextview

Java Problem Overview


I have an android app on which, when the user taps a TextView, I would like to apply a defined style.

I thought to find a textview.setStyle() but it doesn't exists. I tried

textview.setTextAppearance();

but it does not work.

Java Solutions


Solution 1 - Java

I did this by creating a new XML file res/values/style.xml as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>

	<style name="boldText">
		<item name="android:textStyle">bold|italic</item>
		<item name="android:textColor">#FFFFFF</item>
	</style>
	
	<style name="normalText">
		<item name="android:textStyle">normal</item>
		<item name="android:textColor">#C0C0C0</item>
	</style>
		
</resources>

I also have an entries in my "strings.xml" file like this:

<color name="highlightedTextViewColor">#000088</color>
<color name="normalTextViewColor">#000044</color>

Then, in my code I created a ClickListener to trap the tap event on that TextView: EDIT: As from API 23 'setTextAppearance' is deprecated

    myTextView.setOnClickListener(new View.OnClickListener() {
    			public void onClick(View view){
    				//highlight the TextView
    				//myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
    if (Build.VERSION.SDK_INT < 23) {
       myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
    } else {
       myTextView.setTextAppearance(R.style.boldText);
    }
     myTextView.setBackgroundResource(R.color.highlightedTextViewColor);
    			}
    		});

To change it back, you would use this:

if (Build.VERSION.SDK_INT < 23) {
    myTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
} else{
   myTextView.setTextAppearance(R.style.normalText);
}
myTextView.setBackgroundResource(R.color.normalTextViewColor);

Solution 2 - Java

Like Jonathan suggested, using textView.setTextTypeface works, I just used it in an app a few seconds ago.

textView.setTypeface(null, Typeface.BOLD); // Typeface.NORMAL, Typeface.ITALIC etc.

Solution 3 - Java

TextView tvCompany = (TextView)findViewById(R.layout.tvCompany);
tvCompany.setTypeface(null,Typeface.BOLD);

You an set it from code. Typeface

Solution 4 - Java

Programmatically: Run time

You can do programmatically using setTypeface():

textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text
textView.setTypeface(null, Typeface.BOLD);        // for Bold only
textView.setTypeface(null, Typeface.ITALIC);      // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic

XML: Design Time

You can set in XML as well:

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

Hope this will help

Summved

Solution 5 - Java

i found textView.setTypeface(Typeface.DEFAULT_BOLD); to be the simplest method.

Solution 6 - Java

try this line of code.

textview.setTypeface(textview.getTypeface(), Typeface.DEFAULT_BOLD);

here , it will get current Typeface from this textview and replace it using new Typeface. New typeface here is DEFAULT_BOLD but you can apply many more.

Solution 7 - Java

See doco for setText() in TextView http://developer.android.com/reference/android/widget/TextView.html

> To style your strings, attach android.text.style.* objects to a SpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.

Solution 8 - Java

Depending on which style you want to set, you have to use different methods. TextAppearance stuff has its own setter, TypeFace has its own setter, background has its own setter, etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCrisView Question on Stackoverflow
Solution 1 - JavaGlennView Answer on Stackoverflow
Solution 2 - JavaDKDiveDudeView Answer on Stackoverflow
Solution 3 - JavaAzharView Answer on Stackoverflow
Solution 4 - JavaSummved JainView Answer on Stackoverflow
Solution 5 - JavasferaView Answer on Stackoverflow
Solution 6 - JavaMd. Sajedul KarimView Answer on Stackoverflow
Solution 7 - JavaGSreeView Answer on Stackoverflow
Solution 8 - JavaakohoutView Answer on Stackoverflow