RAPOS
RAPOS

Reputation: 99

As for the TextView, programmatically change the style?

For example:

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

How to set the style?

textView.set... ???

Upvotes: 5

Views: 17875

Answers (5)

Jorge Castillo
Jorge Castillo

Reputation: 545

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);

Upvotes: 20

user1140237
user1140237

Reputation: 5045

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

Upvotes: 6

RAPOS
RAPOS

Reputation: 99

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);

Upvotes: -1

Ali Aliasghar
Ali Aliasghar

Reputation: 49

You can use the following links :

How to change a TextView's style at runtime

and

Set TextView style (bold or italic)

Good luck .

Upvotes: 1

Gagan Sethi
Gagan Sethi

Reputation: 407

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);

Upvotes: 9

Related Questions