Created
February 21, 2017 11:57
-
-
Save Arjun-sna/768de4ca141798c59a8166a2e5d4a743 to your computer and use it in GitHub Desktop.
Workaround for android `Softkeyboard` hiding `Edittext` and with custom `TypeFace`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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