1

I am creating subclasses of various Android widgets to create my own widgets. Here is what I have done so far:

(defined in my res/values/attr.xml)

<attr name="font">
    <enum name="ARIAL_BOLD" value="1" />
    <enum name="ARIAL_ROUND_MT" value="2" />
    <enum name="HELVETICA" value="3" />
    <enum name="HELVETICA_BOLD" value="4" />
    <enum name="GILSANCE_LIGHT" value="4" />
  </attr>

<declare-styleable name="EditText">
    <attr name="font" />
</declare-styleable>
<declare-styleable name="Button">
    <attr name="font" />
</declare-styleable>
<declare-styleable name="TextView">
    <attr name="font" />
</declare-styleable>

Then in my EditText I use this as:

public class EditText extends android.widget.EditText {

    public EditText(Context context) {
        super(context);     
    }

    public EditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.EditText);
    }
}

Now I want to read the value of the enum that has been set in the XML code. How can I read it? Then, based on the font supplied, I want to set my custom font. Any help will be appreciated.

1 Answer 1

1

This is what worked for me

<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="font">
    <enum name="ARIAL_BOLD" value="1" />
    <enum name="ARIAL_ROUND_MT" value="2" />
    <enum name="HELVETICA" value="3" />
    <enum name="HELVETICA_BOLD" value="4" />
    <enum name="GILSANCE_LIGHT" value="5" />
</attr>

<declare-styleable name="CustomEditText">
    <attr name="font" />
</declare-styleable>
<declare-styleable name="CustomButton">
    <attr name="font" />
</declare-styleable>
<declare-styleable name="CustomTextView">
    <attr name="font" />
</declare-styleable>

</resources>

Then my custom file

public class CustomEditText extends android.widget.EditText {

    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.CustomEditText);
        final int fontValue = a.getInt(R.styleable.CustomEditText_font, 0);
        setTypeFace(fontValue);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.CustomEditText);
        final int fontValue = a.getInt(R.styleable.CustomEditText_font, 0);
        setTypeFace(fontValue);
    }

    public void setTypeFace(int fontValue) {
        Typeface myTypeFace = Typeface.createFromAsset(this.getContext()
            .getAssets(), getApplicationFont(fontValue));
        this.setTypeface(myTypeFace);
    }
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_background"
    android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res/com.mycomp.myproj">

    <TextView
        android:id="@+id/topBarText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#80000000"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/application_name"
        android:textColor="@color/white"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textSize="35sp" />

     <com.myapp.ui.CustomEditText
        app:font="ARIAL_ROUND_MT"
        android:id="@+id/samPLE"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#80000000"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/application_name"
        android:textColor="@color/white"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textSize="35sp" />

This worked for me. Just to note my custom views were placed in a Android Library Project.

Sign up to request clarification or add additional context in comments.

1 Comment

I had tried but got stuck. When i finally got the answer, i shared it with you guys. no harm done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.