Saturday, August 15, 2015

How to create an AlertDialog with custom layout/view in Android

MainActivity.java

package com.cfsuman.androidtutorials;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;


public class MainActivity extends Activity {
        private MainActivity mContext;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                // Get the context
                mContext = this;

                // Get the widgets reference from XML layout
                Button button = findViewById(R.id.button);



                // Button click listener
                button.setOnClickListener(v -> {
                        // Build an AlertDialog
                        AlertDialog.Builder builder = new AlertDialog
                                .Builder(mContext);


                        LayoutInflater inflater = getLayoutInflater();
                        View dialogView = inflater.inflate(
                                R.layout.alertdialog_custom_view,
                                null);

                        // Set the custom layout as alert dialog view
                        builder.setView(dialogView);

                        // Custom alert dialog view widgets reference
                        Button buttonYes = (Button) dialogView
                                .findViewById(R.id.buttonYes);

                        Button buttonNo = (Button) dialogView
                                .findViewById(R.id.buttonNo);

                        Button buttonCancel = (Button) dialogView
                                .findViewById(R.id.buttonCancel);


                        // Create the alert dialog
                        AlertDialog dialog = builder.create();


                        // Set positive/yes button click listener
                        buttonYes.setOnClickListener(view -> {
                                // Dismiss the alert dialog
                                dialog.cancel();
                                showToast("Yes button clicked");
                        });


                        // Set negative/no button click listener
                        buttonNo.setOnClickListener(view -> {
                                // Dismiss the alert dialog
                                dialog.cancel();
                                showToast("No button clicked");
                        });


                        // Set cancel/neutral button click listener
                        buttonCancel.setOnClickListener(view -> {
                                // Dismiss/cancel the alert dialog
                                dialog.cancel();
                                showToast("Cancel button clicked");
                        });


                        // Display the custom alert dialog on interface
                        dialog.show();
                });
        }



        // Method to show toast message
        private void showToast(String message){
                Toast.makeText(
                        mContext,message,Toast.LENGTH_SHORT).show();
        }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="32dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show AlertDialog"
        android:textAllCaps="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
alertdialog_custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#F8F8F8"
    android:padding="12dp">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Terms."
        android:textStyle="bold"
        android:textSize="19sp"
        android:fontFamily="sans-serif"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textSize="17sp"
        android:fontFamily="sans-serif"
        android:text="Are you agree with the terms?"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvTitle" />

    <Button
        android:id="@+id/buttonYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Yes"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvMessage" />

    <Button
        android:id="@+id/buttonNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="No"
        app:layout_constraintBottom_toBottomOf="@+id/buttonYes"
        app:layout_constraintEnd_toStartOf="@+id/buttonYes" />

    <Button
        android:id="@+id/buttonCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>