Sunday, October 14, 2012

on click set border in android view (button/imageview) by using drawable xml


lot of time we using different drawable to show effect on press event in android. for example when we press a icon/button/imageview, want to set some border outside the image,



  • You have to use layer-list, when android:state_pressed become ture , one for bitmap(image) and another for border using android shape
  • when android:state_pressed become false or normal state of view, using drawable 




sample background.xml



<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <layer-list>
            <item>
                <bitmap android:gravity="center" android:src="@drawable/ic_launcher" android:tileMode="repeat" />
            </item>
            <item>
                <shape>
                     <solid android:color="#00000000" />
         <corners android:radius="3dp" />
         <stroke android:color="#FEA900" android:width="1dp" />
                </shape>
            </item>
        </layer-list>
    </item>
    <item android:drawable="@drawable/ic_launcher" android:state_pressed="false"/>
    <item android:drawable="@drawable/ic_launcher"></item>
</selector>



and how to used this background.xml see below button xml


<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/background" />

see the sample video :- http://screencast.com/t/vtI10uAC

No comments:

Post a Comment