How to display a custom dialog in your Android application


SDK Version: 
M3

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:
1

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <RelativeLayout android:id="@+id/RelativeLayout01&quot;
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. xmlns:android="http://schemas.android.com/apk/res/android">
  7.  
  8. <TextView android:id="@+id/TextView01"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text.">
  12. </TextView>
  13.  
  14. <Button android:layout_height="wrap_content"
  15. android:layout_below="@+id/TextView01"
  16. android:layout_width="wrap_content"
  17. android:id="@+id/Button01main"
  18. android:text="Hey! There is more..."></Button>
  19.  
  20. </RelativeLayout>

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  android:layout_width="wrap_content" android:layout_height="wrap_content">
  4.  
  5. <ImageView android:id="@+id/ImageView01"
  6.  android:layout_width="wrap_content" android:layout_height="wrap_content"
  7.  android:layout_centerHorizontal="true" />
  8.  
  9.  <ScrollView android:id="@+id/ScrollView01"
  10.  android:layout_width="wrap_content" android:layout_below="@+id/ImageView01"
  11.  android:layout_height="200px">
  12.  
  13.  <TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
  14.  android:layout_width="wrap_content" android:layout_height="wrap_content" />
  15.  
  16.  </ScrollView>
  17.  
  18.  <Button android:id="@+id/Button01" android:layout_below="@id/ScrollView01"
  19.  android:layout_width="wrap_content" android:layout_height="wrap_content"
  20.  android:layout_centerHorizontal="true" android:text="Cancel" />
  21.  
  22. </RelativeLayout>

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

  1. public class main extends Activity {
  2.     @Override
  3.     public void onCreate(Bundle savedInstanceState) {
  4.         super.onCreate(savedInstanceState);
  5.         //set up main content view
  6.         setContentView(R.layout.main);
  7.         //this button will show the dialog
  8.         Button button1main = (Button) findViewById(R.id.Button01main);
  9.  
  10.         button1main.setOnClickListener(new OnClickListener() {
  11.         @Override
  12.             public void onClick(View v) {
  13.                 //set up dialog
  14.                 Dialog dialog = new Dialog(main.this);
  15.                 dialog.setContentView(R.layout.maindialog);
  16.                 dialog.setTitle("This is my custom dialog box");
  17.                 dialog.setCancelable(true);
  18.                 //there are a lot of settings, for dialog, check them all out!
  19.  
  20.                 //set up text
  21.                 TextView text = (TextView) dialog.findViewById(R.id.TextView01);
  22.                 text.setText(R.string.lots_of_text);
  23.  
  24.                 //set up image view
  25.                 ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
  26.                 img.setImageResource(R.drawable.nista_logo);
  27.  
  28.                 //set up button
  29.                 Button button = (Button) dialog.findViewById(R.id.Button01);
  30.                 button.setOnClickListener(new OnClickListener() {
  31.                 @Override
  32.                     public void onClick(View v) {
  33.                         finish();
  34.                     }
  35.                 });
  36.                 //now that the dialog is set up, it's time to show it    
  37.                 dialog.show();
  38.             }
  39.         });
  40.     }
  41.  }

2

You can read more about the topic, here.

Comments

Thanks for the code it helped a lot! movies to mp4 player/

I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.Any way Ill be subscribing to your feed and I hope you post again soon...moles on skin

I was very encouraged to find this site. I wanted to thank you for this special read. I definitely savored every little bit of it and I have you bookmarked to check out new stuff you post.
Tablet Android Honeycomb Terbaik Murah

Good post. Very impressive. Thanks for sharing.
wenger backpack

Very interesting articles. Good work done
batik fabrics

Nice WORK !!!

Pull Up banner

how to download Android Apps... anyone can help me? Mercelo

i love Motorola Android. i want to upgrade... how to? Arron

i appreciate the efforts that has spent in this blog. i love Android. Howard

any news, Android updated? Roselia

Android is the best. Ryan

Hey, Really great work, I would like to join your blog anyway so please continue sharing with us,

http://dir.allnutri.com/skin_store.html

nashville barbers This is such a great resource that you are providing and you give it away for free. I truly loved reading your post. Thanks!

Hi, great .. !! this is really a great post .. informative also helpful for me ..

Custom Stickers||Stickers Printing||Banners Printing

Took me a while to read all the comments but I really love the article. It proved to be very useful to me and I am sure to all the commenters here!It's always nice when you can not only be informed but also entertained!I'm sure you had fun writing this article.Comfortably the article is really the sweetest on this precious topic.
dmaa

I can not recommend you guys enough. Full of useful resource and great layout very easy on the eyes. Please do keep up this great work.investment

This is a great blog posting and very helpful. I really appreciate the research you put into this blog.

Surf Fanatics

I want to express my admiration of your writing skill and ability to make
reader to read the while thing to the end. I would like to read more of
your blogs and to share my thoughts with you. I will be your frequent visitor, that’s for sure. Gerogia palatino

Thanks for this tip. It's very cool and nice.

Nikmaya John

Friendzone

Very useful tutorial
Thanks

--------------
MonaVie

Thanks for this great information. I can use it right away on my android download movies

I’ve seen progression in every post. Your newer posts are simply wonderful compared to your posts in the past. Keep up the good work.
Amateurporno Videos

It's my first time visiting here. i'm so glad with this post... great article and helpful for me and maybe to anyone...
nice to meet you...
about bali
lowongan
lowongan kerja jakarta

valuable suggestion about display a custom dialog. low cost car insurance

I am glad to catch idea from your article. It has information I have been searching for a long time. This looks absolutely perfect. All these tinny details are made with lot of background knowledge. I like it a lot. Breast Enlargement Cream

Man you're truly an expert in terms of this. Many thanks for helping folks like us. cityville guide

It's my first-time visiting here. I came across so many helpful stuff within your blog particularly the on going conversation. From the tons of comments on your posts, I suppose I'm not the only one enjoying reading through your blog. Keep up the excellent work. body pillow

Hey,I am looking for this information for a long time..I am very glad to find and visit your site..and read this valuable information..Thank you so much.

mens health

Hi, ask Permission to Blogwalking here.. Looking for some information about anything, that could increase my knowledge. I get one here. Thank you for this very interesting article.
free classified ads

i am a Android developer, this helped me a lot. mariage

This valuable editorial was very useful to read, I savored it completely. I'm about now to email it to my collegues to permit them examine this too. Thank you really bancuri bula

Thanks for the instructions. Very well written and easy to understand, thanks. warts on hands

I do agree with all the ideas you have presented in your post. Theyre really convincing and will definitely work. Still, the posts are very short for novices. Could you please extend them a little from next time? Thanks for the post
wordpress themes
ubuntu

The development process of android based applications is something that also enjoyable in the eyes of many developers and this is something good in the eyers of customers who are going to have the benefit of the end products. Hublot

you can display whatewer you want., the way you want it.
First, make your own layout, with the needed elements.

=========================================================

ipad bag blog
Foods And Culinary Arts
short domain names

Thanks for the coding tips. Does this advice still apply with the newer version of android? i'd love to see an updated version of this coding language.electric car conversion

That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.electric car conversion">electric car conversion

I am rattling glad to deed this aggregation real utilizable for me, as it contains lot of information. I always raise to construe the quality content and this occurrence I launch in you post. Thanks a ton for distribution
This is a big help for as a blog poster.well done!orjin krem pembe maskeenergy balance complex 41

Thank you for sharing with us, i like it very much and i will always give attention...
I was surfing the net and luckily found this site and find interesting things here. I would like to thank you for the efforts you have made in writing this article, it's very fun to read. I really enjoyed....
Mercedes-Benz Mobil Mewah Terbaik Indonesia
Mercedes-Benz Mobil Mewah Terbaik Indonesia
Tablet Android Honeycomb Terbaik Murah
Meriahkan pesta ulang tahun bersama GarudaFood
Mari Berkomunitas Di Faceblog
Harga Jual Blackberry iPhone Laptop Murah
Video Music
Mp3
Beauty and Health

That is very interesting - but is it always applicable? LED Lenser - I think that each situation is different and sometimes generalising like that might not always be applicable to each situation.

WOW!! thank you so much... it work

The great numbers of fans of android based applications should already a good indicators for them to be able to prepare with more and more good applications to download and use. Business Website Design

There are so many independent and formal developers of android based applications continue their endeavours to find the best possible solution for them to create more and more applications and this is something that is good for the users. custom stickers

This is another good sample on how easy and free the android based application to handle. The beauty of freedom in developing any possible applications in this open source platform is unbearable. deta fukkyuu

World of attention has been switched to Asia these days, and this is including music industry. His achievement here shows how this thing is realy true. I want to make sure this is happening. perguntas