2

I want to custom button like this

<shape      xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

<solid   android:color="#EAEAEA"/>

<corners    android:topLeftRadius="8dip"
            android:bottomRightRadius="8dip"
            android:bottomLeftRadius="1dip"
            android:topRightRadius="1dip"
            />
<item android:drawable="@drawable/photo2"
      android:state_pressed="true" />
<item android:drawable="@drawable/photo1" />

but at

<item android:drawable="@drawable/photo2"
      android:state_pressed="true" />
<item android:drawable="@drawable/photo1" />

it doesn't work ( if it work it will chang button backgroud when press or un press button )

What should I do?

user65544
  • 349
  • 3
  • 9
  • 17

2 Answers2

4

First Import LightningColorFilter, then you can change the color by applying this code:

Start_Button.getBackground().setColorFilter(new LightingColorFilter(0x11111111, 0x11111111));

This is to be placed inside your acitivity, and not the XML file.

Navigatron
  • 2,065
  • 6
  • 32
  • 61
  • You would use this instead of a drawable image. – Navigatron May 31 '11 at 10:30
  • How do you set it back to default button color? – powder366 Jun 25 '13 at 18:10
  • Well you will need to find out what the hex of the colour code is and then change it accordingly. The number inside the brackets is what you will change to change the colour. More info: http://stackoverflow.com/questions/7048941/how-to-use-the-lightingcolorfilter-to-make-the-image-form-dark-to-light – Navigatron Jul 01 '13 at 15:07
1

to set the style and color to default button set this code in any new xml and give orgnal button for background as this xml like:

in orignal button layout xml :- andorid:background="@drawable/mylayout"

in mylayout.xml:-

<item android:state_pressed="true" >
    <shape>
        <gradient
            android:startColor="@color/yellow1"
            android:endColor="@color/yellow2"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/grey05" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item android:state_focused="true" >
    <shape>
        <gradient
            android:endColor="@color/orange4"
            android:startColor="@color/orange5"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/grey05" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item>        
    <shape>
        <gradient
            android:endColor="@color/blue2"
            android:startColor="@color/blue25"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/grey05" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

and to change on focus button or click event try to set onfocuschnage other layout:

<item android:state_focused="true" >

and set diffrent color to change the button color or style..

Akash Thakkar
  • 998
  • 1
  • 10
  • 11