Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent softkeyboard dismiss on the android back button press

Tags:

android

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.

like image 446
Jesbin MJ Avatar asked Dec 31 '14 13:12

Jesbin MJ


People also ask

How do I dismiss my keyboard on Android?

To dismiss the keyboard, call clearFocus() on the respective element when the button is clicked.


1 Answers

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 ..... /> 
like image 157
Shayan Pourvatan Avatar answered Oct 06 '22 03:10

Shayan Pourvatan