Home » Android » core » view » Customized dialog

About Ilias Tsagklis

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

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:

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar

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

  Subscribe  
Notify of