1

I have a border that I made for a button that should be enabled when the button is clicked on.

<Button
        android:id="@+id/imageView_red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:src="@drawable/border" />

My question is how can I enable/disable this drawable? Does java provide a solution for this?

I tried looking at this post, but it didn't seem to answer that question

Android - border for button

Andy Chan
  • 133
  • 7

1 Answers1

1

All you need to do is you have to put the selector drawable in the button like this

<Button
 android:id="@+id/button1"
 android:background="@drawable/selector_xml_name"
 android:layout_width="200dp"
 android:layout_height="126dp"
 android:text="Hello" />

And the drawable like this

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape>
        <stroke android:width="10dp"/>
    </shape>
</item>
<item android:state_pressed="false">
    <shape>
        <stroke android:width="0dp"/>
    </shape>
</item>