Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Dialog from another class

I am beginner to android, Instead of writing code repeatedly for dialog in each activity, I just created one class which contains all the methods to show the dialog, i have given small code snippet

public class Dialogues extends Activity implements DialogueMethods {


    public void showAlertDialog(Context context, String title, String message) {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);
        alertDialog.show();
}

    //this method am calling

    public void showAlertDialog(Context context, String title, String message, String ButtonText, boolean cancel) {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);

        if(cancel) {

            alertDialog.setNegativeButton(ButtonText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                finish();
            }
        });
        }

        alertDialog.show();
    }

}

Am calling

//dialogObj is instance of the above class

dialogObj.showAlertDialog(MyActivity.this, "Error", "Not Connected to Internet", "Exit", true);

When I run the code dialog is visible but button isn't, is this because of DialogInterace.onClickListener?, I just want to know is this good idea to do like this?, if it is then what's the correct way to do. please help me.

thanks.

like image 667
TraceIt Avatar asked Mar 11 '23 01:03

TraceIt


1 Answers

Just i want to share my way to you how i'm using.You can make class like and then call where you want.

public class DialogsUtil {

private Context mContext;

public DialogsUtil(Context context) {
    this.mContext = context;
}

/**
 * Return an alert dialog
 *
 * @param message  message for the alert dialog
 * @param listener listener to trigger selection methods
 */
public void openAlertDialog(Context context, String message, String positiveBtnText, String negativeBtnText,
                            final OnDialogButtonClickListener listener) {

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setPositiveButton(positiveBtnText, (dialog, which) -> {
        dialog.dismiss();
        listener.onPositiveButtonClicked();

    });

    builder.setNegativeButton(negativeBtnText, (dialog, which) -> {
        dialog.dismiss();
        listener.onNegativeButtonClicked();

    });
    builder.setTitle(context.getResources().getString(R.string.app_name));
    builder.setMessage(message);
    builder.setIcon(android.R.drawable.ic_dialog_alert);
    builder.setCancelable(false);
    builder.create().show();
}

/**
 * return a dialog object
 * @return
 */
public Dialog openDialog(@LayoutRes int layoutId) {
    Dialog dialog = new Dialog(mContext);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    dialog.setContentView(layoutId);
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    dialog.getWindow().setGravity(Gravity.BOTTOM);
    dialog.setCancelable(true);
    dialog.setCanceledOnTouchOutside(false);
    return dialog;
}
}

and i have created one interface for this dialog,

public interface OnDialogButtonClickListener {

void onPositiveButtonClicked();

void onNegativeButtonClicked();
}

Just implement this interface in your activity where you want to use dialog and with help of class object you can use dialog like this,

 mDialogsUtil.openAlertDialog(YourActivity.this, "text message", "positive button msg", "Negative button msg", this);

you can override these two methods in your acitivty,

@Override
public void onPositiveButtonClicked() {

}

//user clicked cancel.Close the application
@Override
public void onNegativeButtonClicked() {

}

Thanks hope this will help you.

like image 72
Saveen Avatar answered Apr 19 '23 22:04

Saveen