Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating simple confirmation dialog on button press - Android

Tags:

android

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
like image 419
steelthunder Avatar asked Dec 19 '22 17:12

steelthunder


2 Answers

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();
like image 175
Chirag Ghori Avatar answered May 22 '23 02:05

Chirag Ghori


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();
like image 34
Sanket Shah Avatar answered May 22 '23 02:05

Sanket Shah