Issue
I have created a dialog whose layout and cancel and submit buttons are entirely customized, thanks to a layout. I would want to handle click on both buttons - in particular, we consider in this question the cancel button.
The problem
When the cancel button is clicked, the click event handler isn't executed.
Implementation
Explanations
I created a dialog fragment. Within, I use the dialogs builder to create my dialog (as recommended by the official documentation). I set the click handler for the cancel button and cancel the dialog within.
The cancel button is clickable (as specified in the layout file). Thus, the event is normally well triggered. It just seems to be no handled.
Sources
DialogFragment.java
NB : in the following, the output "ok" isn't shown, whereas the output displaying the button reference is. It means that the function that sets the click event handler is executed (and that the button is correctly got from the layout), but that the click event handler isn't even when a click is performed.
public class DialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getActivity()));
builder.setView(R.layout.dialog);
View view = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog, null);
Dialog dialog = builder.create();
setCancelButtonListener((Button) Objects.requireNonNull(view.findViewById(R.id.button_cancel)), dialog);
return dialog;
}
private void setCancelButtonListener(Button button, final Dialog dialog) {
System.out.println(button);
button.setOnClickListener( new Button.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("ok");
dialog.cancel();
}
});
}
}
dialog.xml (i.e.: the cancel button in the XML layout of the fragment)
<Button
android:clickable="true"
android:id="@+id/button_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="@drawable/background_button"
android:text="@string/cancel"
android:textColor="@color/colorRoyalRedLight"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_4" />
Question
Why is the button click event not handled? How to make it work?
Solution
Take a look at these two lines from your code:
builder.setView(R.layout.dialog);
View view = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog, null);
Here, you give the resource id of your layout to the AlertDialog.Builder
so it will use it to populate the AlertDialog
's "customizable" area. (You can achieve the same effect by first inflating a View
and then calling AlertDialog.Builder.setView(View)
)
Next, you let LayoutInflater
create a View
by inflating the same layout file once more. This new View
is accessible by a local variable view in onCreateDialog()
but it is never added to any ViewGroup
so it will never actually be shown. Because the Button
contained in this View
can't be clicked, its OnClickListener
will never fire.
So you should set up the AlertDialog
like this:
AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getActivity()));
View view = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog, null);
builder.setView(view);
Dialog dialog = builder.create();
setCancelButtonListener((Button) Objects.requireNonNull(view.findViewById(R.id.button_cancel)), dialog);
return dialog;
Answered By - Bö macht Blau
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.