Search code examples
androidandroid-dialogandroid-popupwindow

Android - Open dialogue builder in fancy way


I have been using android dialog builder by inflating layouts in it. Everything is working fine and perfect. But now, I want to change the way of opening of the dialog builder. I want open the dialog builder like the swipe card. .i.e. From left to right OR top to bottom etc.

I know stackoverflow is not all about asking questions, but showing some effort at least. But issue is, I am not able to find any examples or clues on it. Need some advice, or reference to be followed.

Thanks!

Dialog Opening Code:

   final Dialog dialog = new Dialog(main.this);
    dialog.setContentView(R.layout.prompt_dialoge);
    dialog.setTitle("Draw your signature below");

   Button dialogButton = (Button) dialog.findViewById(R.id.btnOk);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new View.OnClickListener()
    {
       @Override
        public void onClick(View v)
        {
        }
    });
   dialog.show();

Solution

  • Add style to your new Dialog() constructor like below.

    final Dialog dialog = new Dialog(main.this, R.style.DialogStyle);
    dialog.setContentView(R.layout.prompt_dialoge);
    dialog.setTitle("Draw your signature below");
    
    Button dialogButton = (Button) dialog.findViewById(R.id.btnOk);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new View.OnClickListener()
    {
       @Override
        public void onClick(View v)
        {
        }
    });
    dialog.show();
    

    Add this style to your styles.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="DialogStyle" parent="@android:style/Theme.Dialog">
            <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
        </style>
    
        <style name="DialogAnimation">
            <item name="android:windowEnterAnimation">@anim/slide_in_right</item>
            <item name="android:windowExitAnimation">@anim/slide_out_right</item>
        </style>
    </resources>
    

    slide_in_right.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <translate
            android:duration="200"
            android:fillAfter="true"
            android:fromXDelta="100%p"
            android:interpolator="@android:anim/linear_interpolator"
            android:toXDelta="00%p" />
    
    </set>
    

    slide_out_right.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <translate
            android:duration="200"
            android:fillAfter="true"
            android:fromXDelta="000%p"
            android:interpolator="@android:anim/linear_interpolator"
            android:toXDelta="100%p" />
    
    </set>