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.
Related
I have some TextViews which are dinamically added to a LinearLayout. These TextViews are clickable and have an onLongClickListener (I also intend to add onClickListener later)
Here is the thing, I want these TextView to change their background color when pressed and I read that you can use selectors to do such thing.
So I made this xml file in res/drawable/text_view_pressed.xml:
<?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"/>
<item android:state_pressed="false"
android:color="#FFFFFF"/>
</selector>
I tried to create a TextView and using this xml file likes this:
TextView t = new TextView(this);
t.setBackgroundColor(R.drawable.text_view_pressed);
But when I do this, it will give this error in t.setBackgroundColor: "Should pass resolved color instead of resource id here: getResources().getColor(R.color.text_view_pressed)" but it doesn't work as intended if I use getResources().getColor(R.color.text_view_pressed).
Anyone got an idea how to do it?
You are on the right track. However there is an important detail.
There are two types of resources that can be affected by states: ColorStateList and StateListDrawable.
A color state list can only be used in certain contexts, for example in TextView.setTextColor(). As far as I can see, you cannot use a color state list as parameter of setBackgroundColor() if you want to change the background of a View when it's pressed. You need a state list drawable for that. And in a state list drawable, the android:drawable attribute is mandatory.
So, to sum up:
The xml file should be placed in res\drawable,
Its structure should be slightly different (i.e. state list, not color list), and
You need to use setBackgroundResource() instead of setBackgroundColor().
Example file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/white" />
<item android:drawable="#android:color/black"/>
</selector>
If you want to use custom colors instead of white and black, you simply need to define them as resources in res\values and reference them from here.
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'm working on an application where I'm using a checkedTextView, it all works great. But I really don't like that layout of the "checkbox" within the checkedTextView, it's simply to big. Is there any way to resize it or change the layout to something custom made?
I've tried the android:checkMark attribute, but that resulted in it being marked all the time, and thus showing all the time.
Instead of using a single drawable you should write a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/drawable_checked"
android:state_checked="true" />
<item
android:drawable="#drawable/drawable_unchecked"
android:state_checked="false" />
</selector>
And then set it to the android:checkMark attribute.
I have the following selector defined in an XML file under res/color/redeemlist_item_color.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#FFFFFF" /> <!-- pressed -->
<item android:state_selected="true"
android:color="#FFFFFF" /> <!-- focused -->
<item android:color="#000000" /> <!-- default -->
</selector>
I also have a TextView in a ListView item layout. When I set android:textColor on this TextView to the above selector in XML, then the color changes correctly when the item is selected. However, I am trying to set this resource programmatically in the following way:
holder.label.setTextColor(R.color.redeemlist_item_color);
When set in this way, the color no longer changes. Can a selector be assigned to a TextView in this way?
I think you might need to add findViewById or something of that variety
Edit: the above is incorrect as per my comment the proper answer is
setTextColor(getResources().getColorStateList(R.color.redeemlist_item_color));
You have to use getColorStateList()
I was also struggling with this problem, if you want to have use a state list, you need to declare it in the color resources folder, instead of the drawable folder, and use the setTextColor(getResources().getColorStateList(R.color.redeemlist_item_color)).
You can try:
holder.label.setTextColor(getResources().getColor(R.color.redeemlist_item_color));
instead of :
holder.label.setTextColor(R.color.redeemlist_item_color);
Rasman is correct. You need to give the TextView an ID, android:id="#+/something". You retrieve a reference to that particular using that ID and findViewById, and then you may set the text color.
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