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"
-
android:
layout_width=
"fill_parent"
-
android:
layout_height=
"fill_parent"
-
xmlns:
android=
"http://schemas.android.com/apk/res/android"
>
-
-
<
TextView android:
id=
"@+id/TextView01"
-
android:
layout_width=
"wrap_content"
-
android:
layout_height=
"wrap_content"
-
android:
text=
"This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text."
>
-
</
TextView>
-
-
<
Button android:
layout_height=
"wrap_content"
-
android:
layout_below=
"@+id/TextView01"
-
android:
layout_width=
"wrap_content"
-
android:
id=
"@+id/Button01main"
-
android:
text=
"Hey! There is more..."
></
Button>
-
-
</
RelativeLayout>
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"
-
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"
-
android:
layout_height=
"200px"
>
-
-
<
TextView android:
text=
"@+id/TextView01"
android:
id=
"@+id/TextView01"
-
android:
layout_width=
"wrap_content"
android:
layout_height=
"wrap_content"
/>
-
-
</
ScrollView>
-
-
<
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"
/>
-
-
</
RelativeLayout>
Now that the xml part is all set up, it's time to code.
-
public
class
main extends
Activity {
-
@Override
-
public
void
onCreate(
Bundle savedInstanceState)
{
-
super
.onCreate
(
savedInstanceState)
;
-
//set up main content view
-
setContentView(
R.layout
.main
)
;
-
//this button will show the dialog
-
Button
button1main
=
(
Button
)
findViewById
(
R.
id
.
Button01main
)
;
-
-
button1main.setOnClickListener
(
new
OnClickListener(
)
{
-
@Override
-
public
void
onClick
(
View
v
)
{
-
//set up dialog
-
-
dialog.setContentView
(
R.layout
.maindialog
)
;
-
dialog.setTitle
(
"This is my custom dialog box"
)
;
-
dialog.setCancelable
(
true
)
;
-
//there are a lot of settings, for dialog, check them all out!
-
-
//set up text
-
TextView text =
(
TextView)
dialog.findViewById
(
R.id
.TextView01
)
;
-
text.setText
(
R.string
.lots_of_text
)
;
-
-
//set up image view
-
ImageView img =
(
ImageView)
dialog.findViewById
(
R.id
.ImageView01
)
;
-
img.setImageResource
(
R.drawable
.nista_logo
)
;
-
-
//set up button
-
Button
button
=
(
Button
)
dialog.
findViewById
(
R.
id
.
Button01
)
;
-
button.setOnClickListener
(
new
OnClickListener(
)
{
-
@Override
-
public
void
onClick
(
View
v
)
{
-
finish(
)
;
-
}
-
}
)
;
-
//now that the dialog is set up, it's time to show it
-
dialog.show
(
)
;
-
}
-
}
)
;
-
}
-
}

分享到:
相关推荐
The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...
you'll need to install a Windows keyboard hook if you want to get keyboard input to your dialog without other controls getting in the way. (19KB)<END><br>46,treestore.zip Tree views are capable of...
This chapter covers four different ways to play sound, how to display waveforms, and how to embed MP3 support in your own programs. Chapter 11, Native Integration and Packaging The best software ...
start your Visual Basic program with your project code showing, right click and you should see "Rem Builder".<END><br>3 , syntax.zip This is an excellent example of how to highlight HTML code in a ...
(156KB)<END><br>90,Dialog2.zip A step by step tutorial showing how to create your first windows program using MFC(112KB)<END><br>91,MyMDIApp.zip A brief step-by-step tutorial that demonstrates ...
stopwatch.zip This shows how to count off time in a Stop Watch format.<END><br>31 , taskhide.zip This will hide your application from the taskbar, Alt+Tab, and Alt+Ctrl+Del.<END><br>32 , newbie...
1 , vb5dialog.zip This demonstrates how to subclass the Common Dialog Dialogs and manipulate a specific Dialog.<END><br>2 , cpnl.zip Form_Taskbar is a control for Visual Basic which, once placed...
whether you want to clear the log, in order to avoid from clearing the log file by mistake. * Version 1.91: o Added 'Beep On New Ports' option. * Version 1.90: o Added 'Tray Balloon On New Ports...
The Ribbon bar Application ("main") Button can display a text label instead of icon in the "scenic" mode (usually "File" in English language UI). The following new methods were added to the ...
demo.zip How to send debugging output to a console in a MFC application(12KB)<END><br>56,statuslog.zip A very simple text logger that allows you to use printf-type formatting, with automatic ...
This specification was developed in response to a perceived need for a standardized programming inter-face to digitizing tablets, three dimensional position sensors, and other pointing devices by a ...
labelblink.zip Blinks The Caption In A Label Box<END><br>22,UnZipper.zip ActiveX control designed to easily manipulate ZIP archives from within your application. <END><br>23,gurhancoolbuttonocx....
The add-in offers a transparent view of the protection results, allowing developers to see how the code has been altered and to make any necessary adjustments. Additionally, developers can insert ...
This tutorial aims to provide a detailed introduction to the core concepts and techniques involved in using the Win32 API within the C and C++ programming languages. It covers various aspects such as ...
- Attach a test project where the problem could be reproduced (it helps us to solve your issue as soon as possible)Typically AidAim Software Support Team answer messages in 24 hours, but depending on...
ICS V9 is in early development and is planned to support Android. There are no current plans for ICS for iOS. Version Control repository: --------------------------- svn://svn.overbyte.be/ics or ...
扩展型多尺寸多色彩工具条(226KB)<END><br>15,15.zip How to display tooltips for a toolbar in a dialog 在对话框如何为工具条显示工具提示(3KB)<END><br>16,16.zip Displaying text on a Toolbar ...