1

I have defined my button in the xml like this.

  <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:text="@string/button_node"
    android:textColor="@android:color/white"
    android:backgroundTint="@color/colorAccent" />

I need to grey out this button in the code. I have used the below line of code to change it.

button.setBackgroundResource(R.drawable.button_grey);
button.setEnabled(false);
button.setClickable(false)

My button_grey.xml file is

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:angle="90"
                android:endColor="@color/colorGray"
                android:startColor="@color/colorGray" />
            <corners android:radius="4dp" />
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="1.5dp"
        android:right="0.5dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/cardview_shadow_start_color" />
            <corners android:radius="4dp" />
        </shape>
    </item>
</layer-list>

After changing the buton looks like this enter image description here

But Actually I need the button to be look like below by changing the color to grey.enter image description here Please note:- It should work from API level 18 onwards.

somia
  • 611
  • 5
  • 22
  • Haven't you just set the button background as grey in your code? If you want the button to be blue, with a shadow you can set the background as blue and give it an elevation. – Rachit Feb 22 '17 at 12:16
  • Elevation requires minimum API level 21 – somia Feb 22 '17 at 12:19
  • http://stackoverflow.com/a/30931750/3286614 Use this to set an elevation below level 21. – Rachit Feb 22 '17 at 12:20

1 Answers1

1

Try this it will work

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item android:right="5dp" android:top="5dp">
                <shape>
                    <corners android:radius="3dp" />
                    <solid android:color="#D6D6D6" />
                </shape>
            </item>
            <item android:bottom="2dp" android:left="2dp">
                <shape>
                    <gradient android:angle="270"
                        android:endColor="#24b8eb" android:startColor="#24b8eb" />
                    <stroke android:width="1dp" android:color="#BABABA" />
                    <corners android:radius="4dp" />
                    <padding android:bottom="10dp" android:left="10dp"
                        android:right="10dp" android:top="10dp" />
                </shape>
            </item>
        </layer-list>
    </item>

</selector>

http://mobilenext.net/4-material-design-tweaks-pre-lollipop-android-devices/

http://www.techrepublic.com/article/android-lollipop-material-design-trick-offers-a-more-polished-ux/

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96