103

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.

Azhar
  • 20,500
  • 38
  • 146
  • 211
Cris
  • 12,124
  • 27
  • 92
  • 159
  • 1
    I've read somewhere that this is not possible... http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view . But you can set some properties separately. – Floern Jan 07 '11 at 22:00
  • 1
    Did you try: textView.setTextTypeface? http://developer.android.com/reference/android/widget/TextView.html#setTypeface(android.graphics.Typeface) – Jonathan Roth Jan 07 '11 at 22:54
  • 21
    @Falmarri Have YOU looked through the TextView api documents? If so, maybe you could say something more constructive. – Fletch Jan 17 '12 at 15:32

7 Answers7

144

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);
Pankaj
  • 7,908
  • 6
  • 42
  • 65
Glenn
  • 3,338
  • 3
  • 20
  • 14
  • 2
    Actually this doesnt work with shadow styles. If you would like to change the shadow you must do it this way: lockText.setShadowLayer(20, 4, 4, 0xFFFF0000); – Lukas Apr 24 '13 at 15:07
  • 4
    To avoid the SDK checking you can use TextViewCompat.setTextAppearance(myTextView, R.style.myStyle); – Justin Fiedler Sep 21 '16 at 17:13
91

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.
Alex Kucherenko
  • 20,168
  • 2
  • 26
  • 33
DKDiveDude
  • 1,131
  • 2
  • 10
  • 16
  • 9
    You can use textView.setTypeface(null, Typeface.BOLD); textView..setTypeface(null, Typeface.ITALIC); textView.setTypeface(null, Typeface.BOLD_ITALIC); – peceps Jun 16 '11 at 08:32
  • 2
    textView.setTypeface(null, Typeface.BOLD) is what worked for me. Of course, you could use 1 instead but I guess it's not a good practice to enter constants manually. Better use predefined variables in libraries. – Pijusn Aug 13 '12 at 08:36
  • 4
    Don't encourage people to use ints instead of constants – Chris.Jenkins Sep 11 '12 at 16:00
  • 3
    Good one! My mistake was that I was sending my old typeface in first parameter. So it does work when I change NORMAL -> BOLD but not BOLD -> NORMAL. I didn't know the first parameter could be null! Now it works fine for me! – Felipe Dec 02 '12 at 01:39
12
TextView tvCompany = (TextView)findViewById(R.layout.tvCompany);
tvCompany.setTypeface(null,Typeface.BOLD);

You an set it from code. Typeface

Azhar
  • 20,500
  • 38
  • 146
  • 211
2

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

sfera
  • 1,138
  • 1
  • 15
  • 21
  • This changes the Typeface. _not_ the style as specified by the OP. – HBG Jun 29 '17 at 14:53
  • True, still this was the simplest method - and as far as I recall the only method - at the time of the post. I'm not sure if a new API method was added in the meantime which enables changing the "style" to bold without altering the typeface. Please correct me if I'm wrong. – sfera Jun 30 '17 at 15:34
1

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.

Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
0

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.

akohout
  • 1,802
  • 3
  • 23
  • 42
0

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.

GSree
  • 2,890
  • 1
  • 23
  • 25