I changed the background for a ToggleButton and wish to reset it back to its default state.
Changed the background:
btn.setBackgroundColor(Color.parseColor("#FF2929"));
Tried changing it back:
btn.setBackgroundResource(0); //Just text, no button
btn.setBackgroundColor(android.R.drawable.btn_default); //Normal button not toggle
you are setting a drawable in Color, use btn.setBackgroundResource(android.R.drawable.btn_default).
This should set something in your button.
Create an xml file under drawable folder and put this code , currently the xml file name I put is check.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/Your color"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="#color/Your color"
android:state_checked="false"/>
</selector>
Put this code in your toggle button layout xml
android:background="#drawable/check"
This may help you.
Related
I want to change the color of check box button as when it gets focus its background color will change and when it has no focus it will remain the same.
I have tried using selector but not able to change it
can any one help me out in this
Use this:
Backround-color: 'any color'
Create drawable file like this :
checkbox_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/pink"/>
<item android:state_checked="false" android:color="#color/checkbox_not_selected"/>
</selector>
Apply it like this :
<androidx.appcompat.widget.AppCompatCheckBox
android:id="#+id/chkSelected"
android:layout_width="#dimen/_23sdp"
android:layout_height="#dimen/_23sdp"
android:layout_alignParentEnd="true"
android:buttonTint="#drawable/checkbox_color"
android:checked="false"
android:clickable="false" />
then change it runtime like this to change color runtime based on of it is selected :
chkSelected.isSelected = true
or
chkSelected.isSelected = false
add a new color file in your project. (right click on res then new>android recourse file and put the recourse type on Color )
fill this file with this code :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#ce0e0e" />
<item android:state_checked="false" android:color="#6fc97d"/>
</selector>
then in your CheckBox add this color as your checkBox Color.
you can change your color when checked is true or false in your sample color file
I need to make a style like following screen shot
PLZ NEED HELP !!!!
I have tried using make custom style
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/buttion_inner_icon" android:state_checked="true"/>
<item android:drawable="#drawable/buttion_inner_icon" android:state_checked="false"/>
</selector>
But its not as I want
Please help me to make view same as screen shot
You have setted the same drawable when state_checked = true and state_checked = false.
You must have two differents drawables.
You can use a ToggleButton for this. In your xml file, set the text to an empty string, otherwise it will show 'on' and 'off'.
android:textOff=""
android:textOn=""
Then you have to add two drawables: one with the handle on the left side (so that 'check in' is visible, this is the unchecked state, name it for example 'check_in_unchecked'), and one with handle on the right side (so that 'check out' is visible, this is the checked state, name it for example 'check_in_checked'). After that, add a selector as background of your ToggleButton:
android:background="#drawable/selector_check_in"
selector_check_in will look like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/check_in_checked" android:state_checked="true"/>
<item android:drawable="#drawable/check_in_unchecked" android:state_checked="false"/>
</selector>
I have a ListView and a Button. The button should always be clickable, but the button background image should be disabled(grayed) when the ListView is empty and enabled(actual background) when the ListView has items.
I know this can be achieved from code by always setting the state enabled as true and changing the background image. But I am looking to achieve this in XML using selector.
Take a look at the following code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true" android:dither="true">
<item android:drawable="#drawable/button_enabled" android:state_enabled="true" />
<item android:drawable="#drawable/button_disabled" android:state_enabled="false" />
</selector>
Use this code in your drawables folder (as an XML resource).
Afterwards you use this drawable and set it as a background property to your button. (android:background="#drawable/your_selector_file" without the .xml extension, of course)
If you need more information just consult the following link: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
I have one button. I have added background image to my button using background attribute. Now when I click on button, I am not getting the default orange color. How can I get that color.
One more query, In the above scenario how can I change the default orange color to some other color.
Try this http://developer.android.com/guide/topics/ui/controls/button.html#Style, change state of your button
You have to make your button custom to do that changes for that create an xml file named custom_btn in yoyr drawable folder and paste the code in it as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/tick_btn" /> <!-- pressed -->
<item android:drawable="#drawable/untick_btn" /> <!-- default -->
</selector>
and in the button you have to add android:button="#drawable/custom_btn"
// try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="hover color or drawable" android:state_pressed="true"></item>
<item android:drawable="hover color or drawable" android:state_focused="true"></item>
<item android:drawable="color or drawable" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="hover color or drawable" android:state_enabled="false"></item>
</selector>
if you're just trying to see the "default orange color" then use an ImageButton and apply your drawable to the src (rather than the background). You will then see the native image background behind your image and it will continue to do whatever it was doing before.
I am not getting the default orange color. How can I get that color.
When you add a background to a button it no more remains a default raw button , It becomes a custom button as you have rendered the default behavior by adding some background to it. Now you need to add color to your custom button on your own because the OS deals with only raw buttons not custom.
How can I change the default orange color to some other color.
To change the button state after its pressed can be done in two ways
1) Either add a background image.
2) or Add a xml to the button.
This is a very nice tutorial on Customizing Android buttons.
By default when button is clicked something like orange color will surround the button for short time, that indicates buttons is clicked. But this feature is not working when button contains background image. This is happening in list view too.why ? Any Ideas? TIA
I used setBackgroundColor(Color.BLUE); But here the color is applied and not gone...
You need to use a selector as your background resource :
<?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/button_pressed" />
<item android:state_focused="true" android:drawable="#drawable/button_focus" />
<item android:drawable="#drawable/button_normal" />
</selector>
This selector makes use of 3 separate drawables to change the image of the button , you may use the same image for pressed and focuses.
You need to put this XML into your drawables directory and use it as a background for your button.
For another solution refer : Standard Android Button with a different color
i too had the same problem. so instead of setting the background color,i included three images of button in three different colors , so based on state focused,pressed,default the respective image will replace the other. and it looks like change in the color of the button.
**<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/c2"
android:state_pressed="true" />
<item android:drawable="#drawable/c1"
android:state_focused="true" />
<item android:drawable="#drawable/c1" />
</selector>**
above is the content of the xml file,which must be assigned to the background of the respective button
android:background="#drawable/drawable_button
hope this might be helpful for you