Question

I created a custom dialog, to create one standard dialog that i could create in just one line later one and it would be standard. With the parameters I change the text. The dialog is very simplistic and has only one button.

Now I am rethinking of this was a good idea. I actually want my app to stop until my dialog is displaying. How could i manage that? Give the dialog a return type? Or is there a better way?

my dialog:

/**
 * custom dialog
 * 
 * @param mcontext use activityname.this
 * @param title
 * @param text
 * @param button
 */
 public void showDialog(Context mcontext, String title,String text, String button) {
     // fonts
     Typeface tf_hn = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneue.ttf");
     Typeface tf_hn_bold = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneuebd.ttf");
     Resources res = mcontext.getResources();

     // custom dialog
     final Dialog dialog = new Dialog(mcontext);
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //not the normal dialog title
     dialog.setContentView(R.layout.view_dialog);

     TextView tv_dialog_title = (TextView) dialog.findViewById(R.id.tv_dialog_title);
     tv_dialog_title.setText(title);
     tv_dialog_title.setTypeface(tf_hn_bold);
     tv_dialog_title.setTextColor(res.getColor(R.color.white));

     TextView tv_dialog_text = (TextView) dialog.findViewById(R.id.tv_dialog_text);
     tv_dialog_text.setText(text);
     tv_dialog_text.setTypeface(tf_hn);
     tv_dialog_text.setTextColor(res.getColor(R.color.white));

     Button dialogButton = (Button) dialog.findViewById(R.id.bt_dialog_button);
     dialogButton.setTypeface(tf_hn_bold);
     dialogButton.setText(button);
     dialogButton.setTextColor(res.getColor(R.color.white));
     // if button is clicked, close the custom dialog
     dialogButton.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) { 
             dialog.dismiss();
         }
     });

     dialog.show();
}

and then i can use it like this:

dialogH.showDialog(LoginActivity.this, res.getString(R.string.txt_dialog_fout), res.getString(R.string.txt_dialog_not_connected),res.getString(R.string.txt_dialog_button));

It worked all fine, until i wanted to show a dialog with "you are logged in" (or so) and then start a intent after the display was clicked away. Anyone an idea?

Was it helpful?

Solution

Create a custom dialog class with a built in listener like so.

public class MyDialog extends Dialog {
    String title;
    String text;
    String button;

    DialogListener listener;

    interface DialogListener {
        void onCompleted();

        void onCanceled();
    }

    public MyDialog(Context context, String title, String text, String button) {
        super(context);
        this.title = title;
        this.text = text;
        this.button = button;
    }

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

        Typeface tf_hn = Typeface.createFromAsset(getContext().getAssets(), "helveticaneue.ttf");
        Typeface tf_hn_bold = Typeface.createFromAsset(getContext()..getAssets(), "helveticaneuebd.ttf");
        Resources res = getContext().getResources();

        requestWindowFeature(Window.FEATURE_NO_TITLE); // not the normal dialog title
        setContentView(R.layout.view_dialog);

        TextView tv_dialog_title = (TextView) findViewById(R.id.tv_dialog_title);
        tv_dialog_title.setText(title);
        tv_dialog_title.setTypeface(tf_hn_bold);
        tv_dialog_title.setTextColor(res.getColor(R.color.white));

        TextView tv_dialog_text = (TextView) findViewById(R.id.tv_dialog_text);
        tv_dialog_text.setText(text);
        tv_dialog_text.setTypeface(tf_hn);
        tv_dialog_text.setTextColor(res.getColor(R.color.white));

        Button dialogButton = (Button) findViewById(R.id.bt_dialog_button);
        dialogButton.setTypeface(tf_hn_bold);
        dialogButton.setText(button);
        dialogButton.setTextColor(res.getColor(R.color.white));
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.onCompleted();
                MyDialog.this.dismiss();
            }
        });

        setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                if(listener != null)
                    listener.onCanceled();
            }
        });
    }   public void setDialogListener(DialogListener listener) {
        this.listener = listener;
    }

}

And to implement the dialog:

    MyDialog dialog = new MyDialog(getContext(), title, text, button);
    dialog.setDialogListener(new MyDialog.DialogListener() {

        @Override
        public void onCompleted() {
            // do stuff when dialog is completed
        }

        @Override
        public void onCanceled() {
            // do stuff when dialog is cancelled
        }
    });
    dialog.show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top