y07k2
y07k2

Reputation: 2052

How to show custom Dialog in an Activity?

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;
    }
}

Upvotes: 4

Views: 4668

Answers (2)

Sathish Kumar J
Sathish Kumar J

Reputation: 4335

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.

Upvotes: 1

Ugurcan Yildirim
Ugurcan Yildirim

Reputation: 6132

I guess dialog.show(); is missing.

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

Upvotes: 0

Related Questions