0

I'm a beginner. I want to define the xml for a custom EditText and then programmatically add those custom edittexts at runtime without using a bunch of code to customize the editTexts. This could be similar to implementing custom buttons, textviews, etc. from libraries...although it would be my own. What is the best way to approach this?

Thanks!

sanic
  • 1,905
  • 2
  • 15
  • 31

1 Answers1

4

Alternative code apart from code shared in above comment link is below : basically this code makes easy for user to customize the fonts etc.

public class MyEditText extends EditText {

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

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        parseAttributes(context, attrs);
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        parseAttributes(context, attrs);
    }

    private void parseAttributes(Context context, AttributeSet attrs) {
        TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.Simplified);

        int typefaceValue = values.getInt(R.styleable.Simplified_typeface, 0);
        values.recycle();

        setTypeface(MyFontUtil.obtaintTypeface(context, typefaceValue));
    }

}

XML

 <com.my.mtetno.widget.MyEditText
  android:id="@+id/uname"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:background="@drawable/lh_edit_field_without_border"
  android:inputType="textEmailAddress"
  android:maxLines="1"
  android:overScrollMode="always"
  android:textSize="@dimen/login_page_edit_text_size"
  app:typeface="simplified_regular" />
KOTIOS
  • 10,974
  • 3
  • 34
  • 61
  • if you wanna learn more abt AttributeSet used in my code above this is the best link --> http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml – KOTIOS Nov 25 '14 at 04:36