Friday, August 14, 2015

How to use AlertDialog onClickListener in Android

MainActivity.java

package com.cfsuman.androidtutorials;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
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);

                        // Set a title for alert dialog
                        builder.setTitle("Terms.");

                        // Ask the final question
                        builder.setMessage("Are you agree with the terms?");

                        // Set click listener for alert dialog buttons
                        DialogInterface.OnClickListener dialogClickListener
                                = (dialog, which) -> {
                                switch(which){
                                        case DialogInterface.BUTTON_POSITIVE:
                                                // User clicked the Yes button
                                                showToast(
                                                        "Yes button clicked");
                                                break;

                                        case DialogInterface.BUTTON_NEGATIVE:
                                                // User clicked the No button
                                                showToast("No button clicked");
                                                break;
                                }
                        };

                        // Set the alert dialog yes button
                        builder.setPositiveButton("Yes", dialogClickListener);

                        // Set the alert dialog no button
                        builder.setNegativeButton("No",dialogClickListener);

                        AlertDialog dialog = builder.create();
                        // Display the 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>