How To Make Add Custom Attributes To View In Android

Posted By : Prince Bhardwaj | 27-Sep-2017

To add a custom attribute to a view, you must do these steps -

1) Define custom to your view in a <declarable-styleable> resource element.

2) Specify values for the attributes in your XML layout.

3) Retrieve attribute values at runtime.

4) Apply the retrieved attribute values to your view.

 

 

To define custom attributes, add <declare-styleable> to your project.  Here is an example of attrs.xml file- 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Text">
        <attr name="fontStyle" format="enum">
            <enum name="roboto_light" value="0"/>
            <enum name="roboto_regular" value="1"/>
        </attr>
    </declare-styleable>
</resources>

 

 

Once you have created custom attribute for your view, you can use them in you xml layout as -

 

<com.android.belfrics.utils.helpers.custom.MyTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/login_to_continue"
    android:textColor="@android:color/white"
    android:layout_marginBottom="@dimen/margin_48"
    android:textSize="@dimen/text_size_secondary"
    android:gravity="center"
    app:fontStyle="roboto_regular"/>

 

 

To get attributes and apply them on your view, here is an example class for view -

 

 public class MyTextView extends android.support.v7.widget.AppCompatTextView {

    public MyTextView(Context context) {
        super(context);
        setTypeFace(0);
    }

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


        applyCustomFont(context,attrs);
    }

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

        applyCustomFont(context,attrs);
    }

    private void applyCustomFont(Context context, AttributeSet attrs) {
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.Text,
                0, 0);
        int mTextPos=0;
        try {
            mTextPos = a.getInteger(R.styleable.Text_fontStyle, 0);
        } finally {
            a.recycle();
        }
        setTypeFace(mTextPos);
    }

    private void setTypeFace(int mTextPos) {
        Typeface myTypeface = null;
        switch (mTextPos){
            case 0:
                myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
                break;

            case 1:
                myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf");
                break;
        }
        if(myTypeface!=null) {
            Logger.LogDebug("typeface ", String.valueOf(mTextPos));
            setTypeface(myTypeface);
        }
    }
}
 

 

 

About Author

Author Image
Prince Bhardwaj

Prince Bhardwaj is having Talent as Application Developer, he is an enthusiastic team player in Oodles Technologies.

Request for Proposal

Name is required

Comment is required

Sending message..