0

I created a custom dialog fragment to be used as a YesNo Dialog Box for my project as shown below

here is the image

I want to use it dynamically in where I can use it not only for one xml file. Does anybody know how can I set the button click listener in the appcompact class from the dialogfragment on call?

I just want to set the buttonclick event of the dialogfragment in another xml file.

I tried using interface but it gives me a null pointer exception so I tried the code below.

the YesNoDialogFragment Class

public class DialogYesNo extends DialogFragment {

LayoutInflater inflater;
View v;


@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

    inflater = getActivity().getLayoutInflater();
    v = inflater.inflate(R.layout.dialog_yesno,null);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v);
    return builder.create();
}
}

the XML file of the DialogFragment

 <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/dialog_background"
android:layout_gravity="center">

<TextView
    android:id="@+id/yesno_title"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="TITLE"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:textSize="10pt"
    android:background="#1b1b01"
    android:textColor="#fff"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="7dp"
    android:paddingLeft="10dp"
    android:textStyle="bold"/>

<View
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:background="?android:attr/listDivider" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="#520456">

    <TextView
        android:id="@+id/yesno_message"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Message"
        android:textAllCaps="true"
        android:textSize="12dp"
        android:textColor="#fff"
        android:paddingLeft="10dp"
        android:textStyle="bold"/>

</ScrollView>

<View
    android:id="@+id/divider1"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:background="?android:attr/listDivider" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:gravity="center">

    <Button
        android:id="@+id/dialog_No"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="NO"
        android:background="@drawable/dialog_background"
        android:textColor="#000"
        android:textStyle="bold"
        android:textSize="10pt"/>
    <Button
        android:id="@+id/dialog_Yes"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="YES"
        android:layout_marginLeft="8dp"
        android:background="@drawable/dialog_background"
        android:textColor="#000"
        android:textStyle="bold"
        android:textSize="10pt"/>
</LinearLayout>

this is the code in the MainActivity

                btnYes = dialogYesNo.getActivity().findViewById(R.id.dialog_Yes);
                btnNo = dialogYesNo.getActivity().findViewById(R.id.dialog_No);

                btnYes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this,TutwithNavigation.class);
                        startActivity(intent);
                    }
                });

                btnNo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    // code here

                    }
                });
shizhen
  • 12,251
  • 9
  • 52
  • 88

1 Answers1

0

try findViewById in your DialogYesNo, and add setListener for it like this:

private OnClickListener onYesClick;
private OnClickListener onNoClick;
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    inflater = getActivity().getLayoutInflater();
    v = inflater.inflate(R.layout.dialog_yesno,null);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v);
    View btnYes = v.findViewById(R.id.dialog_Yes);
    View btnNo = v.findViewById(R.id.dialog_No);               
    btnYes.setOnClickListener(onYesClick);
    btnNo.setOnClickListener(onNoClick);
    return builder.create();
}
}
public void setOnYesClick(OnClickListener listener){
    onYesClick = listener
}
public void setOnNoClick(OnClickListener listener){
    onNoClick = listener
}

use setOnYesClick and setOnNoClick instead

Elivish
  • 1
  • 1
  • thanks but there is a problem with that approach.setting onclick listener is good but it leaves the dialog box open after click .. – Gerome Tapales Jan 31 '19 at 05:49
  • what event can I use to check if the button on the dialog fragment has been clicked ? since getDialog.cancel() will not be available to use after setting onClickListener ? – Gerome Tapales Jan 31 '19 at 06:19