+91-7906818313 / +91-7500893874 Monday - Sunday 10:00am - 06:00pm
How to Create Alert Dialog Box in Android

How to Create Alert Dialog Box in Android

How to Create Alert Dialog Box in Android

If you want to create an Alert Dialog Box in android then you should read this article till end. Here you can get all the information about this topic.



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="sumit.net.alert_dialog.MainActivity">

    <Button
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:id="@+id/btn1"
        android:text="exit"
        android:textSize="22dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="89dp"
        android:layout_marginTop="193dp" />

</RelativeLayout>




MainActivity.java

package sumit.net.alert_dialog;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
Button b1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button)findViewById(R.id.btn1);
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("game")
                .setMessage("do you want surf this page")
                .setPositiveButton("Yes",null)
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent i=new Intent(MainActivity.this,Splash.class);
                startActivity(i);
            }
        }).show();
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                    .setTitle("Exit")
                        .setMessage("do you want exit")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).setNegativeButton("No",null)
                        .show();


            }
        });
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("game")
                .setMessage("dO you want exit this application")
                .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        moveTaskToBack(true);

                    }
                }).setNegativeButton("no",null)
                .show();

    }
}




OUTPUT

dialog box in android alert dialog box in android

Leave a Reply

Close Menu
Translate »