view

Customized dialog

In this tutorial we are going to see how can we use Customized dialogs for user interaction in Android. The easiest way to do that is to use the Dialog class.

The basic steps to create a Customized dialog are:

  • Create a new xml Layout file that describes the content of the Dialog box and how it will be rendered
  • use dialog.setContentView method to make the Dialog Box use the new xml file as its layout description
Let’s see the code:

 

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.full_image_layout);
...
final Button closeDialogButton = (Button) dialog.findViewById(R.id.close_full_image_dialog_button);
imageView = (ImageView) dialog.findViewById(R.id.image_view);

closeDialogButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

  dialog.dismiss();
    }
});
final ImageDownloaderTask task = new ImageDownloaderTask();
task.execute(imageUrl);

dialog.show();
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>

    <ImageView 

  android:id="@+id/image_view" 

  android:layout_width="wrap_content"

  android:layout_height="wrap_content" 

  android:layout_gravity="center"

  android:layout_marginLeft="5dip"

  android:layout_marginRight="5dip"

  android:layout_marginTop="5dip"

  android:layout_marginBottom="5dip"

 />

 <Button 

  android:id="@+id/close_full_image_dialog_button" 

  android:layout_width="wrap_content"

  android:layout_height="wrap_content" 

  android:text="@string/close"

  android:layout_weight="0.5"

  android:layout_marginLeft="10dip"

  android:layout_marginRight="10dip"

  android:layout_marginTop="5dip"

  android:layout_marginBottom="5dip"

  android:layout_gravity="center" 
    />

</LinearLayout>

 
This was an example of how to create Customized Dialogs in Android

Related Article:

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button