Home > Android > How to display a custom dialog in your Android application

How to display a custom dialog in your Android application

How to display a custom dialog in your Android application

Yesterday Jozsi showed you, how to make an alert dialog, today I’m going to show you, how to make a custom dialog/popup window.
Sometimes, it’s better to make your own dialog, because this way, you can display whatewer you want., the way you want it.
First, make your own layout, with the needed elements. Here, I’m going to use two buttons, a textview inside a scrollview, and an imageview…

Here is my main layout, main.xml. It’s just a textview, with a button:

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout android:id=”@+id/RelativeLayout01&quot;
<TextView android:id=”@+id/TextView01″
android:text=”This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text.”>
<Button android:layout_height=”wrap_content”
android:text=”Hey! There is more…”></Button>

Here is my dialog’s layout, maindialog.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
 android:layout_width=”wrap_content” android:layout_height=”wrap_content”>
<ImageView android:id=”@+id/ImageView01″
 android:layout_width=”wrap_content” android:layout_height=”wrap_content”
 android:layout_centerHorizontal=”true” />
 <ScrollView android:id=”@+id/ScrollView01″
 android:layout_width=”wrap_content” android:layout_below=”@+id/ImageView01″
 <TextView android:text=”@+id/TextView01″ android:id=”@+id/TextView01″
 android:layout_width=”wrap_content” android:layout_height=”wrap_content” />
 <Button android:id=”@+id/Button01″ android:layout_below=”@id/ScrollView01″
 android:layout_width=”wrap_content” android:layout_height=”wrap_content”
 android:layout_centerHorizontal=”true” android:text=”Cancel” />

Now that the xml part is all set up, it’s time to code.

public class main extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        //set up main content view
        //this button will show the dialog
        Button button1main = (Button) findViewById(R.id.Button01main);
        button1main.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //set up dialog
                Dialog dialog = new Dialog(main.this);
                dialog.setTitle(“This is my custom dialog box”);
                //there are a lot of settings, for dialog, check them all out!
                //set up text
                TextView text = (TextView) dialog.findViewById(R.id.TextView01);
                //set up image view
                ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
                //set up button
                Button button = (Button) dialog.findViewById(R.id.Button01);
                button.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                //now that the dialog is set up, it’s time to show it


You can read more about the topic, here.

From: http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application

Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: