What is alertdialog

Android alert dialog is dialog that is display in graphical user interface. It display when something wrog or unexpected that required immidate user action.

It display with two buttons yes or no that the user click on button for continue or discontinue.


AlertDialog:.

In this section i am going to show the AlertDialog with practical example.

First open the android studio and create a project and give the name of your project like AlertDialogDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the one Button. Open the activity_main.xml file and code look like this


<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="com.example.niitpc.myapplication.MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="203dp"
        android:onClick="displayAlert"
        android:text="alertDialog" />
</RelativeLayout>

       

Open the MainActivity.java file and write code look like this.

Here in the program you click on the button it will display the alert dialog when you click on ok it will close the app if you click on no it will continue .


package com.example.niitpc.myapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


}
public void displayAlert(View v)
{
  AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
  builder.setMessage("Do you want to close app")
 .setCancelable(false)
 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
 finish();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.setTitle("alert !!!");
alert.show();
}

}



Explanation:

displayAlert(View v):First of all i have created a method displayAlert.

AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this):This statement create the object builder of the AlertDialog.Builder.

setMessage("Do you want to close app"):This method Set the message to display that you want to display in alert dialog.

setCancelable(false):This method sets whether the dialog is cancelable or not..

setPositiveButton():This method set a listener to be invoked when the positive button of the dialog is pressed.

setNegativeButton():This method set a listener to be invoked when the negative button of the dialog is pressed.

In my case i am calling the finish() within the setPositiveButton() this will close the app.

In setNegativeButton() i am calling the dialog.cancle() this will cancel the dialog operation.


alertdialog

What is AlertDialog in android for video click here

Smiley face                     Smiley face