Creating A Simple AlertDialog

By Kunal Kapoor
16-05-2018
3185

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.



AlertDialogs are of different types and can have multiple parameters involved. In this example we will be showing you how to create a simple alertdialog with three buttons and then perform a background color change based on that.

To create a simplest AlertDialog contating a button and a text based layout( with a title and a message text), follow the following to steps to complete it:

1. Declare an object for AlertDialog.Builder. That object will contatin the properties of the AlertDialog like number of buttons and their properties, title and message text, etc. After decleration, instantiate the object. 

eg AlertDialog.Builder builder=new AlertDialog.Builder(this);

2. Declare the title and message of your alertdialog  : builder.setTitle("Choose a color");

You can add a message text asbuilder.setMessage("Pick one color");


3. Declare the buttons of your AlertDialog and their respective click actions. By default, there are 3 buttons in the library which are provided. Namely, positive button, neutral button and negative button. You can set any number of buttons out of 3 as per your project needs. here we will be declaring all 3 buttons together.

eg to create 3 different buttons

builder.setPositiveButton("Green", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ChangeColorBackground(1);
}
});

builder.setNegativeButton("Yellow", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ChangeColorBackground(2);
}
});

builder.setNeutralButton("Default", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ChangeColorBackground(3);
}
});


The respective methods for click will be declared separately.


4. Lastly you need to declare an object of AlertDialog and instatntiate it with the builder.

eg AlertDialog alertDialog=builder.create();

You can set the properties of the Alertdialog such as setCancelable,etc on alertdialog variable.

eg to set the alertdialog cancellable on back or side press : alertDialog.setCancelable(true);


5. Lastly show the alertdialog by command :  alertDialog.show();


My code:

Overview:




MainActivity.java:

package com.ourcoaching.simplealertdialog;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.solver.widgets.ConstraintAnchor;
import android.support.constraint.solver.widgets.Snapshot;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private Button button;
private AlertDialog alertDialog;
private AlertDialog.Builder builder;

private ConstraintLayout constraintLayout;

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

button=(Button)findViewById(R.id.button);
constraintLayout=(ConstraintLayout)findViewById(R.id.mainActivityLayout);
}

@Override
protected void onResume() {
super.onResume();

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ChangeColorNow();
}
});
}

private void ChangeColorNow() {
builder=new AlertDialog.Builder(this);
builder.setTitle("Choose a color");

builder.setPositiveButton("Green", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ChangeColorBackground(1);
}
});

builder.setNegativeButton("Yellow", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ChangeColorBackground(2);
}
});

builder.setNeutralButton("Default", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ChangeColorBackground(3);
}
});

alertDialog=builder.create();
alertDialog.setCancelable(true);
alertDialog.show();
}

private void ChangeColorBackground(int i) {
if (i==1){
constraintLayout.setBackgroundColor(getResources().getColor(R.color.colorGreen));
}else
if (i==2){
constraintLayout.setBackgroundColor(getResources().getColor(R.color.colorYellow));
}else {
constraintLayout.setBackgroundColor(getResources().getColor(R.color.colorWhite));
}
}
}


activity_main.xml

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/mainActivityLayout"
tools:context="com.ourcoaching.simplealertdialog.MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

android.support.constraint.ConstraintLayout
>



Download the code from GitHub



Related Tutorials