More than two states using a StateListDrawable - android

I have a requirement where I have to specify a review rating (0 stars to 5 stars) This rating is shown as an ImageView. I understand that StateListDrawable can be used when there are two states in which a button/view exist (for instance a selected state and an idle state)
My question is if there is a way of including more states (in this case 6 states) and show the drawables accordingly.
Currently I can only do this -
<selector>
<item android:drawable="#drawable/zerostars"
android:state_selected="true"
android:background="#color/tab_background_1" />
<item android:drawable="#drawable/fivestars" />
</selector>
Any suggestions will be most helpful.

A Level List seems to fit perfectly.

Related

Want to add an indicator when a button is focused

I am currently developing an android TV app and have configured a basic button on the main fragment. At the current time there is no way of indicating wether you are focusing on, or knowing you can click on this button. I created a view with a drawable background, to use as the indicator; then wrote some code (Kotlin). At first I wrote: indicator.isVisible=button.isFocused to no avail, I also tried earlier in the code to set (as a test) button.isFocused = true this also did not work. After, some research I realised that you could set the button to focused by default. After inserting this into the code, there was of course an issue with the API usage (26 or above); mine being API 22. So, I'd like to know for an API below 26 (in my case 22) how can I make the indicator visible when the button is focused? Once again, so the user knows what they're currently hovering over (especially in rows with many selectable views)
Update:
Well, looking at the View documentation, I found out there is a setOnHoverListener method and a OnHoverListener interface. I haven't tried it but I think this one is what you are looking for.
Old answer:
You can create a new xml drawable with different background colors for each state (hover, pressed, default) and set that drawable as the button's background.
The new drawable would look like this, i.e :
<?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/blue" />
<item android:state_focused="true" android:drawable="#color/gold" />
<item android:drawable="#color/grey" />
</selector>

tabs color changing

I was just wondering about tabs that change color when you are on the selected fragment
For example:
like facebook app tabs, when you are on home page the icon color changes to blue.
Is this a tool or library used, or just i have to explicitly change all colors when fragment change ?
Try out bottom navigation view (https://developer.android.com/reference/android/support/design/widget/BottomNavigationView) by android and use a color selector like
background_color.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/blue" />
<item android:color="#color/grey" />
</selector>
and use this as
app:itemIconTint="#color/background_color.xml"
in your bottom navigation view.
It's hard to say exactly how any given app does that, but chances are good they are using a Color State List resource or a State List Drawable resource. Such a resource allows you to define different values for different "states" all within a single file. Then you can apply this resource to your tabs (or checkbox or anything else) and it will automatically update accordingly.

How can I Change the BackgroungResource different time In my #Kotlin app?

I know it seems that my question had been asking before, but all the questions and the answers were for java but I want it specifically for kotlin!
I have a drawable.xml for change the shape and the color of a button
and I have another drawable in it there is a different shape and color.
In different cases, I want to change the Resource of a button at different times, but it takes only the first change and after that, it keeps that way!
Your sequence could be wrong. You should take care of sequence of states in a way that property without any state should come in last like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffbb00"/>
<item android:state_focused="true"
android:color="#00ffbb"/>
<item android:color="#ffffff"/> <!-- without any state should come in last -->
</selector>
If this do not solve your problem than Please share your code so I can help more specifally

adding style to a android borderless button (android 4.0+)

I want to add a border to a borderlessbutton. However, if I create my own style with the parent borderlessbutton and I overwrite the background tag I loose the state change animation etc, as this is also defined by the background. I also cannot implement them myself as the native android drawables are not available as public and therefore not accessible. I do not want to have to copy the drawables.
Is it only possible to overwrite ondraw progammatically or is there an xml based solution i am missing?
(btw this is for a periodic table so this should not involve having an xml file for each button as there are about 100 of them)
thanks
stephan
The bulk of this comes from this article.
There is a 4 step process to doing something like this:
Create an XML file that contains the states.
Create an XML file (Or background) for each state
Create the style of the button
Add the button to your layout, and see what it looks like.
The single most difficult thing is the first step, so I'll show that one here. For the other ones, you can visit the site or do your own thing. Essentially, it will look something like this. Basically needs to capture all of the 4 states. This should be saved in the drawable folder, and the name of this is what your application will use for the name of the drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/loc_for_button_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/loc_for_button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/loc_for_button_focused" />
<item
android:state_enabled="true"
android:drawable="#drawable/loc_for_button_enabled" />
</selector>
An easy way would be to place your button within a separate layout, e.g. a LinearLayout and give this layout a background color and a padding of e.g. 1dp. This would render a "border" around the button. Note, that this is quite costly in regard of layouting, so do not use this method when you have lots of buttons.
The correct solution would be to create your one drawables for all states and build a statelist drawable with your drawables and assign this statelist drawable as your button's background. Actually it's not that much work, just have a look at http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

ListView item with list_selector_background as LayerList drawable

If we use a ListView or an ExpandableListView the default background is #android:drawable/list_selector_background. I have an ExpandableListView which shows data grouped by date. I like the state list list_selector_background drawable and want to keep the behaviour for different states. But for the weekend (for some of the list view items) i'd like to define a custom background. But these items should still use the drawables for different states defined through list_selector_background.
I saw different not suitable answers. Most of them say i should define my own state list drawable.
The list_selector_background.xml defines, besides different drawable for different states, <item android:state_window_focused="false" android:drawable="#color/transparent" />
So i thought i define my own state list drawable and just change the transparent to my desired color. The problem is the drawables used for the states are not public. I really want to stay close to the android style so i don't want to write my own state list drawable with own drawables for states.
At the end i wrote a solution using a layer list drawable.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/DarkBlue"
android:id="#+id/background" />
<item
android:drawable="#android:drawable/list_selector_background"
android:id="#+id/list_selector_background" />
</layer-list>
This perfectly matches the desired behaviour with one exception :/. I have my custom background and the item reacts with drawable defined in list_selector_background for press events.
But somehow the focused state does not use the drawable defined by list_selector_background (<item android:state_focused="true" android:drawable="#drawable/list_selector_background_focus" />)
So i kindly ask if anyone can imagine why the focused state does not work? I am developing with API level 8. If you need any further information i'd like to post. Thanks

Categories

Resources