usually your layout is defined in an xml file (in res/layout) and in
your Java-like Android code you can reference it so:
d.setContentView(R.layout.dialog_view);
where d is an instance of AlertDialog.
and dialog_view.xml could contain:
<?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"
>
<!-- android:title="any title" does not work
-->
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="The search is case sensitive:"/>
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"/>
<Button
android:id="@+id/find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dip"
android:text="Find Next" />
<Button
android:id="@+id/cancel_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/find"
android:layout_alignTop="@id/find"
android:text="Cancel" />
</RelativeLayout>
The above answers your direct question but there are a few different
ways to use such a layout.
I know 5 techniques for setting up an AlertDialog (details are below):
1. Dialog parent class: if you need to roll a simple version.
2. AlertDialog plain (without the builder): not recommended but works.
3. AlertDialog.Builder: classic; probably better than the above 2.
4. Using the two Activity methods that are called by Android: great
for persisting AlertDialog instances = better performance.
5. Using an Activity that looks like a Dialog: for fancy stuff.
Details:
1. Dialog parent class: a mix of code and xml layout
Dialog d = new Dialog(this); //*this* is the Activity
Window w = d.getWindow((;
w.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
d.setTitle("Dialog (not alert)");
//your layout defined in an xml file (in res/layout)
d.setContentView(R.layout.dialog_view);
d.show();
2. AlertDialog plain (not recommended but works)
I sometimes use this technique in an uncaughtException method.
AlertDialog error = new AlertDialog(this){
//*this* is the Activity because it needs the Context.
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
};
error.setTitle("Anomaly Detected");
error.setMessage("A possible defect has been detected in "
+getString(R.string.app_name)
+" v. "+getString(R.string.versionString)
+"\nFor support, please write [email protected]");
error.show();
There are more complex versions of this technique but the other
techniques below are better.
3. The AlertDialog.Builder technique: recommended, as are # 4 and 5
below.
AlertDialog.Builder bookmarkDialog = new AlertDialog.Builder(this);
//*this* is the Activity because it needs the Context.
bookmarkDialog.setIcon(android.R.drawable.ic_menu_myplaces);
bookmarkDialog.setTitle("Bookmark "+getString(R.string.app_name));
AlertDialog ad = bookmarkDialog.create();
ad.requestWindowFeature(Window.FEATURE_LEFT_ICON);
//more setup here...
//setup completed:
bookmarkDialog.show();
4. Using Activity methods called by Android: good for persisting
AlertDialog instances
4.1. you override these 2 methods:
onCreateDialog(int yourId)
//called by Android the first time that you call showDialog.
//You may use the AlertDialog.Builder technique in this method.
onPrepareDialog(int yourId, Dialog dialog)
//called by Android each time the dialog is shown.
//You don't create any dialog instance here; use the one given.
//You can do things like
dialog.setMessage("new message with new info");
4.2. and you do this somewhere in your code:
showDialog(yourUniqueDialogId);
4.3. yourUniqueDialogId must be defined by you, usually as a static
field; must be an int.
5. Using an Activity that looks like a Dialog:
<activity android:name=".ActivityDialog1"
android:theme="@android:style/Theme.Dialog">
</activity>
And you manage the Activity like any another Activity.
sm1
On Mar 22, 2:56 am, Lucius Fox <[email protected]> wrote:
> Hi,
>
> How is android's AlertDialog layout define? Is there a layout.xml file
> corresponding to the AlertDialog?
> I have looked at AlertDialog implementation, But i don't figure out
> how that is being layout (e.g. the location of the button, the
> location of text) Where are they defined? And how can I change that?
>
> Thank you.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---