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. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < RelativeLayout android : id = "@+id/RelativeLayout01&quot;
  3. android : layout_width = "fill_parent"
  4. android : layout_height = "fill_parent"
  5. xmlns : android = "http://schemas.android.com/apk/res/android" >
  6. < TextView android : id = "@+id/TextView01"
  7. android : layout_width = "wrap_content"
  8. android : layout_height = "wrap_content"
  9. android : text = "This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text." >
  10. </ TextView >
  11. < Button android : layout_height = "wrap_content"
  12. android : layout_below = "@+id/TextView01"
  13. android : layout_width = "wrap_content"
  14. android : id = "@+id/Button01main"
  15. android : text = "Hey! There is more..." ></ Button >
  16. </ 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. < ImageView android : id = "@+id/ImageView01"
  5. android : layout_width = "wrap_content" android : layout_height = "wrap_content"
  6. android : layout_centerHorizontal = "true" />
  7. < ScrollView android : id = "@+id/ScrollView01"
  8. android : layout_width = "wrap_content" android : layout_below = "@+id/ImageView01"
  9. android : layout_height = "200px" >
  10. < TextView android : text = "@+id/TextView01" android : id = "@+id/TextView01"
  11. android : layout_width = "wrap_content" android : layout_height = "wrap_content" />
  12. </ ScrollView >
  13. < Button android : id = "@+id/Button01" android : layout_below = "@id/ScrollView01"
  14. android : layout_width = "wrap_content" android : layout_height = "wrap_content"
  15. android : layout_centerHorizontal = "true" android : text = "Cancel" />
  16. </ 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. button1main. setOnClickListener ( new OnClickListener ( ) {
  10. @Override
  11. public void onClick ( View v ) {
  12. //set up dialog
  13. Dialog dialog = new Dialog ( main. this ) ;
  14. dialog. setContentView ( R. layout . maindialog ) ;
  15. dialog. setTitle ( "This is my custom dialog box" ) ;
  16. dialog. setCancelable ( true ) ;
  17. //there are a lot of settings, for dialog, check them all out!
  18. //set up text
  19. TextView text = ( TextView ) dialog. findViewById ( R. id . TextView01 ) ;
  20. text. setText ( R. string . lots_of_text ) ;
  21. //set up image view
  22. ImageView img = ( ImageView ) dialog. findViewById ( R. id . ImageView01 ) ;
  23. img. setImageResource ( R. drawable . nista_logo ) ;
  24. //set up button
  25. Button button = ( Button ) dialog. findViewById ( R. id . Button01 ) ;
  26. button. setOnClickListener ( new OnClickListener ( ) {
  27. @Override
  28. public void onClick ( View v ) {
  29. finish ( ) ;
  30. }
  31. } ) ;
  32. //now that the dialog is set up, it's time to show it
  33. dialog. show ( ) ;
  34. }
  35. } ) ;
  36. }
  37. }

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android系统自带Emoji表情与表情描述互相
  2. LongClick原理、上下文菜单原理、EditTex
  3. Android(安卓)sdk 简单的客户端源代码
  4. Android关于json数据的处理JSONObject、G
  5. android定时定位 - 利用百度定位API来实
  6. Android高德地图使用之地点关键词的输入
  7. Android(安卓)组件化实现
  8. Android中批处理drawable-xxx目录中图片
  9. Android(安卓)Studio 和 Unity 之间实现
  10. Android开发的第一个例子(内附Sdk、Androi