user2842510
user2842510

Reputation:

Custom Button in XML

I made this XML file to customize a button

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

<corners android:radius="10dip" />

<gradient
    android:angle="90"
    android:centerColor="#0043E1"
    android:centerY="0.4"
    android:endColor="#6495ED"
    android:startColor="#6495ED"
    android:type="linear" />

</shape>

What should I add to make the state focus and pressed please? A gradient color for the state pressed, and another for the state focused?

Upvotes: 3

Views: 12432

Answers (3)

Swetank
Swetank

Reputation: 1597

Create button_selector.xml inside drawable folder as below:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/image_normal" />
<item android:state_focused="false"
        android:state_pressed="true" android:drawable="@drawable/image_pressed" />
<item android:state_focused="true"
        android:state_pressed="false" android:drawable="@drawable/image_focused" />
</selector>

Then create two xml's one for pressed and other for focused as you have created already say image_pressed.xml and image_focused.xml

Upvotes: 2

Jagadesh Seeram
Jagadesh Seeram

Reputation: 2664

You need to write the selector for the button

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_normal_color" android:state_focused="true"></item>
    <item android:drawable="@drawable/button_focus_color" android:state_pressed="true">/item>
    <item android:drawable="@drawable/button_normal_color"></item>
  </selector>

set this xml to the button back ground

Upvotes: 4

Steve Bergamini
Steve Bergamini

Reputation: 14600

Please refer to this tutorial on how do create the appropriate drawable-xml file to allow for different states of a button:

http://undertowsam.wordpress.com/2012/04/23/design-custom-background-and-button-for-android-using-xml/

Upvotes: 2

Related Questions