I want to change the background image of a button when clicked or focused.
This is my code:
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOn
Sorry this is wrong.
For changing background color/image based on the particular event(focus, press, normal), you need to define a button selector file and implement it as background for button.
For example: button_selector.xml (define this file inside the drawable folder)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
<!-- IF you want image instead of color then write
android:drawable="@drawable/your_image" inside the <item> tag -->
And apply it as:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawable="@drawable/button_selector.xml" />
You can also create shapes directly inside the item
tag, in case you want to add some more details to your view, like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#81ba73" />
<corners android:radius="6dp" />
</shape>
<ripple android:color="#c62828"/>
</item>
<item android:state_enabled="false">
<shape>
<solid android:color="#788e73" />
<corners android:radius="6dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#add8a3" />
<corners android:radius="6dp" />
</shape>
</item>
</selector>
Beware that Android will cycle through the items from top to bottom, therefore, you must place the item
without condition on the bottom of the list (so it acts like a default/fallback).
To change the button background we can follow 2 methods
In the button OnClick, just add this code:
public void onClick(View v) {
if(v == buttonName) {
buttonName.setBackgroundDrawable
(getResources().getDrawable(R.drawable.imageName_selected));
}
}
2.Create button_background.xml in the drawable folder.(using xml)
res -> drawable -> button_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/tabs_selected" /> <!-- selected-->
<item android:state_pressed="true"
android:drawable="@drawable/tabs_selected" /> <!-- pressed-->
<item android:drawable="@drawable/tabs_selected"/>
</selector>
Now set the above file in button's background file.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background"/>
(or)
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
2nd method is better for setting the background fd button
you can implement in a xml file for this as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/your_imagename_while_focused"/>
<item android:state_pressed="true" android:drawable="@drawable/your_imagename_while_pressed" />
<item android:drawable="@drawable/image_name_while_notpressed" /> //means normal
</selector>
now save this xml file in drawable folder and name it suppos abc.xml and set it as follows
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
Hope it will help you. :)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/pause" />
<item android:state_selected="false"
android:drawable="@drawable/play" />
<!-- default -->
</selector>
<ImageView
android:id="@+id/iv_play"
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:background="@drawable/pause_button"
android:gravity="center"
android:scaleType="fitXY" />
iv_play = (ImageView) findViewById(R.id.iv_play);
iv_play.setSelected(false);
and also add this
iv_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
iv_play.setSelected(!iv_play.isSelected());
if (iv_play.isSelected()) {
((GifDrawable) gif_1.getDrawable()).start();
((GifDrawable) gif_2.getDrawable()).start();
} else {
iv_play.setSelected(false);
((GifDrawable) gif_1.getDrawable()).stop();
((GifDrawable) gif_2.getDrawable()).stop();
}
}
});
Its very easy to implement . For that you need to create a one xml file(selector file) and put it in drawable folder in res. After that set xml file in button's background in your layout file.
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/your_hover_image" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/your_hover_image" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/your_hover_image"/>
<item android:drawable="@drawable/your_simple_image" />
</selector>
Now set the above file in button's background.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/grey_text"
android:background="@drawable/button_background_selector"/>