How Create the Exit Dialog-box in Android Studio. | Dialog box | Android Studio


1. Open Android Studio.

2. Create the Dialog Box.

3. Download exit Icon

4. Set Positive and Negative Button.

5.Run the Emulator.


CODE:-

package com.example.theacx;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements Dialbox {


    public void onBackpressed() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Confirm exit");
        alertDialogBuilder.setIcon(R.drawable.ic_exit_to_app_black_24dp);
        alertDialogBuilder.setMessage("Are you Sure want to Exit");
        alertDialogBuilder.setCancelable(false);

        alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"you click on cancel",Toast.LENGTH_LONG).show();
            }
        });
        AlertDialog alertDialog= alertDialogBuilder.create();
        alertDialogBuilder.show();
    }
    };


Comments

Popular posts from this blog

How to install Pip pillow | Install Pip Pillow | Python pip | Python pillow tutorial

Samsung Galaxy F62 | Samsung Galaxy F62 with 6.7-inch FHD+ AMOLED Infinity O Display, Exynos 9825, 7000mAh battery launched in India starting at Rs. 23999

How to Create the Button in Android Studio. | Button | Android Studio.