Reputation: 573
In my activity, the soft keyboard should more or less always be open. So when a user presses the back button, the activity should finish like normally. However, the default Android behavior is to close the keyboard instead when it is open. This makes the user have to click twice to exit the activity. How can I override this behavior so that the activity always finishes when the back button is pressed, even when the soft keyboard is open? Is there some simple way to do this?
I'm sure this is a common problem, but I did not find this direct question.
Upvotes: 2
Views: 1606
Reputation: 1648
You should implement TextWatcher
.
Here is the the code of you requirement.
public class MainActivity extends AppCompatActivity implements TextWatcher{
EditText editTextt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextt = (EditText) findViewById(R.id.editTextt);
editTextt.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(before == count + 1)
{
finish();
}
}
}
Upvotes: 1
Reputation: 2677
I have same situation.I tried so many ways finally i got solution. Here i will share with you. I have solved this using CustomEditText.
CustomEditText.Java
public class CustomEditText extends android.support.v7.widget.AppCompatEditText {
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
OnKeyPreImeListener onKeyPreImeListener;
public void setOnKeyPreImeListener(OnKeyPreImeListener onKeyPreImeListener) {
this.onKeyPreImeListener = onKeyPreImeListener;
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
if(onKeyPreImeListener != null)
onKeyPreImeListener.onBackPressed();
return false;
}
return super.dispatchKeyEvent(event);
}
public interface OnKeyPreImeListener {
void onBackPressed();
}
}
MainActivity.Java
public class MainActivity extends BaseActivity{
private CustomEditText editSearchMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editSearchMenu =
(CustomEditText)findViewById(R.id.editSearchMenu);
//Initialise interface
CustomEditText.OnKeyPreImeListener onKeyPreImeListener=new CustomEditText.OnKeyPreImeListener() {
@Override
public void onBackPressed() {
((MainActivity)getActivity()).finish();
}
};
editSearchMenu.setOnKeyPreImeListener(onKeyPreImeListener);
}
}
xml file
<com.app.helper.CustomEditText
android:id="@+id/editSearchMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/diam5dp"
android:background="@drawable/custom_rounded_edittext"
android:drawableLeft="@drawable/ico_search"
android:drawablePadding="@dimen/diam20dp"
android:drawableStart="@drawable/ico_search"
android:hint="@string/M_SEARCH_HINT"
android:padding="@dimen/diam10dp"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white"
android:textSize="@dimen/diam16sp" />
Hope this will help you...this works in every devices i have tested my self...other solutions are creating problems in some devices..there will be some other solution also...if you get something let me know...
Upvotes: 1
Reputation: 8237
Try this.
mEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
finish();
return false;
}
});
You can hide Keyboard and finish Activity.
If you use Dialog,you can do like this.
mDialog.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (isShowDialog) {
isShowDialog = false;
mDialog.dismiss();
context.finish();
}
}
return false;
}
});
Note: It also can use in Dialog.Dismiss Dialog and do something.And you can use v
、 keyCode
、 event
.And it can be used by most View.
Upvotes: 0
Reputation: 525
I use this method to hide the keyboard
//hides keyoard
private void hideSoftKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
Upvotes: 0