Tuesday, March 16, 2021

Android AlertDialog

Android AlertDialog Example

android alert dialog

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.

Android AlertDialog is composed of three regions: title, content area and action buttons.

Android AlertDialog is the subclass of Dialog class.

Methods of AlertDialog class

MethodDescription
public AlertDialog.Builder setTitle(CharSequence)This method is used to set the title of AlertDialog.
public AlertDialog.Builder setMessage(CharSequence)This method is used to set the message for AlertDialog.
public AlertDialog.Builder setIcon(int)This method is used to set the icon over AlertDialog.

Android AlertDialog Example

Let's see a simple example of android alert dialog.

activity_main.xml

You can have multiple components, here we are having only a textview.

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.alertdialog.MainActivity">  
  8.   
  9.     <Button  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/button"  
  13.         android:text="Close app"  
  14.         app:layout_constraintBottom_toBottomOf="parent"  
  15.         app:layout_constraintLeft_toLeftOf="parent"  
  16.         app:layout_constraintRight_toRightOf="parent"  
  17.         app:layout_constraintTop_toTopOf="parent" />  
  18.   
  19. </android.support.constraint.ConstraintLayout>  


strings.xml

Optionally, you can store the dialog message and title in the strings.xml file.

File: strings.xml
  1. <resources>  
  2.     <string name="app_name">AlertDialog</string>  
  3.     <string name="dialog_message">Welcome to Alert Dialog</string>  
  4.     <string name="dialog_title">Javatpoint Alert Dialog</string>  
  5. </resources>  

Activity class

Let's write the code to create and show the AlertDialog.

File: MainActivity.java
  1. package example.javatpoint.com.alertdialog;  
  2.   
  3. import android.content.DialogInterface;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.app.AlertDialog;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.     Button closeButton;  
  13.     AlertDialog.Builder builder;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         closeButton = (Button) findViewById(R.id.button);  
  20.         builder = new AlertDialog.Builder(this);  
  21.         closeButton.setOnClickListener(new View.OnClickListener() {  
  22.             @Override  
  23.             public void onClick(View v) {  
  24.   
  25.                 //Uncomment the below code to Set the message and title from the strings.xml file  
  26.                 builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);  
  27.   
  28.                 //Setting message manually and performing action on button click  
  29.                 builder.setMessage("Do you want to close this application ?")  
  30.                         .setCancelable(false)  
  31.                         .setPositiveButton("Yes"new DialogInterface.OnClickListener() {  
  32.                             public void onClick(DialogInterface dialog, int id) {  
  33.                                 finish();  
  34.                                 Toast.makeText(getApplicationContext(),"you choose yes action for alertbox",  
  35.                                 Toast.LENGTH_SHORT).show();  
  36.                             }  
  37.                         })  
  38.                         .setNegativeButton("No"new DialogInterface.OnClickListener() {  
  39.                             public void onClick(DialogInterface dialog, int id) {  
  40.                                 //  Action for 'NO' Button  
  41.                                 dialog.cancel();  
  42.                                 Toast.makeText(getApplicationContext(),"you choose no action for alertbox",  
  43.                                 Toast.LENGTH_SHORT).show();  
  44.                             }  
  45.                         });  
  46.                 //Creating dialog box  
  47.                 AlertDialog alert = builder.create();  
  48.                 //Setting the title manually  
  49.                 alert.setTitle("AlertDialogExample");  
  50.                 alert.show();  
  51.             }  
  52.         });  
  53.     }  
  54. }  

Output:

android alert dialog example output 1 android alert dialog example output 2

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile