Get custom attribute of custom text view from XML

How to get custom fontname attribute Custom TextView attributes for setting font to text box. Based on the attribute value given by the font in the TextView

public class MyTextView extends TextView
{
    public MyTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

    public MyTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public MyTextView(Context context)
    {
        super(context);
        init();
    }

    public void init()
    {
          // set font_name based on attribute value of textview in xml file
          String font_name = "";
        if (!isInEditMode())
        {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                    "fonts/"+font_name);
            setTypeface(tf);
        }
    }

      

In the Xml file

<com.Example.MyTextView 
            android:id="@+id/header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fontname="font.ttf"
            android:text="Header"   
/>

      

I also put the font.ttf file in assets-> fonts Thanks

+3


source to share


3 answers


1. Add the readAttr (context, attrs) method to your constructor as shown below.

public MyTextView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    readAttr(context,attrs);
    init();
}

public MyTextView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    readAttr(context,attrs)
    init();
}

public MyTextView(Context context)
{
    super(context);
    init();
}

      

2. Define the readAttr () method in the same class.

private void readAttr(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);

    // Read the title and set it if any
    String fontName = a.getString(R.styleable.MyTextView_fontname) ;
    if (fontName != null) {
        // We have a attribute value and set it to proper value as you want
    }

    a.recycle();
}

      

3. Modify attrs.xml file (res / values ​​/attrs.xml) and add below to file

<declare-styleable name="MyTextView">
  <attr name="fontname" format="string" />
</declare-styleable>

      



4. In the Xml file.

<com.Example.MyTextView 
   android:id="@+id/header"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   custom:fontname="font.ttf"
   android:text="Header" />

      

5. Add this line to the top container of the xml file.

xmlns:custom="http://schemas.android.com/apk/res/com.yourpackage.name"

      

Thats all

+5


source


First you want to define declarable-styleable

which should look like this:

<declare-styleable name="MyTextView">
    <attr name="fontname" format="string"/>
</declare-styleable>

      

In your layout, you can "access" this attr , but first you must define namespace

:

xmlns:myPrefix="http://schemas.android.com/apk/res-auto"

      

Note: namespace

is arbitrary. Therefore, you can name it xmlns:whatever

.



And then you can set fontname like this:

<com.Example.MyTextView 
    android:id="@+id/header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    myPrefix:fontname="font.ttf"
    android:text="Header"   
/>

      

To get the value, you must do the following in the constructors MyTextView

:

TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
String font = values.getString(R.styleable.MyTextView_fontname);

// set the typeface etc.

      

Note: MyTextView_fontname

this is always a combination separated by an underscore. So the main structure is StyleableName_AttrName

+3


source


first add your custom font to assets-> fonts directory Like this image

then add this code to values ​​folder of attrs.xml file

 <declare-styleable name="TextViewCustomFont">
    <attr name="showText" format="boolean" />
    <attr name="custom_font" format="enum">
        <enum name="roboto_Regular" value="0"/>
        <enum name="roboto_bold" value="1"/>
        <enum name="roboto_medium" value="2"/>

    </attr>
</declare-styleable>

      

now add this custom class extending TextView. one thing is that it defaults to a normal robot and it has a default of 0. so when the custom: custom_font attribute is not set than a.getInteger (R.styleable.TextViewCustomFont_custom_font, 0) returns 0.

public class CustomFontTextView extends TextView {


    public CustomFontTextView(Context context) {
        super(context);


    }

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);

    }

    public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);



    }

    private void init(Context context, AttributeSet attrs) {
        int fontFlag;
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.TextViewCustomFont,
                0, 0);

        try {

            fontFlag = a.getInteger(R.styleable.TextViewCustomFont_custom_font, 0);
            Log.v("fontFlag", fontFlag + "");
        } finally {
            a.recycle();
        }

        Typeface tf = null;

        if (fontFlag == 0)
            tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf");
        else if (fontFlag == 1)
            tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf");
        else if (fontFlag == 2)
            tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Medium.ttf");


        setTypeface(tf);

    }
}

      

now use this class in your layout file

 <com.example.tomal.CustomFontTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"

                    android:text="@string/title"
                    your_choice:custom_font="roboto_medium"


                    />

      

add to row to parent layout

xmlns:your_choice="http://schemas.android.com/apk/res-auto"

      

0


source







All Articles