fastread homefastrread library fastread menu

Android : AlertDialog

Tutorial by:      Date: 2018-01-23 03:55:28

❰ Previous Next ❱

Android AlertDialog can be utilized to show the exchange message with OK and Cancel catches. It can be utilized to hinder and get some information about his/her decision to proceed or suspend. 

Android AlertDialog is made out of three districts: title, content territory and activity catches. 

Android AlertDialog is the subclass of Dialog class.

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

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity" >  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerHorizontal="true"  
        android:layout_centerVertical="true"  
        android:text="@string/hello_world" />  

RelativeLayout>  

strings.xml

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

File: strings.xml

xml version="1.0" encoding="utf-8"?>  
<resources>  
    <string name="app_name">alertdialogstring>  
    <string name="hello_world">Hello world!string>  
    <string name="menu_settings">Settingsstring>  
    <string name="dialog_message">Welcome to Alert Dialogstring>  
   <string name="dialog_title">Javatpoint Alert Dialogstring>  
resources>  

Activity class

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

File: MainActivity.java

package com.example.alertdialog;  

import android.os.Bundle;  
import android.app.Activity;  
import android.app.AlertDialog;  
import android.content.DialogInterface;  
import android.view.Menu;  

public class MainActivity extends Activity {  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        AlertDialog.Builder builder = new AlertDialog.Builder(this);  
        //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 want to close this application ?")  
            .setCancelable(false)  
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int id) {  
                finish();  
                }  
            })  
            .setNegativeButton("No", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int id) {  
                //  Action for 'NO' Button  
                dialog.cancel();  
             }  
            });  
        //Creating dialog box  
        AlertDialog alert = builder.create();  
        //Setting the title manually  
        alert.setTitle("AlertDialogExample");  
        alert.show();  
        setContentView(R.layout.activity_main);  
    }  

    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu);  
        return true;  
    }  
} 

❰ Previous Next ❱