Here, I am going to discuss about custom alert dialog box in android application using Android Studio.

An alert dialog box is a small window that prompts some information to the user to make a decision or to get some additional information. Alert dialog android is used to show the message with a positive(OK) and Negative(Cancel) buttons. It is just used to provide and ask the user about their choice to continue or discontinue.

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from templates to implement alert dialog box in android .

1. Create a button to your main layout file :

We create a button to your main layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dialog"/>

</LinearLayout>

2. Create a custom layout for Alert Dialog :

Here we create an ImageView and TextView for custom Alert Dialog view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/head"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="@color/colorAccent"
        android:text="TechNxt Code Labs"
        android:textAlignment="center"
        android:textColor="#fff"
        android:textStyle="bold"
        android:textSize="30sp"/>
    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="375dp"
        android:src="@drawable/patientcare"/>
</LinearLayout>

3. Now open your MainActivity.java and add following code :

Now we create a custom Alert Dialog view in android  .

package com.technxtcodelabs;

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

public class MainActivity extends AppCompatActivity {
    Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Create a Custom Alert Dialog
                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = getLayoutInflater();
                View view = inflater.inflate(R.layout.list_layout,null);
                TextView tv = (TextView)view.findViewById(R.id.head);
                ImageView iv = (ImageView)view.findViewById(R.id.iv);
                builder.setView(view);
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Dismiss the dialog here
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Add ok operation here
                    }
                });

                builder.show();


            }
        });

    }
}

Now run the project/app to test custom Alert Dialog in android app through android device or any Emulator .

%d bloggers like this: