I have an activity with an edittext. when the activity comes, i always focusing that Edittext. i made the softkeyboard always visible by giving
android:windowSoftInputMode="stateAlwaysVisible"
in the manifest. i m doing the functionality on the done button press of the softkeyboard. Actually my need is the softkeyboard should always present when the user is on this activity. now i disabled the back button press, by overriding and do nothing.
@Override public void onBackPressed() { // Do nothing }
but softkeyboard dismisses on the back press. how to get rid of this. I had tried this Prevent soft keyboard from being dismissed but still on the back press keyboard get dismisses. But it comes back by again pressing the back button, My need is backpress should not dismiss the softkeyboard. Any help will be appreciated.
To dismiss the keyboard, call clearFocus() on the respective element when the button is clicked.
create one custom EditText like follow with one interface:
public class CustomEditText extends EditText { public void setHandleDismissingKeyboard( handleDismissingKeyboard handleDismissingKeyboard) { this.handleDismissingKeyboard = handleDismissingKeyboard; } private handleDismissingKeyboard handleDismissingKeyboard; public interface handleDismissingKeyboard { public void dismissKeyboard(); } @SuppressLint("NewApi") public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); // TODO Auto-generated constructor stub } public CustomEditText(Context context) { super(context); // TODO Auto-generated constructor stub } public CustomEditText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub } @Override public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { handleDismissingKeyboard.dismissKeyboard(); return true; } return super.dispatchKeyEvent(event); }
then in your activity create initialize your CustomEditText and use:
customEditText.setHandleDismissingKeyboard(this);
then implement class and override method and put your code in that
for more info about my answer all thing you need is onKeyPreIme
, you can override that in your EditText class ( as i post for you ) to handle all key on that, like back Key or any other keyboard key, I put one inner interface in custom class to get call back from this class to each activity or class that you want, you can do that with static method too, and as you want prevent from dismissing keyboard you can just return true
in that.
for using CustomEditText
you can use xml or in java, define that and initialize that like other widget ( Button , TextView , ... ), and only different is you need define this in your xml like :
<yourPackage.CustomEditText ..... />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With