I am using a Recyclerview. It has a GridLayoutManager and contains/displays a grid of images using the Glide/Picasso library. My item in the recyclerview has the following layout.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/item_recycler_view"
android:clickable="true"
android:focusable="true"
android:foreground="#drawable/item_recycler_view">
<ImageView
android:id="#+id/savedPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:transitionName="imageScale" />
I have set the following states drawable(item_recycler_view) to the parent FrameLayout.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/holo_red_dark" android:state_selected="true" />
<item android:drawable="#android:color/holo_red_dark" android:state_pressed="true" />
<item android:drawable="#android:color/transparent" />
I change in state does not take place on pressing the item in the recyclerview.
How do I set different drawables for the different states of a RecyclerView item while continuing to use Picasso/Glide ??
I had same problem and I solved with android:state_focused="true" attribute in this way:
in your item_recycler_view
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/colorAccent"
android:state_focused="true" />
</selector>
</item>
</ripple>
Related
I am using a drawable to change the background and text of textViews inside of my navigation drawer. I would like to keep the background white for the text area, but by testing keeping the background white does not show the ripple effect on the background, instead it does it to the text making the text gray. In the picture below, the middle one is being pressed causing the ripple effect.
Here are my drawable files that are used to make the changes in colors
Background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/selected" android:state_activated="true" />
<item android:drawable="#color/white" />
</selector>
Text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/primary" android:state_activated="true" />
<item android:color="#color/primary_text" />
</selector>
textView layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25.0sp"
android:background="#drawable/activated_background"
android:textColor="#drawable/activated_text"
android:id="#id/text1"
android:layout_weight="0"
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip" />
</RelativeLayout>
you should use 2 drawable files and use it as your view background.
for pre-lolipop versions:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white_list_item_selected_background" android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
for lolipop (v21):
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/white_list_item_selected_background" >
<item android:drawable="#android:color/white" />
</ripple>
If you use custom background, ripple effect will not be shown. You may have to use other ripple library such as Material Ripple.
In my case ripple effect is working after the first click, but for first click it didn't work for me. Have changed the background selector file with android:state_activated="true" and in main.xml android:clickable="true" then it's work fine for all time.
selector.xml (under res\drawable\selector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
</selector>
In activity_main.xml
<com.mysample.RecyclingImageView
android:id="#+id/imageview_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/selector"
android:clickable="true" />
I am implementing an endless ListView (like in the Facebook app). I want to make item as select(temporarily show background as grey) which I select. when I click on listview, nothing happens(not shown as grey). I tried setting android:focusable="false" and android:cickable="false" for my List view contents. But I didn't notice any chanage What is my fault? Any One help me. Thanks in advance.
You can accomplish this by using a selector which you can set as the background of your list item.
drawable/background_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/gray" android:state_pressed="true" />
<item android:drawable="#color/gray" android:state_selected="true" />
<item android:drawable="#color/gray" android:state_activated="true" />
<item android:drawable="#color/default_color" />
</selector>
layout/list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_list_item"
android:orientation="vertical">
<... />
</LinearLayout>
If you want to show the clicked option as highlighted. Don't put clicklistener to convetview. Use
ListView.SetOnclicklistener(..){
...
}
That is best one to remove the issue.
Try this as background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/someGreyColor" android:state_activated="true"/>
<item android:drawable="#color/anyColor" android:state_pressed="true"/>
<item android:drawable="#color/backgrounColor"/>
</selector>
I have a problem with ListView background on android 2.3.3 (android 4 no problem). When click on list item, changes ListView background to #color/holo_green from android:listSelector attribute. How to resolve this problem?
my_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
android:alwaysDrawnWithCache="false"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#drawable/sa_list_item_selector"
android:divider="#drawable/sa_list_divider"/>
Adding android:background="#android:color/transparent" nothing changes
sa_list_item_selector.xml
<?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/holo_green" />
<item android:drawable="#android:color/transparent" />
</selector>
Maybe try to change:
<?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/holo_green" />
<item android:drawable="#android:color/transparent" />
</selector>
To:
<?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/transparent" />
<item android:drawable="#color/holo_green" />
</selector>
Look at your selector. The code is there.
I am using this tutorials and I want to add selectors for this list view. I tried some codes but it doest work. How can I do it.
I used code as list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
<item android:state_pressed="true"
android:drawable="#drawable/list_selector_background_pressed" />
</selector>
and my List view
<ListView
android:id="#+id/select_names_tags_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector"
android:layout_alignParentTop="false"
android:layout_gravity="center_vertical">
</ListView>
Just change a word here from listSelector to background. Because you want it as the background drawable selector, right?
<ListView
android:id="#+id/select_names_tags_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:layout_alignParentTop="false"
android:layout_gravity="center_vertical">
</ListView>
EDIT:
try changing your selector file to (I changed the order of the two items):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/list_selector_background_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
</selector>
I followed this tutorial to create a color state list for a particular Android view. I just want it to highlight when clicked so the user knows why the screen just changed.
When the view is rendered, I get the following error:
org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: tag requires a 'drawable' attribute or child tag defining a drawable
My color XML (in res/color/viewcolor.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="#ff33ffff"/> <!-- pressed -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
My layout XML (in res/layout/myview.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myview"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:background="#color/viewcolor">
<!--crap in the layout-->
</LinearLayout>
What did I miss?
I remember that I worked around this error by using state drawable instead of state color. For some reason layout background just doesn't work with stateful colors. So try creating a stateful drawable (for example list of shape drawables with different colors) and use it as background.
res/drawable/pressed.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff33ffff" />
</shape>
res/drawable/normal.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff000000" />
</shape>
res/drawable/background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/pressed" />
<item android:drawable="#drawable/normal" />
</selector>
Then use background.xml drawable as background :)
Instead of using shapes in your drawable, you can use the android:drawable attribute which accepts a color resource (e.g. #color/black).
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myview"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:background="#drawable/myDrawable">
<!-- other views in layout-->
</LinearLayout>
my_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- focused -->
<item android:state_focused="true" android:drawable="#color/YOUR_COLOR_HERE" />
<!-- focused and pressed-->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/YOUR_COLOR_HERE" />
<!-- pressed -->
<item android:state_pressed="true" android:drawable="#color/YOUR_COLOR_HERE" />
<!-- default -->
<item android:drawable="#color/YOUR_COLOR_HERE" />
</selector>
In my_drawable.xml you need to make sure that the colors you specify are defined in res/values/colors.xml, or this won't work.
If you want to use an image instead of a color change from a color resource to a drawable resource.
Example:
android:drawable="#color/YOUR_COLOR_HERE"
android:drawable="#drawable/YOUR_IMAGE_HERE"