Android Dialog

| Page Views: 73

Let's expore this Dialog class.

This class is the base class for all dialogs in android and was added in Android API 1.0.

It's a public class that extends the java.lang.Object.

This class implements 4 interfaces:

  1. DialogInterface.
  2. Window.Callback.
  3. KeyEvent.Calllback.
  4. View.OnCreateContextMenuListener.

The Dialog class resides in the package.

Some of the direct subclasses of this class include:

No. SubClass Type SubClass
1. AlertDialog Direct
2. AppCompatDialog Direct
3. CharacterPickerDialog Direct
4. MediaRouteChooserDialog Direct
5. Presentation Direct
6. AlertDialog Indirect
7. BottomSheetDialog Indirect
8. DatePickerDialog Indirect
9. MediaRouteControllerDialog Indirect
10. ProgressDialog Indirect
11. TimePickerDialog Indirect

Dialog HowTo examples

1. How to create a custom progress dialog.

Here's an example class that shows how to create a custom dialog. This is a dialog that can be used for loading.

Obviously you start by extends the class. You override two constructors, one taking a context object only, the other a context object and a theme integer.

Here's the full code:

import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jkapp.R;

 * Load dialog with animation
public class CustomLoadDialog extends Dialog {
     private Animation animation;
     private static ImageView imageView ;
     private static CustomLoadDialog dialog;
    private static Handler mHandler = new Handler();
    public CustomLoadDialog(Context context) {

    public CustomLoadDialog(Context context, int theme) {
        super(context, theme);

     * Called when the window focus changes
    public void onWindowFocusChanged(boolean hasFocus) {
         imageView = (ImageView) findViewById(;
         animation = AnimationUtils.loadAnimation(getContext(), R.anim.spinner);


     * @param message
    public void setMessage(CharSequence message) {
        if (message != null && message.length() > 0) {
            TextView txt = (TextView) findViewById(;

     * @param context
     * @param message
     * @param cancelable
     * @param cancelListener
     * @return
    public static CustomLoadDialog show(Context context, CharSequence message, boolean cancelable, OnCancelListener cancelListener,int resId) {
        dialog = new CustomLoadDialog(context,;
//      dialog.setContentView(R.layout.progress_custom);
        if (message == null || message.length() == 0) {
        } else {
            TextView txt = (TextView) dialog.findViewById(;
        // Press the back button to cancel
        // Listening back key handling
        // Set centered
        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        // Set background layer transparency
        lp.dimAmount = 0.2f;
        // dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

        if(!((Activity) context).isFinishing())
            //show dialog
        return dialog;

    public  static void dismisDialog(){
            dialog= null;



Top Android Dialog Examples

Let's look at some examples.

An AlertDialog is a subclass of a Dialog that can display one, two or three buttons.

AlertDialogs don't require any layout.

AlertDialog was added in Android API level 1.

It resides in the package.


AlertDialog derives from the class:

public class AlertDialog 
extends Dialog..{}

AlertDialog implements the DialogInterface interface:

public class AlertDialog 
extends Dialog implements DialogInterface

In this class we see how to show an alert dialog with buttons. AlertDialog allows us display simple dialogs without us creating any layout.

Our alert dialog in this case will have two buttons: a positive button and a negative button.

Android AlertDialog with List Tutorial.

We will see how to render list items in an alert dialog in android. Java is our programming language.

First understand that AlertDialog is a subclass of the class and allows us show simple messages, buttons and even lists in our...

Look the aim is simple.To load images from our File System and into our GridView.Maybe we can do that via a loop but see we want control.We want to select images we load,not just loop through and load all images,though we have such a tutorial as well.

So we shall be using a FilePicker,a material filepicker third party library to help us choose images with a nice material interface.Moreover we can define the total number of images users can choose at a go. We then display these images in a GridView.

Mobile phones are nowadays quite powerful and an important part of our day to day lives. Be it in keeping our todo lists, sending some emails or taking selfies, we pretty much find it difficult to leave our phones behind. One common usage of phones of course is taking selfies via the camera.

These images get stored in the external storage of our devices. What about if we want to use these images in our app? What about enabling dear users select images in an easy way? Yes, that’s the purpose of this session, to allow users easily select images from the external storage of their devices to a listview. We make use of a FilePicker library by droidninja.

Users will be able to select images and show them alongside the images name ina ListView with cardviews.

This is an android filesystem tutorial. Our aim is to see how to load images from external storage, particularly from the downloads folder into our ListView.

User clicks a button and we load those images into our ListView.

In this tutorial we see how to load images from extrnal storage into a custom ListView. We will render the images alongside the relative paths of spacecraft images.

Those images are located in the downloads directory of our device or emulator.

To load the individual images we use Picasso, probably the most popular image loader library out there. We will simply loop through that downloads folder and hold the files in a File array.

In android there are various storage options for users to persist application data. Normally android apps create and work with app data. Obviously critical data can be persisted in sqlite database or fetched via web services. However, temporary data can just be stored in the filesystem of the device. Android devices come with external storage that can be used to save and retrieve files.

Be aware that external storage can either be removable storage such as SD card or internal non-removable storage. Usually external storage is alwasy a shared storage. This means the data there can be edited by the user or other applications. Suppose a user removes the external storage, it becomes unavailable to your app. It can also be unavailable when mounted to a computer via, say a USB cable.

Android comes default with an alert dialog.Thats good since majority of simple stuff it can asily display,in maybe 4-5 lines of code.But when it comes custom views,it falls short flatly.Luckily its just as easier to display a custom view as a dialog.Its what we show here,using custom layout without any third party libraries.

Android Material FilePicker tutorial - Pick Multiple images and show in a RecyclerView.


Android FilePicker tutorial. We show how to pick multiple images using Material FilePicker and show them in a RecyclerView. Please watch the video tutorial step by step for explanations.

This is a simple example to examine how to create a custom dialog with a custom layout and render it on button click.

Kotlin Android Dialog List Tutorial.

Best Regards, Oclemy.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post