Skip to main content

How to display Android Alert Dialog

In this article let us see how to display Android Alert Dialog or Popup window with a message to the user that shows “Do you want to quit the application?” with one button says “Yes” and other says “No”.

What is Android Dialogs?

A dialog is really a small window which requests the user to make a choice. A dialog will not fill up the display screen and it is used for modal activities which require users to take an action before they are able to proceed.

How to Convert Any Website into Android Application
How to install Android x86 on Vmware Workstation

Activity_main.xml:

<?xmlversion=”1.0″encoding=”utf-8″?>
<RelativeLayoutxmlns: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.sysadminview.web.myapplication.MainActivity”>

<Button
android:id=”@+id/buttonclk”
android:layout_width=”368dp”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:text=”Quit”
android:layout_marginTop=”50dp”/>

</RelativeLayout>

How to use putExtra() and getExtra() in Intent Android
How to use Explicit / Implicit android intents

As I said early, the below javascript will display a popup window having a message to your user that shows “Do you want to quit the application?” with “Yes” and “No” button, if user click “No” no action will take place, else if user click “Yes” your application will close gracefully.

MainActivity.java:


package com.sysadminview.web.myapplication;

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

public class MainActivity extends AppCompatActivity {

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

public void quitbutton() {
b1 = (Button)findViewById(R.id.buttonclk);
b1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder a_quit = new AlertDialog.Builder(MainActivity.this);
a_quit.setMessage(“Do you want to quit?”).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 = a_quit.create();
alert.setTitle(“Alert !!!”);
alert.show();

}
}
);

}
}


I hope this article provides you the information about, How to display Android Alert Dialog. Thank you for studying!!. Be Social and share it in social media, if you really feel worth sharing it.

Leave a Reply

Your email address will not be published. Required fields are marked *