Sunday 29 June 2014

Custom EditText With change hint font style ,text font style and password character change.


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"
       />




4 comments:

  1. Replies
    1. include xmlns:app="http://schemas.android.com/apk/res-auto" in top layout

      Delete
  2. Dafont.com is a site where you can download a ton of free fonts. You can search for a specific typeface, or search by the type of lettering you want, whether it’s serif or sans serif, hand lettered or grunge style. You can also put in your own phrase to see how it looks in a particular font. A lot of these fonts are very decorative and many are handdrawn, so it’s not always the best place to search for body text fonts. Each selection also tells you whether your download is free for personal or commercial use. The download is easy – you get a zip file with the font file inside. Unzip, install, and you’re ready to go.

    ReplyDelete
  3. Dafont.com is a site where you can download a ton of free fonts. You can search for a specific typeface, or search by the type of lettering you want, whether it’s serif or sans serif, hand lettered or grunge style. You can also put in your own phrase to see how it looks in a particular font. A lot of these fonts are very decorative and many are handdrawn, so it’s not always the best place to search for body text fonts. Each selection also tells you whether your download is free for personal or commercial use. The download is easy – you get a zip file with the font file inside. Unzip, install, and you’re ready to go.

    ReplyDelete