Is there a way to get a drawable resource from a boolean resource?
For example:
bools.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="use_version_1_drawables">true</bool>
</resources>
my_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/my_drawable_version_1"/>
<item android:drawable="#drawable/my_drawable_version_2"/>
</selector>
Is there a specific state I should be using because from what I understand they are all related to specific events (checking, focusing, etc). Perhaps I shouldn't be using a selector. I simply want to have one resource I can call upon that's actually linking to two others but will select one based on my bool.
You are close. You can think of a selector as an if/else statement. What you're missing now is your actual state. Here is an example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimaryDark" android:state_checked="true" />
<item android:color="#color/gray" android:state_checked="false" />
</selector>
If you're using it with something like a Checkbox, then just set this selector as a drawable resource for the checkbox, and state_checked will be updated automatically.
EDIT: Problem was slightly more complex, and solved in the comments.
Related
Here is my selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/black"/>
<item android:color="#color/white"/>
</selector>
apparently I cannot save it in the /drawable folder as my android won't compile. Then if I create a /color folder in res I get a red mark from eclipse. So where do I save my selector so I can use it as the background color of a TextView (or really any other view).
I have a colors.xml file in values, but how would I add a selector to it?
THE ACTUAL SOLUTION
I am posting this edit in case someone else needs help. I hope you this saves you some time.
For the correct answer, I did following
In strings.xml
<drawable name="black_drawable">#color/black</drawable>
<drawable name="white_drawable">#color/white</drawable>
In colors.xml
<color name="black">#000000</color>
<color name="white">#ffffff</color>
Then in the selector, I did
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/black_drawable" android:state_pressed="true"/>
<item android:drawable="#drawable/white_drawable"/>
</selector>
The selector is saved in /drawable as selector_black_white.xml
Here's a solution I found.
You just need to create a "color" folder in your res and save the file in the newly created folder.
After completing this step you will be able to get the color selector file in your attribute of respective xml file.
I'm attaching screenshots for better understanding.
Step 1
Step 2
Last edit i messed up pretty in my previous edit sorry my bad this one is working now
res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/whiteColor"/>
<item android:drawable="#color/backColor"/>
</selector>
then on your res/value/string or create new values color
<color name="whiteColor">#000000</color>
<color name="backColor">#ffffff</color>
I think your problem is not the location - it should be in the /res/color folder. The problem is in how you specify the color:
<item android:color="#color/ffffff"/>
should be
<item android:color="#ffffff"/>
I've got a group of radio buttons, and I want to set the button's background to a solid color when checked. I created a drawable resource, using a selector and item def's like:
<item android:state_checked="true" android:state_pressed="false"
android:drawable="#color/app_tint"/>
with several variations while trying to get it to work. In the layout containing the buttons, I've tried setting both button and background properties (not at the same time, just one or the other in testing) like:
android1:background="#drawable/radio_state"
OR
android1:button="#drawable/radio_state"
I've read several posts, and I feel I'm close, just missing something to get it done. Thanks.
Here's one we did for an app:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_checked="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_selected="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_focused="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling" />
</selector>
Each state has a different drawable, although in this example, we don't really care about all states being very different - just focus=true get a highlighted drawable (it has "..._focus")
I am using drawables to nicely style my buttons, and that works fine, except for the text color in the button.
I have defined a state_enabled="false" item in a selector and using setEnabled gives me the right button styles, but I have to jump through quite some loops to get the text color different. This code for example doesn't work (it shows no, or black, text when disabled, and darkgray when enabled):
public void setButtonsEnabled(boolean enable) {
btnAccept.setEnabled(enable);
btnDecline.setEnabled(enable);
int color = R.color.White;
if (!enable) {
color = R.color.DarkGray;
}
btnAccept.setTextColor(color);
btnDecline.setTextColor(color);
}
I found the solution.
The key lies in also setting the TextColor to a selector in res/colors:
android:textColor="#color/button_text"
android:background="#drawable/button_selector"
For the background selector I used this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false" android:drawable="#drawable/btn_buddy_enabled"></item>
<item android:state_enabled="false" android:drawable="#drawable/btn_buddy_disabled"></item>
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#drawable/btn_buddy_clicked"></item>
</selector>
And the textColor selector is this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false" android:color="#color/White"></item>
<item android:state_enabled="false" android:color="#color/Gray"></item>
<item android:state_enabled="true" android:state_pressed="true" android:color="#color/White"></item>
</selector>
Simply calling setEnabled() will make everything work fine.
You are using the wrong value for the color. R.color.White returns the resource ID of the value, not the value itself. Try Color.WHITE, or getResources().getColor(R.color.White)
Have you checked out ColorStateLists? They are pretty awesome. So basically apply all those ideas of Drawable selectors to a set of colors.
Make a folder called [Your Project]/res/colors/ and then put an xml file in there called, button_color.xml (or whatever).
button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- Any Enabled button, gets White Text -->
<item
android:color="#color/White"
android:state_enabled="true" />
<!-- Buttons with any other state, get DarkGray Text -->
<item
android:color="#color/DarkGray"/>
</selector>
And then for your TextView, you can just do something like, mTextView.setTextColor(R.color.button_color); At that point there is no need for that if/else type of logic, the selector will do it for you. The selector gets rolled up into the color resource but the class it actually generates is called a ColorStateList in case you find it referenced in other documentation.
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 try to switch the background of Buttons if they are pressed. I build a Selector like the answer suggested here: Standard Android Button with a different color
Finally I want to put GradientDrawables inside, but for debugging purposes I just set a color, to check if it works.
Here is my Selector
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/red"/>
<item
android:state_focused="true"
android:drawable="#color/white"/>
<item
android:state_pressed="true"
android:drawable="#color/white"/>
</selector>
Unfortunatly this doesn't work. I set the Selector as Button background and only see them in red color. What Am I doing wrong (Build Target 2.1)
put this at the end
item android:drawable="#color/red"
i mean as the third option, it will work.
android checks the xml conditions from the start, the first tag doesn't have any condition, so it will always pick red, so you have put conditions first and then the default one.
here is the code I use, and it works really well.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_clicked"
android:state_pressed="true" android:state_enabled="true" />
<item android:drawable="#drawable/button" android:state_enabled="true" />
</selector>
here I use two images I made using photoshop as a background
the first is button_clicked and the second is button
copy it and change use your own resources.
hope I could help :)