Farhana Naaz Ansari
Farhana Naaz Ansari

Reputation: 7928

Handle click of custom dialog class with custom view from fragment or activity

In custom dialog class with the custom view, I want to handle click of the button from Activity or fragment, I have created an interface for handling the button click but showing error.

Attempt to invoke interface method 'void com.ymcaspi.util.CustomDialog$DialogInterface.doLogin(com.ymcaspi.util.CustomDialog)' on a null object reference

My dialog class is

public class CustomDialog extends Dialog implements
    android.view.View.OnClickListener {
public Activity activity;
public Button btnYes, btnNo;
CustomDialog customDialog;
public CustomDialog(Activity activity) {
    super(activity);
    this.activity = activity;
}

DialogInterface dialogInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.alert_login);
    btnYes = (Button) findViewById(R.id.btn_yes);
    btnNo = (Button) findViewById(R.id.btn_no);
    btnYes.setOnClickListener(this);
    btnNo.setOnClickListener(this);
}
//in custom adapter class i want to handle click of button from Activity or fragment, I have created a interface for handling button click
//but showing
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_yes:
            customDialog=new CustomDialog(activity);
            dialogInterface.doLogin(customDialog);
            break;
        case R.id.btn_no:
            dismiss();
            break;
        default:
            break;
    }
    dismiss();
}



  public interface DialogInterface{
        void doLogin(CustomDialog dialog);

   }
}

i have implemented this interface in fragment but not working?

Upvotes: 0

Views: 360

Answers (2)

Arash GM
Arash GM

Reputation: 10395

You didn't initialize dialogInterface on your dialog , if you've implemented the interface on your Activity , set your Activity to dialog interface

public CustomDialog(Activity activity,DialogInterface dialogInterface ) {
    super(activity);
    this.activity = activity;
    this.dialogInterface = dialogInterface ;
}

Upvotes: 2

Aaron
Aaron

Reputation: 3894

Here's one example that you can try if you intend to receive callback on your activity from the dialog:

class YourActivity extends Activity implements DialogInterface {

    void showDialog() {
        CustomDialog dialog = // init your CustomDialog
        dialog.setOnLoginClickListener(this);
        dialog.show();
    }

    void doLogin() {
        // Button yes has been clicked, do stuff... 
    }
}

And create a method to assign the listener in your CustomDialog class:

public class CustomDialog extends Dialog implements OnClickListener {

    private DialogInterface dialogInterface;

    public void setOnLoginClickListener(DialogInterface dialogInterface) {
        this.dialogInterface = dialogInterface;
    }
}

Upvotes: 0

Related Questions