Search code examples
androidcustom-controlsandroid-custom-viewandroid-alertdialogcustomdialog

Custom alert dialog android


I am working with custom alert dialog in android.

I have followed the link 1 and linke 2.

In my code using these styles.

<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_up_dialog</item>
    <item name="android:windowExitAnimation">@anim/slide_out_down</item>

<!-- Animation for dialog box -->
<style name="DialogSlideAnim" parent="AppBaseTheme">
    <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style> 

From these styles I am getting bordered dialog. But I need like the below.Custom alert dialog

I am using LG Nexus 4 device. What should I have to do to achieve this?


Solution

  • I have some solution. Please look at the example below.

    In style xml use this:

    <resources>
        ....
        <style name="DialogAnimation">
            <item name="android:windowEnterAnimation">@anim/abc_slide_in_bottom</item>
            <item name="android:windowExitAnimation">@anim/abc_slide_out_bottom</item>
        </style>
    
        <style name="DialogSlideAnim">
            <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowFrame">@null</item>
            <item name="android:windowNoTitle">true</item>
        </style>
    </resources>
    

    And dialog layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Male"
                android:id="@+id/male"
                android:layout_marginBottom="-10dp"
                android:layout_gravity="center_horizontal"/>
        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Female"
                android:id="@+id/female"
                android:layout_below="@id/male"
                android:layout_gravity="center_horizontal"/>
        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Cancel"
                android:layout_below="@id/female"
                android:id="@+id/cancel"
                android:layout_gravity="center_horizontal"/>
    </RelativeLayout>
    

    And dialog java file:

    public class YourDialog extends DialogFragment {
    
        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return new Dialog(getActivity(), R.style.DialogSlideAnim);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            final View view = inflater.inflate(R.layout.your_dialog, container, false);
            return view;
        }
    
        @Override
        public void onResume() {
            super.onResume();
            final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            final Window window = getDialog().getWindow();
            window.setGravity(Gravity.BOTTOM);
            lp.copyFrom(window.getAttributes());
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
        }
    }
    

    Result:

    enter image description here