How to Set Image Button Backgroundimage for Different State

How to set Android ImageButton backgroundimage for different states in Java Code

I wound up simply replacing the selector when the button changes states as follows. Was fairly simple in the end. Inside the button's class:

    StateListDrawable states = new StateListDrawable();
states.addState(new int[] {-android.R.attr.state_pressed},getResources().getDrawable(R.drawable.button_normal));
states.addState(new int[] {android.R.attr.state_pressed},getResources().getDrawable(R.drawable.button_pressed));

this.setImageDrawable(states);
this.invalidate();

html5 button background image states

Yes, you can use button:focus for click state. Just declared it on css.

<button>Click me</button>

CSS sample :

button {
background:yellow;
padding:12px 40px;
border:1px solid yellow;
color:#454545;
}
button:focus {
background:blue;
}

Just change color value on background with your background image path.

DEMO

Use one image for different buttons state

I found a solution that I am satisfied:

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

On button background I put this drawable, where my_image image from resources and my_image_pressed next drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap android:src="@drawable/my_image" />
</item>
<item>
<color android:color="#5FEC" />
</item>
</layer-list>

Toggle button using two image on different state

Do this:

<ToggleButton 
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/check" <!--check.xml-->
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>

create check.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/unselected_image"
android:state_checked="false"/>

</selector>

How to set the button background image through code

for set background image for button which is in drawable folder then use below code

btn.setBackgroundResource(R.drawable.new_todo_image);

Create a button programmatically and set a background image

Your code should look like below

let image = UIImage(named: "name") as UIImage?
let button = UIButton(type: UIButtonType.Custom) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside)
self.view.addSubview(button)


Related Topics



Leave a reply



Submit