Now that we have learned how to organize and launch our Dialogs, it's time to re-shape the content of the AlertDialog. Let's do it with a simple example:
Imagine that in our application we have a photo gallery (we will learn how to create photo galleries in Android with other articles, don't worry though it is very simple!!) at the moment the images are a bit small and we want to see them larger. We could program the “click-on-the-image-to-enlarge” functionality.
So, when we click on one of the images in the gallery it will appear as an AlertDialog with a larger image.
To do this, we need to create a layout with the widgets structure we want to show in the AlertDialog, in our case, we are just going to show an Image so it will be something like this.
<LinearLayout android:id="@+id/layout_image"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout android:id="@+id/body" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/image_view"
android:layout_width="300px" android:layout_height="250px"
android:padding="5px" android:layout_marginTop="5px" >
</ImageView>
</FrameLayout>
</LinearLayout>
and we call it image_dialog.xml for example
Then, in the Android code we have to get this view and create an AlertDialog Object:
imageDialog = new Dialog(TraficoActivity.this);
imageDialog.setContentView(R.layout.image_dialog );
ImageView imageV = (ImageView) imageDialog.findViewById(R.id.image_view);
And we create an ImageView object that will contain the enlarged image.
When we click on the image? Inside the onClickListener defined to the event, we do the following:
imageV.setImageBitmap(whateverImageYouWantToPut);
And we call the ShowDialog(IMAGE_DIALOG) method (Remember from first paragraph?).
That easy!