Create AlertDialog box with Yes NO Cancel button in android

How to show cancel button at the left side of alert dialog it shows three buttons.

In this tutorial we are creating a alert dialog with three different buttons Yes, No and Cancel. Cancel button is present at left side of alert dialog and used to cancel the alert dialog at current state. So here is the complete step by step tutorial for Create AlertDialog box with Yes NO Cancel button in android.

android-project-download-code-button

How to Create AlertDialog box with Yes NO Cancel button in android.

Code for MainActivity.java file.

 package com.alertdialogwithyesnocancel_android_examples.com;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 TextView textview;
 Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button =(Button)findViewById(R.id.button1);
 textview = (TextView)findViewById(R.id.textView1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 
 new AlertDialog.Builder(MainActivity.this)
 
 .setIcon(R.drawable.ic_launcher)
 
 .setTitle("Alert Dialog Box Title")
 
 .setMessage("Are you sure( Alert Dialog Message )")
 
 .setPositiveButton("YES", new DialogInterface.OnClickListener()
 {
 @Override
 public void onClick(DialogInterface dialog, int which) 
 {
 Toast.makeText(MainActivity.this, "You Clicked on Yes", Toast.LENGTH_SHORT).show();
 }
 })
 .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
 {
 @Override
 public void onClick(DialogInterface dialog, int which) 
 {
 Toast.makeText(MainActivity.this, "You Clicked on Cancel", Toast.LENGTH_SHORT).show();
 }
 })
 .setNeutralButton("NO", new DialogInterface.OnClickListener()
 {
 @Override
 public void onClick(DialogInterface dialog, int which) 
 {
 Toast.makeText(MainActivity.this, "You Clicked on NO", Toast.LENGTH_SHORT).show();
 }
 })
 .show();
 }
 });
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 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.alertdialogwithyesnocancel_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="169dp"
 android:text="ALERT DIALOG STATUS"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="15dp"
 android:text="OPEN ALERT DIALOG" />

</RelativeLayout>

Screenshot:

Create AlertDialog box with Yes NO Cancel button in android

Click here to download Create AlertDialog box with Yes NO Cancel button in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *