Is it possible in Android to give to a LevelListDrawable an empty case? "Empty" is like "No Image". For instance, take a look at the LevelListDrawable here below:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/face_1"
android:maxLevel="1" />
<item
android:drawable="#drawable/face_2"
android:maxLevel="2" />
<item
android:drawable="#drawable/face_3"
android:maxLevel="3" />
<item
android:drawable="#drawable/face_4"
android:maxLevel="4" />
<item
android:drawable="#drawable/face_5"
android:maxLevel="5" />
<item
android:drawable="#drawable/face_6"
android:maxLevel="6" />
</level-list>
It is a Dice with its 6 faces. I'd like to have an empty face too. A NULL case, or something like that.
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable=""
android:maxLevel="0" />
<item
android:drawable="#drawable/face_1"
android:maxLevel="1" />
<item
android:drawable="#drawable/face_2"
android:maxLevel="2" />
<item
android:drawable="#drawable/face_3"
android:maxLevel="3" />
<item
android:drawable="#drawable/face_4"
android:maxLevel="4" />
<item
android:drawable="#drawable/face_5"
android:maxLevel="5" />
<item
android:drawable="#drawable/face_6"
android:maxLevel="6" />
</level-list>
Currently I simulate this behavior like this:
ImageView image = new ImageView(context);
image.setImageDrawable(getResources().getDrawable(R.drawable.dice));
image.setImageLevel(3);
if(somethingHappened){
image.setImageDrawable(null);
}
But after that I have to call image.setImageDrawable(getResources().getDrawable(R.drawable.dice)); another time instead of changing the image level. Thank you.
minLevel is 0 by default, so if you want to exclude face_1 from showing for level=0 add it explicitly
<item
android:drawable="#drawable/face_1"
android:minLevel="1"
android:maxLevel="1" />
The problem is that now face_2 will be shown (because it's matching [0, 2]), but if you add min and max for all of the faces with the same values each, they'll play nicely.
See android.graphics.drawable.LevelListDrawable.LevelListState#indexOfLevel for more.
Alternatively I saw a magic value somewhere:
<item
android:drawable="#null"
android:maxLevel="0" />
But this will likely throw an NPE or exception from LevelListDrawable#inflate:
<item> tag requires a 'drawable' attribute or child tag defining a drawable
Related
I had been trying to disable the highlight in a ExpandableListView. I tried setting the next drawable as background, but this didn't work.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/_GREY_Weak"/>
<item android:drawable="#color/_GREY_Weak"
android:state_pressed="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_selected="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_focused="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_checked="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_long_pressable="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_hovered="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_expanded="true" />
</selector>
I tried setting each state drawable as that color, #null or transparent, but still the same. Here are some screenshots of my issue. I want to get rid of that highlight in the childrens as in the parent.
I also triead setting the property drawingCacheHint in the ExpandableListView xml with no luck.
Try adding
android:listSelector="#android:color/transparent"
in ExpandableListView xml
Or override the ExpandableListView style.
In my project I have one service which returns image of Ratting, i want to set this image to RattingBar. Does anybody know how to set image dynamically in RattingBar ?
Thanks
Apply this:
ratingbar.setProgressDrawable(getResources().getDrawable(R.drawable.rating_bar_selector));
rating_bar_selector.xml:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background" android:drawable="#drawable/your_image" />
<item android:id="#android:id/secondaryProgress"
android:drawable="#drawable/your_image_half selected" />
<item android:id="#android:id/progress"
android:drawable="#drawable/your_image_full_selected" />
</layer-list>
I've made a ratingbar in my app which uses a custom style, using my own stars.
My ratingbar is defined as followed:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="15dp"
android:id="#+id/ratingBar"
android:numStars="10"
android:stepSize="1"
android:rating="10"
android:layout_marginTop="5dp"
android:layout_below="#id/feedbackTable"
android:layout_toRightOf="#id/ratingText"
android:layout_marginLeft="5dp"
style="#style/starBar" />
The style that I use in styles.xml is:
<style name="starBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/ratingstars</item>
<item name="android:minHeight">22dip</item>
<item name="android:maxHeight">22dip</item>
</style>
With ratingstars.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/background" android:drawable="#drawable/ratingstars_full_empty" />
<item android:id="#+id/secondaryProgress" android:drawable="#drawable/ratingstars_full_empty" />
<item android:id="#+id/progress" android:drawable="#drawable/ratingstars_full_filled" />
</layer-list>
When I adjust the android:rating it shows the correct amount of stars filled and being empty in the preview. However, using my emulator or real device the bar stays completely filled with stars (10/10). I've tried adjusting it programmatically by using the following piece of code:
ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Log.d("rating", rating+"");
ratingbar.setRating(rating);
}
});
Now the log from logcat shows actually that rating IS changed, but the bar does not visually update itself with e.g. 6 stars being full, and 4 empty and I have no clue why.
The drawables are correct as the preview also shows it correct, so what can cause this?
Edit:
This is ratingstars_full_filled.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/star" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star" />
<item android:drawable="#drawable/star" />
</selector>
And here is ratingstars_full_empty.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/star_empty" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star_empty" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star_empty" />
<item android:drawable="#drawable/star_empty" />
</selector>
You should change the id attribute in ratingstars.xml from "#+id/..." to "#android:id/...", since the resource is part of the android system, and already defined in the default android progress drawable.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background" android:drawable="#drawable/ratingstars_full_empty" />
<item android:id="#android:id/secondaryProgress" android:drawable="#drawable/ratingstars_full_empty" />
<item android:id="#android:id/progress" android:drawable="#drawable/ratingstars_full_filled" />
</layer-list>
It looks like it's not a problem with the listener being called, but rather just the custom android:progressDrawable style attribute you're applying to it. My guess is that the drawable you're using is doing unexpected things. Try removing style="#style/starBar" from your RatingBar and see if things are working as expected. If so, that's your problem
I was wondering if there was any way in Android to set an ImageView's image dynamically by using its tag.
Ideally it would be like this:
// image_view_style.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:tag="1"
android:drawable="#drawable/image_for_tag_1"/>
<item android:tag="2"
android:drawable="#drawable/image_for_tag_2"/>
<item android:tag="3"
android:drawable="#drawable/image_for_tag_3"/>
</selector>
And simply calling the ImageView's setTag() method with the value I want.
I tried this but it doesn't work. Is there any clean way to do the image selection in the resource file (for something else than state_selected, state_pressed, etc) ?
I think you can use LevelListDrawable to achieve what you want:
https://developer.android.com/reference/android/graphics/drawable/LevelListDrawable.html
From the documentation:
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="#drawable/ic_wifi_signal_1" />
<item android:maxLevel="1" android:drawable="#drawable/ic_wifi_signal_2" />
<item android:maxLevel="2" android:drawable="#drawable/ic_wifi_signal_3" />
<item android:maxLevel="3" android:drawable="#drawable/ic_wifi_signal_4" />
</level-list>
then use setLevel(...) in code to set a level.
For example, say I have a color state list declared in XML, called example1.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#color/red"
android:state_pressed="true" />
<item
android:color="#color/blue"
android:state_checked="true" />
<item
android:color="#color/green"
android:state_disabled="true" />
<item
android:color="#color/orange" />
</selector>
Then, I want to create example2.xml and I want it to be the exact same as example1.xml except I want the pressed color to be purple instead of red:
<item
android:color="#color/purple"
android:state_pressed="true" />
So example2.xml would end up acting like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#color/purple" <-- note this value is different
android:state_pressed="true" />
<item
android:color="#color/blue"
android:state_checked="true" />
<item
android:color="#color/green"
android:state_disabled="true" />
<item
android:color="#color/orange" />
</selector>
but without all of the duplicate code. Also, if I want to change a color I can just change it in one place.
Is this possible?
I don't think you can do this on Android, You have to create multiple xml resources for different selector.