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
Related
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.
daily_event_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/daily_event_selected"
android:state_pressed="true" />
This is what I was using for ONE SPECIFIC button but I want to change this for different buttons while using the same selector
<-- <item android:drawable="#drawable/daily_event_normal" />-->
</selector>
So I have this selector and I want to use it on all my ImageButtons
P.S.: it's working
But I want to give a different background to each of my ImageButtons in their "normal" state.
I want to use this as a generic selector to have the same effect on all my Buttons while keeping the background different.
But since the selector is only specified (as per my knowledge) as
android:background="#drawable/daily_event_selector"
in the background property.
So, I can't seem to find a way to add background to the Buttons.
Any idea?
Use different selectors for different buttons.
The selectors will only differ in their normal state.
This is the smartest way I know to do that.
Another way would be to have only 1 selector, and change it's normal state in Java.
But this would really be other-than-smart.
Because you have to do some extra work in code (therefore, eventually slowing down the CPU).
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 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
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.