Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To create a customized switch background that resembles a shadow in Android, you can try the following steps:

  1. Create a new drawable resource file in your project (e.g. "switchbackgroundshadow.xml").
  2. Inside the drawable resource file, use a shape element to create a rectangle shape with rounded corners. Set the "solid" color to the background color you want your switch to have.
  3. Use a layer list to add another shape element inside the rectangle shape. This time, use a rectangle shape with the same rounded corners as the previous one, but set the "gradient" color to a transparent color. This should create a transparent rectangle that sits on top of the solid-colored rectangle.
  4. Add a stroke element to the rectangle shape with a color that matches the solid-colored rectangle. This should create a border around the entire shape.
  5. Save and close the file, and then use this XML file as the background for your switch in your layout file.

Here's an example of what the "switchbackgroundshadow.xml" file might look like:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <solid android:color="#EDEDED" />
            <stroke android:color="#EDEDED" android:width="2dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <gradient
                android:startColor="@android:color/transparent"
                android:endColor="@android:color/transparent"
                android:angle="90" />
        </shape>
    </item>
</layer-list>

This should create a switch background that has a subtle shadow effect, with a solid-colored rectangle and a transparent rectangle sitting on top of it. The stroke element creates a border around the entire shape to give it some extra depth.