I created a custom layout (using LayoutInflator) for list items. Here are the relevant files:
my_list.xml - the main activity which has the ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/listitem_selector"
>
</ListView>
list_item.xml - references drawable .png for background
android:background="#drawable/list_item_bg"
listitem_selector.xml
<selector>
<item android:state_focused="false" android:drawable="#drawable/list_item_bg" />
<item android:state_pressed="true"
android:drawable="#drawable/list_item_pressed_bg" />
<item android:state_focused="true"
android:drawable="#drawable/list_item_pressed_bg" />
</selector>
If I don't use the listitem_selector.xml to override the default state styles, I get the ugly default Android green/orange colors, but otherwise the layout looks fine. When I use the listitem_selector.xml, it adds extra space at the top of each list item. (if I use smaller images, it still adds space)
here's a screenshot: [edit - not screenshots allowed.]
How can I override the default list item state styling without adding the weird space?
p.s. - I referenced this very helpful post to get where I got.
Related
I have a problem with my ListView. If I click an item, this item is highlighted (this is ok) and another activity starts to edit details or to delete a dataset. After coming back to my list the item is still selected, even if it is no longer available...
<img src="https://i.stack.imgur.com/7Xi1z.png">
<img src="https://i.stack.imgur.com/JaqCP.png">
<img src="https://i.stack.imgur.com/p3OAU.png">
here should be some pictures that i cannot insert unfortunately because the editor thinks it is code ...
Only if I use the back-narrow (left of "Vertrag) the item is no longer highlighted. If I use another menu item the item stays highlighted. Also if I use the FloatingActionButton the item in my list stays highlighted.
How can I remove this highlighting? I already tried clearChoices() in different ways but it did not work.
My fragment_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContractList">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/colorListDivider"
android:dividerHeight="#dimen/divider_height"
android:listSelector="#color/colorListSelected"
android:cacheColorHint="#color/colorListcacheColorHint"
android:drawSelectorOnTop="false" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/nodata"
android:visibility="invisible"
android:background="#color/colorListEmptyBackground" />
</RelativeLayout>
EDIT: - - - - - - - - - - - - - - - - - - - - -
I got the solution:
I set
android:listSelector="#drawable/list_selector"
and created an drawable xml file list_selector.xml with
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:state_selected="false"
android:drawable="#color/colorListBackground" />
<item android:state_pressed="true">
<shape>
<gradient
android:startColor="#color/colorListSelected"
android:endColor="#color/colorListSelected"
android:angle="270" />
</shape>
</item>
<item android:state_pressed="false"
android:state_selected="true"
android:drawable="#color/colorListBackground" />
</selector>
So the list item highlights with my custom color and does not stay highlighted after selection.
Please remove this from your fragment_xml's listview: android:listSelector="#color/colorListSelected"
or use
android:listSelector="?attr/selectableItemBackground" instead
and if it does not work, you may like to use your own custom style.
Each item in the ListView consists of a CardView that has a TextView inside.
This is not a bug this is just the normal way android listview shows that the item was clicked (like highlighting it till you release touch). (u have seen it right?)
But i dont want this functionality as I handle the clicks manually. How to remove this?
To remove selector color try this(add transparent list selector color):
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#android:color/transparent"
/>
To change it, try below:
First create a selector XML (listitem_selector.xml) file in your drawable folder,
<!-- pressed state-->
<item android:drawable="#drawable/button_selected_state" android:state_pressed="true"/>
<!-- focused state-->
<item android:drawable="#drawable/button_selected_state" android:state_focused="true"/>
<!-- default state-->
<item android:drawable="#drawable/button_normal_state"/>
Then in your Layout XML, Find your listview and add
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#drawable/listitem_selector"
/>
Or you can add selector for your row items.
For that, you need to set background drawable of the row layout as
android:background="#drawable/listitem_selector"
and you need to set your listview's list selector like that:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#android:color/transparent"
/>
I have a ListView which displays some inert items, so this should not change their backgrounds when a user touches the list view or item. This turns out hard to do:
What i have tried so far:
Java:
listView.setLongClickable(false);
listView.setClickable(false);
listView.setFocusable(false);
This has no effect what so ever. If I touch listview the background changes.
In the xml I tried;
<ListView
android:id="#android:id/list"
android:choiceMode="none"
android:clickable="true"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:listSelector="#null" />
Setting listSelector to #null is something I saw elsewehere on stack overflow. Does not work.
I also tried setting each item's background in code to transparent.
convertView.setBackgroundResource(R.color.transparent);
Nothing. On every touch there is always a change in background to blue.
How to make my listview not change item background on touch?
Try This, It works For Me
<ListView
android:listSelector="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
/>
You can set view.setEnabled(false); in your Adapter getView() - it will remove background effects.
You are probably using a layout for your list items that has a background which has a different selected state.
See what layout you use for the list items and change that to not include that background. If you are using the default (android.R.layout.simple_list_item) layout, you can create and use your own layout.
for this you need to make a custom background for the listview like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#android:color/white" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#android:color/white" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#android:color/white" />
<item
android:state_enabled="true"
android:drawable="#android:color/white" />
</selector>
place this xml file with name listbg.xml inside your drawable folder
<ListView
android:id="#+id/listt"
android:choiceMode="none"
android:clickable="true"
android:background="#drawable/listbg"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:listSelector="#null" />
I am using a selector working fine in all activities as you can see its XML below.
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/active_date_horizontal_5_pressed" /> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/active_date_horizontal_5_pressed" /> <!-- focused -->
<item
android:drawable="#drawable/active_date_horizontal_5" /> <!-- default -->
Except one activity, it does not work ridiculously. You can see three screenshots below which the selector works fine in the first two screenshots, and does not work in the last screen shot.
1)
2)
3)
These are customized GridViews which filled with customized BaseAdapter. Cells in the GridViews have an Xml Layout which its background is the selector. Any ideas?
It seems I solved my own problem.
You can see the Xml Layout which I am applied the selector below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#drawable/calendar_cell_selector" >
<TextView
android:id="#+id/txtWeeklyEventName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imgCornerWeekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtWeeklyEventName"
android:layout_alignRight="#+id/txtWeeklyEventName"
android:src="#drawable/corner_orange"
android:visibility="visible" />
</RelativeLayout>
You can see the background of the RelativeLayout is the selector, but the TextView fills upon the whole layout so Android cannot handle the selector.
Thus, I removed the background from the RelativeLayout and set it for the TextView, it totally works.
Thanks to me!
I seem to be having a UI problem with a listview. I have a listview and selecting any item on the list, highlights the entire listview. The selection is working fine, I get the correct item in the listener.
But the problem is that when i select any item, the entire listview gets highlighted, so it is difficult to tell which row got selected.
This is working fine on Android >3.1 - Currently i am testing on a 2.3 device.
<ListView
android:id="#+id/myList"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="#dimen/margin_medium"
android:background="#drawable/border"
android:listSelector="#99000000"
android:scrollbarFadeDuration="1000000"
android:scrollbars="vertical" >
</ListView>
I recently had the same issue, but the reason was in Android < 3.0 bug: if you set a #color as a drawable for your list item pressed selector, then it will fill the entire list area. Solution is to use a 'shape' drawable instead of #color.
So, create a new XML file in res/drawable folder with similar content:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/my_list_item_pressed_color" />
</shape>
And reference created drawable in your ListView selector for state_pressed (also created as an XML file in res/drawable folder):
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/list_item_pressed_bg" />
<item android:drawable="#android:color/transparent" />
</selector>
And use this selector in your ListView:
<ListView
.....
android:listSelector="#drawable/list_item_selector" />
That's all. Works at least with Android 2.2-2.3.
If you don't want to change your selector, you can also set it as the background of the list item's layout, rather than in the listSelector field of the ListView. The list items receive the "selected" state when touched and the color is displayed properly.
This works for all versions of Android.
Ex: This is your list item layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_color">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your text here" />
</LinearLayout>
And your list with no list selector:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I fixed this by removing this line: (not sure why it worked)
android:listSelector="#99000000"