4

i want to add shadow effect in alert dialog box . i want this type of shadow effect in my dialog box here i post 3 files first is style.xml second is theme.java or third file is demo_bg.xml file.i already try like that

Style.xml 
 <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/bg_color</item>
        <item name="android:textColorPrimary">@color/colorBlack</item>
        <item name="android:background">@drawable/demo_bg</item>

    </style>

theme.java

final AlertDialog.Builder builder =
                new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
        builder.setMessage(R.string.wsdialogdata);
        builder.setPositiveButton("START", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent ii = new Intent(WelcomeFromActivity.this, Question1Activity.class);
                startActivity(ii);
            }
        });
        builder.show();



demo_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <!-- SHADOW LAYER -->
            <item android:left="2dp" android:top="2dp">
                <shape>
                    <solid android:color="#66000000" />
                    <corners android:radius="35dip" />
                </shape>
            </item>
            <!-- CONTENT LAYER -->
            <item android:bottom="4dp" android:right="4dp">
                <shape>
                    <solid android:color="@color/btn_fb" />
                    <corners android:radius="40dip" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>
Developer_99
  • 135
  • 1
  • 7
  • 17

3 Answers3

3

I have created custom shadow file (shadow.xml) below

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle"> <!-- this shape is used as shadow -->
            <padding android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp"/>
            <solid android:color="#44000000"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle"> <!-- this is for dialog frame -->
            <corners android:radius="5dp"/>
            <stroke android:color="#ff272727" android:width="2dp" />
            <gradient android:angle="90"
                android:startColor="#ffa7a7a7"
                android:centerColor="#ff6a6a6a"
                android:endColor="#ffa7a7a7"
                android:type="linear"/>
        </shape>
    </item>
</layer-list>

In the next step you should change your dialog theme as what is in the following:

<style name="dialog_theme">
     <item name="android:windowBackground">@drawable/shadow</item>
 </style>

Now you are done, just create a new instance of the Dialog class and apply this theme to it (in Dialog constructor):

Dialog dialog = new Dialog(this, R.style.dialog_theme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.mdialog);
dialog.show();
Pranav Darji
  • 744
  • 3
  • 13
3

create background_shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape>
            <padding android:bottom="1dp" />
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:bottom="1dp" />
            <solid android:color="#10CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:bottom="1dp" />
            <solid android:color="#20CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:bottom="1dp" />
            <solid android:color="#30CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:bottom="1dp" />
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>

    <item>
        <shape>
            <padding android:right="1dp" />
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:right="1dp" />
            <solid android:color="#10CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:right="1dp" />
            <solid android:color="#20CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:right="1dp" />
            <solid android:color="#30CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:right="1dp" />
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="@color/white" />
        </shape>
    </item>
</layer-list>

style

 <style name="AppCompatAlertDialogStyle" parent="android:Theme.Dialog">
        <item name="android:windowBackground">@drawable/background_shadow</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

Show dialog

Dialog dialog = new Dialog(mContext, R.style.AppCompatAlertDialogStyle);
builderSingle.setTitle(getString(R.string.select_quantity));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.layout);
dialog.show();

Hope this will solve your problem

Rahul Devanavar
  • 3,917
  • 4
  • 32
  • 61
2

I know I'm too late. However it might help others who are in need and who want to achieve this in a simple way. Just add this to your parent layout in xml.

android:background="@android:drawable/dialog_holo_light_frame"

for example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@android:drawable/dialog_holo_light_frame"
          android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:drawablePadding="10dp"
    android:drawableStart="@drawable/filter"
    android:gravity="center"
    android:text="Filter"
    android:textColor="@color/grey"
    android:textSize="18sp" />

</LinearLayout>

So here LinearLayout is your parent. so adding,

android:background="@android:drawable/dialog_holo_light_frame"

will do it.

Hope it helps. Thanks.

Vijay E
  • 829
  • 10
  • 14
  • hey there thanks for this simple answer, but there is an issue in my case actually I have rounded corners so there is some white space around the corners, in my dialog java file I have added this line of code which helped me before getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); but after implementing your method the issue comes up again can you help me with that – Vasant Raval Nov 05 '21 at 09:26