An Image as Alert Dialog
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.
<FrameLayout android:id="@+id/body" android:layout_width="fill_parent"
android:padding="5px" android:layout_marginTop="5px" >
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);
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:
And we call the ShowDialog(IMAGE_DIALOG) method (Remember from first paragraph?).