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
Related
We have a checkbox inside a listview whose background image needs to change according the content of the listview item, like the gmail email listview.
Our approach right now is to set the checkbox background by using the android:button="#drawable/startcheckbox" option where startcheckbox is an xml in the drawable folder containing:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/star_rate_white_54x54" />
<item android:state_checked="true" android:drawable="#drawable/star_rate_yellow_54x54" />
<item android:drawable="#drawable/star_rate_white_54x54" /> <!-- default state -->
</selector>
My question is how can we change the state_checked="false" and state_checked="true" from inside the fragment where the listview is being populated.
Mutate the drawable via Drawable.mutate() then you can change whatever you want.
In general though I do find that if you want to do anything programmatic with a drawable other than scale it, you're better off avoiding xml and using code and custom drawables.
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 currently have a keypad in my application 0 - 9, I require an on and off state for each button.
To do this I've used a StateList as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="#drawable/dialpad_1_off" />
<item android:state_pressed="true"
android:drawable="#drawable/dialpad_1_on" />
</selector>
However this is only for one button, each button has a different on and off graphic, dialpad_2_off, dialpad_3_on etc...
So do I have to create a Statelist for every single button or is there a way to do it within one Statelist XML file?
You could make the background of the image change state and use that common background for all of the buttons. Then you could use either text or an image as the button foreground.
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
I want to remove the default orange focus color from GridView and from the EditText.
I have tried the android:focusable="false" but it is not working.
I think if this works as the other views, you have to use a selector in which you define various states for your view. A selector is a drawable (stored in the drawable folder) and you use it as if it was just an image. For instance, you could do something like that, if you want the focus to be red instead of orange :
selectorgridview.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#color/darkred" />
<item android:color="#color/white" />
</selector>
Then, you put the background of your GridView with it : android:background="selectorgridview"
I actually never tried it on a GridView but I think it works as the other views. More infos in the docs from google