Reputation: 34530
I have an Activity, where I want the software keyboard to be alaways opened. How to close the Activity after BACK press, when keyboard is opened? Now I have to click BACK twice, first to close the keyboard and then to finish the Activity.
Upvotes: 14
Views: 4051
Reputation: 9629
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
But tell you one thing, you have to put another effort for detecting that keyboard is open and than use flag which can be used to decide whether go back or finish activity.
Upvotes: 0
Reputation: 445
I don't believe you will need the listener, setOnBackButtonListener, as it's been first suggested.
I've got it working by using getContext():
public class EditedText extends EditText {
@Override
public boolean onKeyPreIme (int keyCode, KeyEvent event){
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
Activity activity = (Activity) getContext();
activity.finish();
return true;
}
return super.onKeyPreIme(keyCode, event);
}
}
Note, since the event was 'handled', I'm returning true and not false as again first suggested in previous answers.
Upvotes: 1
Reputation: 46787
As mentioned, onKeyPreIme can be used to catch the back button, but this needs to be overridden on the text view, not in the activity.
Here's a complete solution:
First a new class that derived from EditText, overrides onKeyPreIme and calls a callback interface:
// EditTextWithBackButton class
public class EditTextWithBackButton extends EditText
{
public interface IOnBackButtonListener
{
boolean OnEditTextBackButton();
}
public EditTextWithBackButton(Context context)
{
super(context);
}
public EditTextWithBackButton(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public void setOnBackButtonListener(IOnBackButtonListener l)
{
_listener = l;
}
IOnBackButtonListener _listener;
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event)
{
if (event.getAction()==KeyEvent.ACTION_UP && keyCode==KeyEvent.KEYCODE_BACK)
{
if (_listener!=null && _listener.OnEditTextBackButton())
return false;
}
return super.onKeyPreIme(keyCode, event); //To change body of overridden methods use File | Settings | File Templates.
}
}
Next, update your layout:
<com.yournamespace.whatever.EditTextWithBackButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textField"
/>
Next, update your activity, inside OnCreate, after setContentView:
((EditTextWithBackButton) findViewById(R.id.textField)).setOnBackButtonListener(new EditTextWithBackButton.IOnBackButtonListener()
{
@Override
public boolean OnEditTextBackButton()
{
finish();
return true;
}
});
Upvotes: 9
Reputation: 186
set a OnEditorActionListener for the EditText in the activity for that you are providing the soft keyboard like below,
editText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (event != null
&& (event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
finish();
}
return false;
}
});
Upvotes: 0
Reputation: 668
Use onKeyPreIme(int keyCode, KeyEvent event) method and check for KeyEvent.KEYCODE_BACK event. It's very simple without doing any fancy coding.
Upvotes: 4
Reputation: 40168
You can use the following
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyUp(keyCode, event);
}
Upvotes: 0