Breaking

Monday 24 December 2018

Alert Dialog Tutorial With Example In Android Studio | Android Solution | Alert Dialog Tutorial


Alert Dialog Tutorial With Example In Android Studio

Alert Dialog in an android UI prompts a little window to settle on choice on versatile screen. Once in a while before making a choice it is required to give an alert to the client without moving to next action. To take care of this issue alert dialog came into training. For example you have seen this sort of alert when you endeavor to leave the App and App request that you confirm exiting.



AlertDialog.Builder Components Used In Alert Dialog


AlertDialog.Builder is utilized to make an interface for Alert Dialog in Android for setting like alert title, message, image, button, button onclick usefulness and so on.

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

1.setTitle(CharSequence title) – This segment is utilized to set the title of the alert dialog. It is optional part.

  // Setting Alert Dialog Title
        alertDialogBuilder.setTitle("Confirm Exit..!!!");

2. setIcon(Drawable symbol) – This part include symbol before the title. You should spare image in drawable symbol.

// Icon Of Alert Dialog
        alertDialogBuilder.setIcon(R.drawable.question);

3. setMessage(CharSequence message) – This part shows the required message in the alert dialog.

// Setting Alert Dialog Message
        alertDialogBuilder.setMessage("Are you sure,You want to exit");

4. setCancelable(boolean cancelable) – This segment has boolean esteem i.e genuine/false. Whenever set to false it permits to drop the dialog box by clicking on territory outside the dialog else it permits.

alertDialogBuilder.setCancelable(false);

5. setPositiveButton(CharSequence content, DialogInterface.OnClickListener audience) – This segment include positive button and further with this client confirm he needs the alert dialog question to occur.

alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
 
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                finish();
                            }
                        });

6. setNegativeButton(CharSequence content, DialogInterface.OnClickListener audience) – This segment include negative button and further with this client confirm he doesn't need the alert dialog question to occur.

alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,"You clicked over No",Toast.LENGTH_SHORT).show();
                            }
                        });

7. setNeutralButton(CharSequence content, DialogInterface.OnClickListener audience) – This segment basically include another button and this button engineer can set some other onclick usefulness like drop button on alert dialog.

alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(),"You clicked on Cancel",Toast.LENGTH_SHORT).show();
                            }
                         });

Alert Dialog Example In Android Studio


The following is the example of Alert Dialog in which the usefulness of Alert Dialog is defined over button click. In this example we have utilized a straightforward button and on that button tap the alert dialog window will show up.

Beneath you can download code, see final yield and well ordered clarification of Alert Dialog example in Android Studio.



Stage 1: Create another venture and name it AlertDialogExample. 

Stage 2: Open res - > layout - > activity_main.xml (or) main.xml and include following code: 

Here we include the button UI snap of which alert dialog will show up. We likewise utilized textview for asking client to tap on button.



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.alertdialogexample.MainActivity">
 
    <Button
        android:text="@string/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:onClick="exit"
        android:textStyle="normal|bold" 
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="131dp"/>
 
    <TextView
        android:text="@string/click_over_button_to_exit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="68dp"
        android:id="@+id/textView2"
        android:layout_above="@+id/button"
        android:layout_centerHorizontal="true"
        android:textSize="18sp"
        android:textStyle="normal|bold"
        android:gravity="center" />
</RelativeLayout>


Stage 3 : Now open application - > java - > package - > MainActivity.java and include the underneath code. 

In this progression right off the bat AlertDialog.Builder is utilized to make an interface like setting alert title, message, image, button, button onclick usefulness and so forth. 

Important Note: Add an image to the drawable organizer before setting the alert dialog symbol.


package com.example.alertdialogexample;
 
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.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void exit(View view){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        // Setting Alert Dialog Title
        alertDialogBuilder.setTitle("Confirm Exit..!!!");
        // Icon Of Alert Dialog 
        alertDialogBuilder.setIcon(R.drawable.question);
        // Setting Alert Dialog Message
        alertDialogBuilder.setMessage("Are you sure,You want to exit");
        alertDialogBuilder.setCancelable(false);
 
                alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
 
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                finish();
                            }
                        });
 
                alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,"You clicked over No",Toast.LENGTH_SHORT).show();
                            }
                        });
                alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(),"You clicked on Cancel",Toast.LENGTH_SHORT).show();
                            }
                         });
 
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}


OutPut :-

No comments:

Post a Comment