AlertDialog Android

To display the dialog message with buttons like OK, Yes, No and Cancel, the AlertDialog is used in Android. To interrupt to ask the user about the choice to continue or discontinue, the AlertDialog is used. There are three regions: title, content area, and action buttons in Android AlertDialog which is a subclass of Dialog class.

Methods of AlertDialog class:

Method Uses
public AlertDialog.Builder setTitle(CharSequence) To set the title of AlertDialog.
public AlertDialog.Builder setMessage(CharSequence) To set the message for AlertDialog.
public AlertDialog.Builder setIcon(int) To set the icon over AlertDialog.

Android AlertDialog Example:

activity_main.xml:(File: activity_main.xml)

In this example, we are adding only a text view, but we can add multiple components.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="MainActivity">
 
   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/button"
       android:text="Close app"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent" />
 
</android.support.constraint.ConstraintLayout>

strings.xml:

The dialog message and title can be stored in the strings.xml file.

<resources>
   <string name="app_name">radioapp</string>
   <string name="dialog_message">Welcome</string>
   <string name="dialog_title">Hello World</string>
</resources>

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we are adding the code to create and show the AlertDialog.

package com.example.radioapp;
 
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
   Button closeButton;
   AlertDialog.Builder builder;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
 
       closeButton = (Button) findViewById(R.id.button);
       builder = new AlertDialog.Builder(this);
       closeButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
 
               //Uncomment the below code to Set the message and title from the strings.xml file
               builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);
 
               //Setting message manually and performing action on button click
               builder.setMessage("Do you really want to close the application?")
                       .setCancelable(false)
                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                               finish();
                               Toast.makeText(getApplicationContext(),"The application is closed.",
                                       Toast.LENGTH_SHORT).show();
                           }
                       })
                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                               //  Action for 'NO' Button
                               dialog.cancel();
                               Toast.makeText(getApplicationContext(),"you choose no action for alertbox",
                                       Toast.LENGTH_SHORT).show();
                           }
                       });
               //Creating dialog box
               AlertDialog alert = builder.create();
               //Setting the title manually
               alert.setTitle("Alert Message");
               alert.show();
           }
       });
   }
}

Output 1:

Output 2:

Please follow and like us:
Content Protection by DMCA.com