Reputation: 438
I am creating a simple stopwatch app and have a button that will stop the chronometer on button press. Very simple see below.
I want to create a confirmation dialog that pops up and asks the user if they are sure they want to stop the chronometer. How can I do this?
public void stopClick (View view){
chronometer.stop(); }
UPDATE:
Thank you. I now have the below code but receive an error. Also posting logs below.
public void stopClick (View view){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.app_name);
builder.setMessage("Do you want to stop ?");
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
chronometer.stop(); // stop chronometer here
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
Log:
03-15 09:21:49.130: I/Process(27748): Sending signal. PID: 27748 SIG: 9
03-15 09:21:53.550: D/AndroidRuntime(28182): Shutting down VM
03-15 09:21:53.550: W/dalvikvm(28182): threadid=1: thread exiting with uncaught exception (group=0x42055898)
03-15 09:21:53.560: E/AndroidRuntime(28182): FATAL EXCEPTION: main
03-15 09:21:53.560: E/AndroidRuntime(28182): java.lang.IllegalStateException: Could not execute method of the activity
03-15 09:21:53.560: E/AndroidRuntime(28182): at android.view.View$1.onClick(View.java:3838)
03-15 09:21:53.560: E/AndroidRuntime(28182): at android.view.View.performClick(View.java:4475)
03-15 09:21:53.560: E/AndroidRuntime(28182): at android.view.View$PerformClick.run(View.java:18784)
03-15 09:21:53.560: E/AndroidRuntime(28182): at android.os.Handler.handleCallback(Handler.java:730)
03-15 09:21:53.560: E/AndroidRuntime(28182): at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 09:21:53.560: E/AndroidRuntime(28182): at android.os.Looper.loop(Looper.java:137)
03-15 09:21:53.560: E/AndroidRuntime(28182): at android.app.ActivityThread.main(ActivityThread.java:5450)
03-15 09:21:53.560: E/AndroidRuntime(28182): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 09:21:53.560: E/AndroidRuntime(28182): at java.lang.reflect.Method.invoke(Method.java:525)
03-15 09:21:53.560: E/AndroidRuntime(28182): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-15 09:21:53.560: E/AndroidRuntime(28182): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-15 09:21:53.560: E/AndroidRuntime(28182): at dalvik.system.NativeStart.main(Native Method)
03-15 09:21:53.560: E/AndroidRuntime(28182): Caused by: java.lang.reflect.InvocationTargetException
03-15 09:21:53.560: E/AndroidRuntime(28182): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 09:21:53.560: E/AndroidRuntime(28182): at java.lang.reflect.Method.invoke(Method.java:525)
03-15 09:21:53.560: E/AndroidRuntime(28182): at android.view.View$1.onClick(View.java:3833)
03-15 09:21:53.560: E/AndroidRuntime(28182): ... 11 more
03-15 09:21:53.560: E/AndroidRuntime(28182): Caused by: java.lang.NullPointerException
03-15 09:21:53.560: E/AndroidRuntime(28182): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
03-15 09:21:53.560: E/AndroidRuntime(28182): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
03-15 09:21:53.560: E/AndroidRuntime(28182): at steel.timer.MainActivity.stopClick(MainActivity.java:56)
03-15 09:21:53.560: E/AndroidRuntime(28182): ... 14 more
Upvotes: 4
Views: 14855
Reputation: 9225
Try this
using this Alert() method Click button
/* this method for stop Alert app */
private void Alert() {
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this);
String titleText = "App Name";
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.BLACK);
SpannableStringBuilder ssBuilder = new SpannableStringBuilder(titleText);
ssBuilder.setSpan(
foregroundColorSpan,
0,
titleText.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
builder.setCancelable(false);
builder.setTitle(ssBuilder);
builder.setMessage("Do you want to stop ?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
chronometer.stop(); // stop chronometer here
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// Create the alert dialog
android.support.v7.app.AlertDialog dialog = builder.create();
// Finally, display the alert dialog
dialog.show();
// Get the alert dialog buttons reference
Button positiveButton = dialog.getButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE);
Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
// Change the alert dialog buttons text and background color
positiveButton.setTextColor(Color.parseColor("#00aff1"));
negativeButton.setTextColor(Color.parseColor("#00aff1"));
}
Upvotes: 0
Reputation: 4231
Use AlertDialog.Builder to ask confirmation from user.
Try this
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.app_name);
builder.setMessage("Do you want to stop ?");
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
chronometer.stop(); // stop chronometer here
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
Upvotes: 12
Reputation: 4382
try below code for creating dialog
AlertDialog.Builder builder = new AlertDialog.Builder(_context);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// Stuff to do
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// Stuff to do
}
});
builder.setMessage("Your_MSG");
builder.setTitle("Warning..");
AlertDialog d = builder.create();
d.show();
Upvotes: 1