Custom dialog in Android Poster

Android custom dialog with Example[Downlod]

The custom dialog in android is used to show some alert message to the users of the application.

By default android having the support for the dialog.But to give some different user experience and to perform some customized action we need go for the custom dialog in android.

Dialog

Dialog class is used to create the custom dialog in android.

Know more about dialog please visit:
https://developer.android.com/reference/android/app/Dialog.html


Related Post

Custom font in android example

custom toast android example

Set custom font for webview in android


Steps to implement custom dialog

  • Create Dialog Object.
  • Attach the custom view to dialog
  • Add animation to the dialog.
  • Implement the click listeners for the dialog.

Create Dialog Object

Create the dialog object by using the current class context.

final Dialog dialog;
dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

Attach the custom view to dialog

Create the layout with views you want in the dialog. In my case, I have added single Button and Textview for my reference.

dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright"
    android:padding="20dp"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Alert Dialog"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white" />
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="close" />
</LinearLayout>

By using the setContextView() method you can set layout view to the dialog.

dialog.setContentView(R.layout.dialog_layout);

Add animation to the dialog

Set the animation to the custom dialog.

dialog.getWindow().getAttributes().windowAnimations = R.style.animation;

Here I have developed the new style. Place your styles under res/values/style.xml

<style name="animation">
        <item name="android:windowEnterAnimation">@anim/in_animation</item>
        <item name="android:windowExitAnimation">@anim/out_animation</item>
    </style>

In the style, I have defined my In / Out animation.

in_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<scale  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromXScale="0"
  android:toXScale="1"
  android:fromYScale="0"
  android:toYScale="1"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="400"
  android:fillAfter="true">
</scale>
</set>

out_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<scale  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromXScale="1"
  android:toXScale="0"
  android:fromYScale="1"
  android:toYScale="0"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="400"
  android:fillAfter="true">
</scale>
</set>

Implement the click listeners for the dialog

If you want to dialog cancellable when pressing the back button, set the cancellable to true in the dialog and you can also cancel the dialog by touching outside of the dialog view.
to enable that,

dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(true);

In this example, I have developed two buttons,
One to display the dialog,

btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
            }
        });

By clicking the show dialog button you can see the dialog with the animation. another one is to hide the dialog with the animation,

btn_cancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });

Screenshot

screenshot of custom dialog android example

Conclusion

I hope, Now you can create your own custom dialog based on your requirements. Thanks for the reading. Please provide me your feedback in the comments section.

Download

6 thoughts on - Android custom dialog with Example[Downlod]

Leave a Reply

Your email address will not be published. Required fields are marked *