user65544
user65544

Reputation: 349

how to set button color

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?

Upvotes: 2

Views: 11478

Answers (2)

Navigatron
Navigatron

Reputation: 2115

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.

Upvotes: 4

Akash Thakkar
Akash Thakkar

Reputation: 998

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..

Upvotes: 1

Related Questions