Create custom alert dialog box in android example

Alert dialog box in android with ok and cancel button.

Alert dialog box is a type of custom pop up window that shows a particular application message with two different type of buttons Yes, No( OK, Cancel ). OK( Yes ) button is used to go on next further window and Cancel( No ) button is applied to cancel the alert dialog box so user can stay on current activity screen. So here is the complete step by step tutorial for Create custom alert dialog box in android example.

android-project-download-code-button

Create custom alert dialog box in android example.

Code for MainActivity.java file.

 package com.android_examples.com.customalertdialogbox;

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

public class MainActivity extends Activity {

 Button btnOpenCustomAlertDialogBox;
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 btnOpenCustomAlertDialogBox = (Button)findViewById(R.id.button1);
 
 btnOpenCustomAlertDialogBox.setOnClickListener(new View.OnClickListener()
 {
 @Override
 public void onClick(View v) 
 {
 new AlertDialog.Builder(MainActivity.this)
 .setIcon(R.drawable.ic_launcher)
 .setTitle("Alert Dialog Box Title")
 .setMessage("Are you sure( Alert Dialog Message )")
 .setPositiveButton("OK", new DialogInterface.OnClickListener()
 {
 @Override
 public void onClick(DialogInterface dialog, int which) 
 {
 Toast.makeText(MainActivity.this, "You Clicked on OK", 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();
 }
 }).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.android_examples.com.customalertdialogbox.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="173dp"
 android:text="CLICK HERE TO OPEN DIALOG BOX " />

</RelativeLayout>

Screenshots:

open dialog box

Create custom alert dialog box in android example

clicked on cancel

Click Here To Download Create custom alert dialog box in android example project.

Leave a Reply

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