Creating and Displaying a Custom Dialog in Android

In this post I would like to describe the process of creating and displaying a custom dialog. Though the Android documentation describes pretty well the topic, I faced some problems implementing it, and I would like to share my findings.

The final output should look like this:

Android Custom Dialog

1. The first issue I have encountered was that I was getting a BadTokenException in the onCreateDialog() method where I was instantiating the Dialog: android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application, 

Context context=getApplicationContext();
Dialog dialog=new Dialog(context);

Well, though the Android documentation suggests to use getApplicationContext(); actually it turns out that this is not the proper way to do it and most likely it will throw an exception. The correct way is to use this, or “ActivityName”.this instead of getApplicationContext(). For example:

Context context=MainActivity.this;
Dialog dialog=new Dialog(context);


2. The second issue I was facing was that I wanted to get rid off the standard dialog title.
Normally you would set the Dialog title with this:

dialog.setTitle("Dialog Title");

However, if you don’t want to display a standard title for your dialog, not calling the above line most probably won’t meet your expectations, because it will leave an empty space where the title should be.

Hopefully, this is an easy fix. Just call requestWindowFeature(Window.FEATURE_NO_TITLE); and you are done.



To sum up with a concrete working example, below I presented an implementation of a simple custom dialog.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
   android:orientation="vertical" >

   android:text="Custom Dialog"
   android:textSize="18dp" />

   android:text="Restart Game" />




public class MainActivity extends Activity {

Dialog dialog;

public void onCreate(Bundle savedInstanceState) {
   // ............

protected Dialog onCreateDialog(int id) {
   switch (id) {
     Context context=MainActivity.this;
     dialog=new Dialog(context);


     Button restart=(Button)dialog.findViewById(;

     restart.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {

        //whatever code you want to execute on restart

   default: break;
   return dialog;

22 thoughts on “Creating and Displaying a Custom Dialog in Android

  1. Nice, I like the way you cut down the code into smaller parts. Andy, you are my top liker on my blog,, and I have decided to give you the Harry Potter Giveaway that’s happening on my blog. I’ll email you.

  2. Hi,
    Which syntax highlighter are you using to write code into your post ? is it comes as plugin?
    I have free blog, how would i achieve the same as yours?

    1. The syntax highlighter plugin is built in the wordpress.
      [ sourcecode lang=”java” ]
      //java code
      [ /sourcecode ]

      (Without spaces at the beginning and closing brackets)

  3. Good work. I am designing a view engine on top of a .net wcf service. Will post it when it’s working for comments. This implementation might just make it into it

      1. can u please make a sample for that question i really need solution for that problem i want the background of the behind activity not to become dark i want it as it is but the focus will be with the dialog please make the sample😀

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s