Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting TextView TextAppeareance Programmatically in android

I am going to implement a LinearLayout in which the input fields are programmatically generated according to the number of fields of the database table.

Unfortunately, when I am trying to set the attribute: textApperance as textApperanceLarge in the TextView, it doesn't work. Below is my code...

for (int i = 0; i < selectedProducts; i++) {

            premLayout[i] = new LinearLayout(this);
            premLayout[i].setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            premLayout[i].setOrientation(LinearLayout.HORIZONTAL);
            premLayout[i].setGravity(Gravity.TOP);

            premTextView[i] = new TextView(this);
            premTextView[i].setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                    2.0f));
            premTextView[i].setTextAppearance(this, android.R.attr.textAppearanceLarge);

            premTextView[i].setText(premiumChannels.get(i));
            premTextView[i].setId(i + 600);

            int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());
            premTextView[i].setWidth(px);

            premLayout[i].addView(premTextView[i]);
like image 321
Raju yourPepe Avatar asked Apr 29 '13 03:04

Raju yourPepe


People also ask

How to set Text size of textView dynamically in android?

setTextSize(float size) method to set the size of text. textView. setText(arg) used to set the text in the Text View. Save this answer.

What is TextAppearance in Android?

TextAppearance allows you to define text-specific styling while leaving a View 's style available for other uses. Note, however, that if you define any text attributes directly on the View or in a style, those values would override the TextAppearance values.

How to change textView size in android studio?

Go to File -> Settings, a new setting dialogue box will appear. Then go to Editor -> General. Now mark the checkbox Change font size with Ctrl + Mouse wheel and click on Apply button. Now to change your editor font size, you just have to press and hold Ctrl and rotate the Mouse wheel.


2 Answers

Use like this. It will work.

textView.setTextAppearance(this, android.R.style.TextAppearance_Large);

Or, since API 23, you don't need to pass a context. Hence, you can simply call:

textView.setTextAppearance(android.R.style.TextAppearance_Large);

If you want to support API 23 or higher as well as lower one, you can use the below method to simplify your task. Use the below method only if you are already targeting API 23 or higher. If you are targeting API is less than 23, the below code will give error as the new method wasn't available in it.

public void setTextAppearance(Context context, int resId) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        super.setTextAppearance(context, resId);
    } else {
        super.setTextAppearance(resId);
    }
}
like image 91
Homam Avatar answered Oct 04 '22 20:10

Homam


Use TextViewCompat.setTextAppearance() method which will take care of your sdk version checks.

like image 26
Raghav Sharma Avatar answered Oct 04 '22 20:10

Raghav Sharma