How To Set Background Of Selected/unselected Button In The Xml File
Solution 1:
If it's selected or not selected you should use a toggle button https://developer.android.com/reference/android/widget/ToggleButton.html
Be aware that there are still 4 states for that
You define them in a selector like this
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_checked="true"android:state_pressed="true"android:drawable="@drawable/likeactivepressed" /><itemandroid:state_pressed="true"android:drawable="@drawable/likeinitialpressed"/><itemandroid:state_checked="true"android:drawable="@drawable/likeon"/><itemandroid:drawable="@drawable/likeinitial"/></selector>
Then define it in your button like this
android:background="@drawable/like_button"
Edit
You could actually just use 1 button for your use. Alternatively you can use 2 radio buttons
https://developer.android.com/reference/android/widget/RadioButton.html
Solution 2:
This is used to change color of button on pressed or focused write this code in your drawable folder
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><!-- Button Focused--><itemandroid:state_focused="true"android:state_pressed="false"android:drawable="@drawable/login_hover"
/><!-- Button Focused Pressed--><itemandroid:state_focused="true"android:state_pressed="true"android:drawable="@drawable/login_hover"
/><!-- Button Pressed--><itemandroid:state_focused="false"android:state_pressed="true"android:drawable="@drawable/login_hover"
/><!-- Button Default Image--><itemandroid:drawable="@drawable/login_bg"/>
</selector
http://nishantvnair.wordpress.com/2010/10/05/change-color-of-button-on-click-android/
Solution 3:
To change background image:
public void onClick(View v) {
if(v == ButtonName) {
ButtonName.setImageResource(R.drawable.ImageName);
}
}
Or, using an XML file:
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"android:drawable="@drawable/login_selected" /><!-- pressed --><itemandroid:state_focused="true"android:drawable="@drawable/login_mouse_over" /><!-- focused --><itemandroid:drawable="@drawable/login" /><!-- default --></selector>
In OnClick, just add this code:
ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName));
Solution 4:
Yes, you can select a Drawable
based on the state of the View
that is rendering it.
This is the exact purpose of a Selector
type of drawable. See examples and guide here: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Basically, each item
of a selector defines which states have which values. It also defines which drawable represents this set of values.
Next, you can set state of a View
from the code, eg.
celsiusButton.setPressed(true);
This is great in practice because you're separating UI settings from the model/controller. Maintaining large sets of drawables comes easier when the code isn't responsible for changing the UI of your application in a direct way.
An example of my working selector is:
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/ic_background_pressed"android:state_pressed="true"/><itemandroid:drawable="@drawable/ic_background_focused"android:state_focused="true"/><itemandroid:drawable="@drawable/ic_background_default" /></selector>
This example renders a button background depending on it's state.
Post a Comment for "How To Set Background Of Selected/unselected Button In The Xml File"