0

I need to show an AlertDialog with "Don't Show Again" checkbox. I searched, but I couldn't find a working solution :/

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
PackageManager pk = getPackageManager();
Drawable icon;

alertDialogBuilder
    .setTitle(R.string.confirm)
    .setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            //Do something
        }
    });

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
rupinderjeet
  • 2,829
  • 26
  • 48
drozdzynski
  • 1,159
  • 2
  • 11
  • 16

3 Answers3

1

If i'm not wrong then just Make one class extends with View.

public class DialogShow extends View {
SharedPreferences dialogPreferences;
String prefrencestring;
CheckBox nevershowagain;
Button closedialog;
Dialog dialog;
View view;

public DialogShow(final Context context) {
    super(context);
    dialog = new Dialog(context);
    view = View.inflate(context, R.layout.startdialog, null);
    dialog.setContentView(view);
    nevershowagain = (CheckBox) view.findViewById(R.id.nevershowagain);
    closedialog = (Button) view.findViewById(R.id.closedialog);

    closedialog.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (nevershowagain.isChecked()) {
                prefrencestring = "1";

                dialogPreferences = PreferenceManager
                        .getDefaultSharedPreferences(context);
                Editor editprefrences = dialogPreferences.edit();
                editprefrences.putString("showdialog", prefrencestring);
                editprefrences.commit();
            }

            dialog.dismiss();
        }
    });

    dialogPreferences = PreferenceManager
            .getDefaultSharedPreferences(context);
    String check = dialogPreferences.getString("showdialog", "");
    if (check.equals("1")) {
    } else {
        dialog.show();
    }
}

}

Now call this class in your splash Activity on onCreate() method..

     DialogShow  d = new Dialog(this);
Piyush
  • 24,288
  • 6
  • 40
  • 72
  • Why in the world would you call a Dialog a View? That's Confusing. This is overkill for what the guy is trying to do. – JoxTraex Mar 01 '14 at 06:38
1

You can try this for AlertDialog :

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();

builder.setView(inflater.inflate(R.layout.myDialogLayout, null));

Dialog d = builder.create();

Edit: Please look into HERE FOR DETAILS EXPLANATION

Community
  • 1
  • 1
Nitesh Tiwari
  • 4,692
  • 3
  • 27
  • 45
0

Pass your view to the setView() method and it will set your view to the dialog body.

alertDialogBuilder.setView(your_view);
Hamid Shatu
  • 9,503
  • 4
  • 29
  • 40