5

For example:

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

How to set the style?

textView.set... ???
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
RAPOS
  • 99
  • 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,963
  • 1
  • 19
  • 27
Jorge Castillo
  • 555
  • 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
6

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
  • 5,015
  • 1
  • 28
  • 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
  • 49
  • 1
  • 9
-1

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
  • 4,195
  • 2
  • 42
  • 53
RAPOS
  • 99
  • 1
  • 2
  • 10