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"?><selectorxmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:state_pressed="true"><shapeandroid:shape="rectangle"><cornersandroid:radius="5dp"/><gradientandroid:startColor="#ad1c1c"android:endColor="#cc3737"android:angle="90"/><paddingandroid:left="10.0dip"android:top="10.0dip"android:right="10.0dip"android:bottom="10.0dip"/><strokeandroid:width="1.0dip"android:color="#7d0000"/></shape></item><itemandroid:state_pressed="false"><shapeandroid:shape="rectangle"><cornersandroid:radius="5dp"/><gradientandroid:startColor="#cfcfcf"android:endColor="#ebebeb"android:angle="90"/><paddingandroid:left="10.0dip"android:top="10.0dip"android:right="10.0dip"android:bottom="10.0dip"/><strokeandroid: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"?><selectorxmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:state_pressed="true"><shapeandroid:shape="rectangle"><cornersandroid:radius="5dp"/><solidandroid:color="#f00"/><paddingandroid:left="10.0dip"android:top="10.0dip"android:right="10.0dip"android:bottom="10.0dip"/><strokeandroid:width="1.0dip"android:color="#222"/></shape></item><itemandroid:state_pressed="false"><shapeandroid:shape="rectangle"><cornersandroid:radius="5dp"/><solidandroid:color="#f1f1f1"/><paddingandroid:left="10.0dip"android:top="10.0dip"android:right="10.0dip"android:bottom="10.0dip"/><strokeandroid: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
publicvoidonClick(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"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"android:drawable="@drawable/backbutton" /><itemandroid: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"