Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the style attribute of a TextView

I am creating a Custom TextView class MTextView . Inside the constructor i want to know the value of style attrib of the textview so that I can set different typefaces depending on whether style is set to bold or not. But there is no getStyle() function ? What to do?

    public class MTextView extends TextView{

    public MTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            if(style.equals(TypeFace.bold))  //how to get style?
                setTypeface(Typeface.createFromAsset(getContext().getAssets(),"rc.ttf"));
    }



    }
like image 643
Rahul Verma Avatar asked Apr 02 '13 10:04

Rahul Verma


People also ask

What is the default value of textstyle attribute for textview?

The default value of textStyle attribute for TextView is normal. <TextView android:textStyle="normal" android:text="Normal Text" /> <TextView android:textStyle="italic" android:text="Italic Text" /> <TextView android:textStyle="bold" android:text="Bold Text" /> <TextView android:textStyle="bold|italic" android:text="Bold Italic Text" />

How do I style text in a textviewstyle?

I’d suggest this workflow for styling text: Set any app wide styling in a textViewStyle default style in your theme. Set up a (small) selection of TextAppearance s your app will use (or use/extend from MaterialComponent’s styles) and reference these directly from your views

How to change a textview style at runtime in Android?

How to change a textView Style at runtime in android? This example demonstrates how do I change a textView style in runtime in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

Why can’t I change the style of a text view?

The reason this isn’t possible is that styles, by default, are set through the constructor of the view (e.g., TextView ). So to make this possible, you just need to set defStyleRes through the constructor


2 Answers

You can get the textStyle from the TextView's getTypeface() instance method.

int style = getTypeface().getStyle();

If no textStyle has been specified (i.e. you want to support normal textStyle) then getTypeface() can return null.

In the case where it is not null it may be best to assume textStyle is implicitly set to normal.

like image 149
Rahul Verma Avatar answered Oct 16 '22 00:10

Rahul Verma


Use this code:

    if (attrs != null) {
        try {
            int style = attrs.getAttributeIntValue(
                "http://schemas.android.com/apk/res/android",
                "textStyle", 
                Typeface.NORMAL);

            setTypeface(Typeface.createFromAsset(
                            getContext().getAssets(), 
                            "rc.ttf"), 
                        style);
        }
        catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }
like image 29
Borzh Avatar answered Oct 16 '22 00:10

Borzh