3

As above.

I have got custom Dialog (which I suppose should be extended by different class...)

public class ResetPasswordDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_lost_password, null, false);
        ButterKnife.bind(this, v);

        final AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.change_password)
                .setCancelable(false)
                .setView(v)
                .setPositiveButton(R.string.change_password, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //
                    }
                })
                .create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface arg0) {

                Button okButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
                okButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO: check if password is correct
                    }
                });
            }
        });
        return dialog;
    }
}
y07k2
  • 1,898
  • 4
  • 20
  • 36

2 Answers2

1

You can create a method on OneActivityClass. like

public static void showDialog()
{
// YOUR CUSTOM DIALOG CODE HERE

}

then you can call where you want. like

 OneActivityClass.showDialog();

Assume MyActivity have,

public void showDialog(String title,String message)
    {
        Builder builder = new Builder(global_context);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
 //this is not custom dialog // intead of this method you can put your custom Dialog code

then call to you MainActivity like,

MyActivity.showDialog("YOUR TITLE","YOUR MESSAGE");

This may helps you.

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
  • But what if I want to dynamically show `Dialog` in `MainActivity.java`? How can I call this class to show it? – y07k2 Jun 03 '16 at 11:07
  • Ok, that helps. But I've got one more question: Can I have `CustomDialog` java class and call dialog from that class in e.g. `MainActivity.java` (and how if it is possible) cause I'd liike to have all Custom Dialogs in package `dialogs`? – y07k2 Jun 03 '16 at 11:33
  • you can call that dialog into where you want – Sathish Kumar J Jun 03 '16 at 11:38
0

I guess dialog.show(); is missing.

See the following: http://android-developers.blogspot.com/2012/05/using-dialogfragments.html

Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73