Skip to content

Instantly share code, notes, and snippets.

@Arjun-sna
Created February 21, 2017 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arjun-sna/768de4ca141798c59a8166a2e5d4a743 to your computer and use it in GitHub Desktop.
Save Arjun-sna/768de4ca141798c59a8166a2e5d4a743 to your computer and use it in GitHub Desktop.
Workaround for android `Softkeyboard` hiding `Edittext` and with custom `TypeFace`
public class TypeFaceEditText extends EditText {
static Map<String, Typeface> inflatedFonts = new HashMap<>();
public TypeFaceEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public TypeFaceEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public TypeFaceEditText(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TypeFaceEditText);
String fontName = a.getString(R.styleable.TypeFaceEditText_etFontName);
if (fontName != null) {
Typeface myTypeface = inflatedFonts.get(fontName);
if (myTypeface == null) {
myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
inflatedFonts.put(fontName, myTypeface);
}
setTypeface(myTypeface);
}
a.recycle();
}
handleActionBtnClick();
}
private void handleActionBtnClick() {
setOnEditorActionListener(new OnEditorActionListener() {
@Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
((InputMethodManager) v.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
v.getWindowToken(), 0);
clearFocus();
return false;
}
});
}
@Override public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
clearFocus();
}
return super.onKeyPreIme(keyCode, event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment