Android AlertDialog with CheckBox

For my old project YahrtzeitZmanim I needed to add start-up alert box, which will announce new version of Yahrtzeit2 app.

For these purposes, I added the following code to onCreate function:

AlertDialog.Builder adb=new AlertDialog.Builder(TabView.this);
	    LayoutInflater adbInflater = LayoutInflater.from(TabView.this);
	    View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
	    dontShowAgain = (CheckBox)eulaLayout.findViewById(R.id.skip);
	    adb.setView(eulaLayout);
	    adb.setTitle("Attention");
	    adb.setMessage(Html.fromHtml("This version is going to be depricated soon. 
" + "Get Yahrtzeit2 in case you want to store your own Yahrtzeit dates
" + "Do you want to get Yahrtzeit2?")); adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String checkBoxResult = "NOT checked"; if (dontShowAgain.isChecked()) checkBoxResult = "checked"; SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("skipMessage", checkBoxResult); // Commit the edits! editor.commit(); startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=com.mb.yartzeit2"))); return; } }); adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String checkBoxResult = "NOT checked"; if (dontShowAgain.isChecked()) checkBoxResult = "checked"; SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("skipMessage", checkBoxResult); // Commit the edits! editor.commit(); return; } }); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); String skipMessage = settings.getString("skipMessage", "NOT checked"); if (!skipMessage.equalsIgnoreCase("checked") ) adb.show();

and before onCreate function some pre-requisites:

public static final String PREFS_NAME = "MyPrefsFile1";
	public CheckBox dontShowAgain;

Additionally, I needed to create layout for checkbox.xml


    
    

As a result, I got the following alert box:

13 comments

  1. Hi,
    Thank you for outlining it.
    Seems it is the problem of the plugin, which highlights the code. Original code is correct.

  2. Pingback: Yarrow Answers

Leave a Reply

%d bloggers like this: