Skip to content Skip to sidebar Skip to footer

SetBackgroundColor In Android

In this simple game I want to change the background color of the button that I press. But I get the following result, the buttons appearance becomes not good (the shape becomes di

Solution 1:

pressedButton.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);

Solution 2:

create selector file any name like button_selector.xml in drawable folder

Edited with Gradient

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <gradient android:startColor="#ad1c1c" android:endColor="#cc3737" android:angle="90"/>
            <padding android:left="10.0dip" android:top="10.0dip"
                android:right="10.0dip" android:bottom="10.0dip"/>
            <stroke android:width="1.0dip" android:color="#7d0000"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <gradient android:startColor="#cfcfcf" android:endColor="#ebebeb" android:angle="90"/>
            <padding android:left="10.0dip" android:top="10.0dip"
                android:right="10.0dip" android:bottom="10.0dip"/>
            <stroke android:width="1.0dip" android:color="#8f8f8f"/>
        </shape>
    </item>
</selector>

then set in button background

<Button
    android:background="@drawable/button_selector"
/>

Solution 3:

As @pratik said save this button.xml in drawable folder

button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <solid android:color="#f00"/>
            <padding android:left="10.0dip" android:top="10.0dip"
                android:right="10.0dip" android:bottom="10.0dip"/>
            <stroke android:width="1.0dip" android:color="#222"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <solid android:color="#f1f1f1"/>
            <padding android:left="10.0dip" android:top="10.0dip"
                android:right="10.0dip" android:bottom="10.0dip"/>
            <stroke android:width="1.0dip" android:color="#222"/>
        </shape>
    </item>
</selector>

Apply this button as background

<Button
    android:background="@drawable/button"/>

and in your class file do like this

public void onClick(View v) {

  pressedButton.setPressed(true);
}

so that the red color will be stable


Solution 4:

try this :

you have create like this in the ImageButton xml

create xml file using the button image like this

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

add that xml file as background for IMageButton

<ImageButton                 
android:layout_height="50px" 
android:layout_width="50px" 
android:id="@+id/settings" 
android:background="@drawable/settings_button" //setting_button in
                                                     the xml file            
android:text="Settings"/>

Post a Comment for "SetBackgroundColor In Android"