Reputation: 539
I'm getting a problem in a UI form that I'm building in Android. In that form I have some edit text in which the user has to touch them to open a dialog fragment. In the dialog fragment, user can set a value and then, this value is shown on the edittext touched. The problem is the following: when user close the dialog fragment and the edittext touched gets focus, if the user press the back button to go out, the onBackPressed() method is not being called.
I must clarify that the edittexts that open a dialog fragment doesn't show keyboard because the user can't write on them. I don't want to use textviews.
Here I show you part of the layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
....
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_input_when"
android:layout_marginTop="30dp">
<EditText
android:hint="@string/meeting_when"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text_when"
android:textSize="18sp"
android:inputType="text|date"
android:textIsSelectable="true"
android:focusable="true"
android:drawableLeft="@drawable/ic_black_18dp"
android:drawableStart="@drawable/ic_black_18dp"
android:drawablePadding="10dp"
android:onClick="onEditTextWhenClicked"
android:nextFocusForward="@+id/edit_text_time"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/text_input_time"
android:layout_marginTop="30dp">
<EditText
android:hint="@string/meeting_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text_time"
android:textSize="18sp"
android:inputType="time"
android:textIsSelectable="true"
android:nextFocusForward="@+id/edit_text_place"
android:focusable="true"
android:drawableLeft="@drawable/ic__black_18dp"
android:drawableStart="@drawable/ic__black_18dp"
android:drawablePadding="10dp"
android:onClick="onEditTextTimeClicked" />
</android.support.design.widget.TextInputLayout>
....
....
</LinearLayout>
So, for example, if the user touch the "when" edit text, a datepicker dialog is open:
when the user set the date, the dialog fragment is closed and the value is setted on the edit text
And now, if the user press the back button, it doesn't work.
In the activity I have
@Override
public void onBackPressed(){
if ( !areAllFieldEmpty() ) {
showAlertCloseDialog();
}else
super.onBackPressed();
}
But these method it doesn't be called. . I don't have any idea about how to solve it. Please help me. If you need more information, let me know it. Thanks.
Upvotes: 4
Views: 1527
Reputation: 7971
Register a callback in your fragment to be invoked when a hardware key is pressed in your view:
if (mLayout != null) {
mLayout.setFocusableInTouchMode(true);
}
mLayout.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
getActivity().onBackPressed();
return true;
}
return false;
}
});
If it's the back key, go back and consume the event returning true
, return false
otherwise.
If you have troubles with back and the keyboard override onKeyPreIme()
extending your EditText:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// User has pressed Back key. So hide the keyboard
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
// Hide your view as you do it in your activity
} else if (keyCode == KeyEvent.KEYCODE_MENU) {
// Eat the event
return true;
}
return false;
}
Upvotes: 1