I am trying to create a "hint" dialog box that informs the user about enabling GPS on their phone will decrease battery life. I want it to pop-up but have a check box that says: "Do not ask me again".
How do I go about creating this in Android?
Thank you,
Zukky.
AlertDialog.Builder prompt = new AlertDialog.Builder(this);
prompt.setCancelable(false);
prompt.setTitle("Warning");
prompt.setMessage ("HINT: Otherwise, it will use network to find" +
"your location. It's inaccurate but saves on " +
"battery! Switch GPS on for better accuracy " +
"but remember it uses more battery!");
EDIT: Beware! Code duplication ahead. Since I'm no longer developing for Android, I can't refactor the code below.
It sets a value in Android Preferences and checks it to whether it will show the dialog or not.
checkbox.xml in resources/layouts
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp" >
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok please do not show again." >
</CheckBox>
</LinearLayout>
Activity.java
public class MyActivity extends Activity {
public static final String PREFS_NAME = "MyPrefsFile1";
public CheckBox dontShowAgain;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onResume() {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
LayoutInflater adbInflater = LayoutInflater.from(this);
View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String skipMessage = settings.getString("skipMessage", "NOT checked");
dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
adb.setView(eulaLayout);
adb.setTitle("Attention");
adb.setMessage(Html.fromHtml("Zukky, how can I see this then?"));
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);
editor.commit();
// Do what you want to do on "OK" action
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);
editor.commit();
// Do what you want to do on "CANCEL" action
return;
}
});
if (!skipMessage.equals("checked")) {
adb.show();
}
super.onResume();
}
}