Android: ListView selected item (onClick) stay selected after action - android

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.

Related

How to make ListView not respond (change background) on touch?

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" />

GridView with multi-selection does not display selected item state (only pressed state)

I have a GridView which has a ChoiceMode of GridView.CHOICE_MODE_MULTIPLE_MODAL. I'd like the selection to behave similarly to what you would see in the Gallery app (ie. long-press on an item, selected item has a semi-transparent foreground color to indicate it's selected, short-press on other items adds or removes them from the selection).
I've created a state selector like so:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#drawable/pressed_background" android:state_activated="true"/>
<item android:drawable="#drawable/pressed_background" android:state_checked="true"/>
<item android:drawable="#drawable/pressed_background" android:state_selected="true"/>
<item android:drawable="#drawable/pressed_background" android:state_pressed="true"/>
</selector>
Here is the pressed_background.xml file:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/pressed" />
</shape>
And the color it refers to:
<color name="pressed">#CC30d3dc</color>
I'm applying it to my grid like so:
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/header"
android:layout_marginTop="8dp"
android:columnWidth="112dp"
android:divider="#null"
android:dividerHeight="0dp"
android:drawSelectorOnTop="true"
android:listSelector="#drawable/selectable_background"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
And here is the XML for the grid item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" >
<ImageView
android:id="#+id/photo"
android:layout_width="#dimen/add_photos_image_size"
android:layout_height="#dimen/add_photos_image_size"
android:layout_centerInParent="true" />
</RelativeLayout>
The problem is that the selected state never appears on the grid item. When I start to long press, the pressed state color appears as expected on top of the grid item, but as soon as it becomes selected, the color disappears and there is no indication that the item is in a selected state. The contextual action bar appears as expected. I've tried as many different variations of the <selector> and the grid item background as I could think of.

Grid view item not hightlight when press

My app has a grid view, By default, Its item does not highlight when I click on it (I don't know why? ). I try to add it a list selector as below, But it does not work too,
<GridView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="#+id/load_more_process_id"
android:layout_alignParentTop="true"
android:drawSelectorOnTop="false"
android:listSelector="#drawable/grid_selector"
>
</GridView>
Here my selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#android:color/black"></item>
<item android:state_pressed="false" android:drawable="#android:color/white"></item>
If you have a focusable element in the list item contents (or the views for each grid item), the selector isn't drawn
For example, if the template for your list items is:
<LinearLayout android:orientation="horizontal" ... >
<CheckBox android:id="#+id/checkBox" ... />
<TextView android:id+"#+id/textView" ... />
</LinearLayout>
then ListView will not draw a selector for each ListView item, because the CheckBox is, by default, focusable.
You can either provide a background on each item that changes with selection state, or disable focus on all focusable elements (which in turn requires you to write a fairy fancy adapter to check the checkboxes when selection state changes.
eg:
<CheckBox android:id="#+id/checkBox" android:focusable="false" ... />
will cause an enclosing ListView to start drawing the selector again.
Example of a stateful drawable:
drawable/my_list_selector_bg.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_background_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/list_background_focused"
android:state_focused="true" />
<item android:drawable="#android:color/transparent" />
</selector>
You then apply that to the background of each view returned by your adapter:
<LinearLayout android:orientation="horizontal"
android:background="#drawable/my_list_selector_bg"
... >
Either approach will work.
The list adpater classes (GridView, ListView, &c) call hasFocusable() on each view returned by the adapter, and disable selection drawing if hasFocusable() returns true. Fortunately, they also replicate the selection/focus/pressed/active state to the currently focused or selected adapter item, so you can draw it yourself if you want.
For those of you who are having the same issue I was, try
gridView.setDrawSelectorOnTop(true);
I have an ImageView and a TextView in each of my GridView items. My selector was working, but it was drawing behind my image.
Layout of grid view:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:listSelector="#drawable/gridselector"/>
Create custom selector gridselector.xml like:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/selector_item">
<item
android:state_pressed="true"
android:drawable="#drawable/focus_offfocus">
</item>
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#drawable/focus_onfocus">
</item>
<item android:state_enabled="true"
android:drawable="#drawable/focus_offfocus">
</item>
</selector>

Android listview entire list getting selected

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"

Android list items break boundaries when using background drawables

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.

Categories

Resources