4

For example:

TextView textView = new TextView(PhotoActivity.this);
textView.setText("Photo not found.");

How to set the style?

textView.set... ???
Viral Patel
  • 30,539
  • 18
  • 75
  • 106
RAPOS
  • 91
  • 1
  • 2
  • 10
  • check (Related Method) next to (XML Attributes) in the android docs for textview http://developer.android.com/reference/android/widget/TextView.html – Tasos Jan 19 '16 at 06:08
  • Search on google .Too old question – IntelliJ Amiya Jan 19 '16 at 06:12
  • Possible duplicate of [Android - set TextView TextStyle programmatically?](http://stackoverflow.com/questions/7919173/android-set-textview-textstyle-programmatically) – Vishwa Jan 19 '16 at 06:20
  • Please accept an answer and I will come back to upvote the question; many people have offered good information. – PGMacDesign May 08 '17 at 18:28

5 Answers5

20

For styling TextView programmatically you must have a style inheriting a TextAppearance style, and you can apply it using the following:

TextViewCompat.setTextAppearance(statusLinkTextView, R.style.YourTextAppearanceStyle);
Mohamed Nageh
  • 1,795
  • 1
  • 17
  • 27
Jorge Castillo
  • 525
  • 4
  • 7
8

For styling you can use from following these options:

textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);
Gagan Sethi
  • 397
  • 1
  • 8
5

You can change TextView Style by using setTextAppearance / setTextAppearance

for below api level 23

setTextAppearance(int res id)

From api level 23

setTextAppearance (Context context, int resId)


TextView textView = new TextView(PhotoActivity.this);
        textView.setText("Photo not found.");
        if (Build.VERSION.SDK_INT > 22) {
            textView.setTextAppearance(PhotoActivity.this, R.style.yourTextViewStyleResourceID);
            /*
            * To give font style Bold Italic I would suggest you check this answer
            * https://stackoverflow.com/a/6200841
            * */
        } else {
            textView.setTextAppearance(R.style.yourTextViewStyleResourceID);
        }

UPDATE

Its also possible to give style without checking sdk version

TextViewCompat.setTextAppearance(textViewObject, R.style.yourTextViewStyleResourceID);

To give font style Bold Italic I would suggest you check this answer

user1140237
  • 4,927
  • 1
  • 27
  • 56
  • 4
    As @JorgeCastillo [notes](http://stackoverflow.com/a/39247670/295028), you can avoid the version check by using TextViewCompat: `TextViewCompat.setTextAppearance(statusLinkTextView, R.style.YourTextAppearanceStyle);` – Beer Me Oct 10 '16 at 15:36
1

You can use the following links :

How to change a TextView's style at runtime

and

Set TextView style (bold or italic)

Good luck .

Community
  • 1
  • 1
Ali Aliasghar
  • 57
  • 1
  • 9
0

Thank you all , I found the solution I need .

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;

TextView textView = new TextView(PhotoActivity.this);
textView.setText("Photo not found.");
textView.setTextColor(Color.WHITE);
textView.setLayoutParams(params);

As an alternative way:

textView.setGravity(Gravity.CENTER);
valerybodak
  • 3,780
  • 2
  • 38
  • 51
RAPOS
  • 91
  • 1
  • 2
  • 10
  • if this your answer and solution than question should "How to set TextColor dynamically ? " not how to set Style ? – user1140237 Jan 19 '16 at 06:22
  • in which multiverse does this change the style as stated in "As for the TextView, programmatically change the style?" ???? – desgraci Jun 12 '20 at 07:14