Android custom dialog example
In this tutorial, we show you how to create a custom dialog in Android. See following steps :
- Create a custom dialog layout (XML file).
- Attach the layout to
Dialog. - Display the
Dialog. - Done.
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.
Note
You may also interest to read this custom AlertDialog example.
You may also interest to read this custom AlertDialog example.
1 Android Layout Files
Two XML files, one for main screen, one for custom dialog.
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/buttonShowCustomDialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Custom Dialog" /> </LinearLayout>
File : res/layout/custom.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" /> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FFF" android:layout_toRightOf="@+id/image"/>/> <Button android:id="@+id/dialogButtonOK" android:layout_width="100px" android:layout_height="wrap_content" android:text=" Ok " android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_below="@+id/image" /> </RelativeLayout>
2. Activity
Read the comment and demo in next step, it should be self-explorary.
File : MainActivity.java
package com.mkyong.android; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { final Context context = this; private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.buttonShowCustomDialog); // add button listener button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // custom dialog final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.custom); dialog.setTitle("Title..."); // set the custom dialog components - text, image and button TextView text = (TextView) dialog.findViewById(R.id.text); text.setText("Android custom dialog example!"); ImageView image = (ImageView) dialog.findViewById(R.id.image); image.setImageResource(R.drawable.ic_launcher); Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); // if button is clicked, close the custom dialog dialogButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); } }); } }
3. Demo
Start it, the “main.xml” layout is display.

Click on the button, display custom dialog “custom.xml” layout, if you click on the “OK” button, dialog box will be closed.

Download Source Code
Download it – Android-Custom-Dialog-Example.zip (16 KB)
References
- Android Dialog Javadoc
- Android Dialog example
- Android relative layout example
- Android prompt dialog (custom AlertDialog) example

Very useful example. I tried to do a simpler example myself and initially I wrote it like this:
public class MainActivity extends Activity {
Context mContext = null;
Dialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle(“Custom Dialog”);
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(“Hello, this is a custom dialog!”);
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
dialog.show();
}
}
The result was that the application crashed right after launching. Then I compared it to your example and I saw that the assignment
Context mContext = null;
should be instead
Context mContext = this;
and also the assignment to mContext inside the method becomes no longer necessary. I would be very grateful if someone could explain why is this so. It seems reasonable, though I can’t grasp the full meaning.
Thank you, mkyong!!
To remove the dialog’s title,
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
before you set the layout (with dialog.setContentView(R.layout.education_details_layout);)
Thank you, worked like a charm
Great post! Thank you. I compiled a list of some top resources for creating a custom dialog box in android. I included your post. Check it out/ feel free to share. http://www.verious.com/board/Giancarlo-Leonio/customizing-dialog-boxes-in-android/ Hope this can be useful to other developers too. :)
Nice tutorial. I have a question. Question is what if I want to return some value to activity from this dialog. Here is my example.
Activity layout file : activity_my_dialog.xml
Dialog Layout File : education_details_layout.xml
Creating custom dialog : Mydialog.java
Main Java File : MyDialogActivity.java
package com.myclass.mydialog; import android.os.Bundle; import android.app.Activity; import android.app.Dialog; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MyDialogActivity extends Activity implements OnClickListener { Button btn; EditText et; Mydialog d; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_dialog); btn = (Button) findViewById(R.id.btn); et = (EditText) findViewById(R.id.etd); d = new Mydialog(this); btn.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_my_dialog, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub if(v == btn) { d.show(); String result = d.result; if(result.equals("")) { Toast.makeText(getBaseContext(), "Action cancelled", Toast.LENGTH_LONG).show(); } else { et.append("\n"+result); } } } }What ever details I am accepting from dialog box needs to be appended in the EditText component in main activity.
Please help me. Thanks in advance.
I got the answer myself. Answer is to add OnDismissListener() to dialog as below:
MyDialogActivity.java
very nice example…
very useful!
thank you!
It will cause the black background,like the http://stackoverflow.com/questions/8117849/remove-black-background-of-custom-dialog?answertab=votes#tab-top
and there have a solution.
Great Tutorial..Worked well….thanks
This is Very easy Tutorial for Beginners Like Me……Thank you very much…….
it don’t run in Android 3.0 (API 11)
oh, I sr ! it work well :)
that was really nice example for beginners. thank you very much.
Perfect, straight to the point!
Hi, mkYong
The section
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
crashed on my app.
But not on your sample code.
What could happened?
Thanks
have you import this? :)
Thank you very much for the post! Just I’m searching for it.
hi thanks a lot….but by using this code,my dialog box is dismissed without clicking ok button .If i click anywhere on window it will be closed …can u tell me the reason???thank u…..
Can you provide any simple example of zoom in,zoom out…in android?
Your website is good examples for beginner.. keep it up..
Thanks very much. With this tutorial I have understood which one was the error made lost me many hours.
Thanksssssss.
Hi could you please tell me how can I add this into my existing application. I understand how to add the xml layout files, but not sure where to add the .java file. Is it under the src folder ? I tried adding your two xml files and the .java file to my app but when I launch the app nothing appears. not even an error message
Hi,
look this: http://stackoverflow.com/questions/5821749/android-crash-in-dialog-show
final Dialog dialog = new Dialog(MainActivity.this);
Viktor
Hi could you please tell me how can I add this into my existing application. I understand how to add the xml layout files, but not sure where to add the .java file. Is it under the src folder ? I tried adding your two xml files and the .java file to my app but when I launch the app nothing appears