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
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With