Sitemap
Faheem unnisa

Faheem unnisa

Data Analytics | Marketing | AI | LIFE | Sql, Python ,Mathmatics, AI , ML , Tableau my Fav https://www.linkedin.com/in/faheemunnisa/

Dialogs In Android

3 min readSep 4, 2018

--

A dialog is a small window that prompts the user to make a decision or enter additional information.

Creating Dialogs in android are easy task . Here we discuss about the various types of Dialogs in android and how to create them.

Get Faheem unnisa’s stories in your inbox

Join Medium for free to get updates from this writer.

You can check out the code here :https://github.com/faheema/DialogsInAndroid

Dialogs in Android

Here is the Dialog Hierarchy in android Looks like. As you can see, Dialog is the base class for dialogs in Android, Its recommended avoiding instantiating Dialog class directly. We use AlertDialog, And its sub-classes to instantiate Dialogs.

Press enter or click to view image in full size

Below is the demo of All Dialogs :

AlertDialog

The simplest form of dialog is AlertDialog in android.

public void showSimpleDialog(View view) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false);
builder.setTitle("AlertDialog Title");builder.setMessage("Simple Dialog Message")
.setPositiveButton("OK!!!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
////
}
})
.setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
;
// Create the AlertDialog object and return it
builder.create().show();
}

List Dialog

public void showListDialog(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setTitle("pick a color")
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

switch (which) {
case 0:
root_layout.setBackgroundColor(Color.RED);
break;
case 1:
root_layout.setBackgroundColor(Color.GREEN);
break;
case 2:
root_layout.setBackgroundColor(Color.BLUE);
break;

}
//Toast.makeText(MainActivity.this," "+which ,Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
Press enter or click to view image in full size

Single Choice Dialog

public void showSingleChoiceDialog(View view) {
AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
builder.setSingleChoiceItems(R.array.single_choice_array, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
builder.create().show();
}

Multi Choice Dialog

public void showMultiChoiceDialog(View view) {
AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
builder.setMultiChoiceItems(R.array.single_choice_array, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {

}
});

Custom Dialog

  1. Create a Custom Layout, In my case I am creating a Login dialog with User Name and Password
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:src="@drawable/dlg_icon"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/username" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>

</LinearLayout>

2. Now you can Attach this layout to the dialog and Also handle the events .

public void showCustomDialog(View view)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// Get the layout inflater
LayoutInflater inflater = MainActivity.this.getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.login_dialog, null))
// Add action buttons
.setPositiveButton("Sign in ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {

}
})
;
builder.create().show();
}

Progress Dialog

Progress Dialogs can be styled in Horizontal and Continuos animation . Here is an example

public void showHProgressDialog(View view)
{

final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setMax(200);
dialog.setTitle("Dialog Title");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

// Update the progress bar
Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
dialog.setProgress(150);
}
});

dialog.show();

}
public void showVProgressDialog(View view)
{ ProgressDialog dialog=new ProgressDialog(MainActivity.this);
dialog.setTitle("Dialog Title");
dialog.setMessage("Please wait while we process");
dialog.show();
}

--

--

Faheem unnisa
Faheem unnisa

Data Analytics | Marketing | AI | LIFE | Sql, Python ,Mathmatics, AI , ML , Tableau my Fav https://www.linkedin.com/in/faheemunnisa/