This is my CustomEditText Class and its attr.xml
public class CustomEditText extends EditText implements TextWatcher {
private Typeface tf = null, tfhint = null;
private String customhintfont, customFont;
boolean inputtypepassword;
private CharSequence chartype;
// private CharSequence mSource;
public CustomEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
// initViews();
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
// initViews();
setCustomFontEdittext(context, attrs);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
// initViews();
setCustomFontEdittext(context, attrs);
}
private void setCustomFontEdittext(Context ctx, AttributeSet attrs) {
final TypedArray a = ctx.obtainStyledAttributes(attrs,
R.styleable.CustomEditText);
customFont = a.getString(R.styleable.CustomEditText_edittextfont);
customhintfont = a
.getString(R.styleable.CustomEditText_edittextfontHint);
// custompwd = a.getString(R.styleable.CustomEditText_edittextpwd);
inputtypepassword = a.getBoolean(
R.styleable.CustomEditText_edittextpwd, false);
setCustomFontEdittext(ctx, customFont);
setCustomFontEdittextHint(ctx, customhintfont);
chartype = (CharSequence) a
.getText(R.styleable.CustomEditText_editcharpwd);
setCustompwd(inputtypepassword);
a.recycle();
}
public boolean setCustompwd(boolean pwd) {
if (pwd) {
this.setTransformationMethod(new PasswordCharacterChange());
}
return pwd;
}
public boolean setCustomFontEdittext(Context ctx, String asset) {
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
return false;
}
setTypeface(tf);
return true;
}
public boolean setCustomFontEdittextHint(Context ctx, String asset) {
try {
tfhint = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
return false;
}
setTypeface(tfhint);
return true;
}
@Override
public void onTextChanged(CharSequence text, int start, int lengthBefore,
int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
if (text.length() < 0) {
setCustomFontEdittextHint(getContext(), customhintfont);
} else {
setCustomFontEdittextHint(getContext(), customFont);
}
if (TextUtils.isEmpty(text)) {
setCustomFontEdittextHint(getContext(), customhintfont);
}
// this.setText("*");
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public class PasswordCharacterChange extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
// TODO Auto-generated method stub
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return chartype.charAt(0); // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
}
}
2) attrs.xml
<declare-styleable name="CustomEditText">
<attr name="edittextfont" format="string" />
<attr name="edittextfontHint" format="string" />
<attr name="edittextpwd" format="string" />
<attr name="editcharpwd" format="string" />
</declare-styleable>
3) activity_main.xml
<com.customdemo.CustomEditText
android:id="@+id/activity_main_edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:edittextfont="Calibri Bold Italic.ttf"
app:edittextfontHint="beatmygu.ttf"/>
<com.customdemo.CustomEditText
android:id="@+id/activity_main_edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:editcharpwd="$"
app:edittextpwd="true"
/>