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

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

Related

ListView items' borders change color (like temporally highlighted or flicker) when clicked?

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

Android checkbox is not visible well in black background prob

I want make a list of Checkbox with white text in black background. We are using following code:
CheckBox chkAdditionalPack = new CheckBox(MainActivity.this);
chkAdditionalPack.setTag(j);
chkAdditionalPack.setText(offerPackageListForAddl.get(j).getOfferPackageName().toString());
chkAdditionalPack.setTextColor(Color.WHITE);
It gives a view like below:
The problem is now boxes of checkboxes is not clearly visible. How can I make it clearly visible keeping intact other parts?
If you are using android-studio, select your checkbox, and in the properties, set 'buttonTint' property value to whatever color you want using the editor. Or, if you prefer the XML solution, use
android:buttonTint="#color/white"
Note: This assumes that the color white is defined.
try the below xml code ref from solution
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="false" >
</item>
<item
android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="true" >
</item>
<item
android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="false" >
</item>
<item
android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="true" >
</item>
</selector>
One really simple way to do it, within the xml for the checkbox, simply set the android:background variable to the colour you wish.
Instead of having the text and check box created together like this, simply create a custom list item layout xml file.
e.g custom_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
/>
</LinearLayout>
Simply use this :
android:buttonTint="#FFFFFF"
** replace #FFFFFF with any color

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>

ListView Selector not working with ListViewItem's background set

I have a ListView that has a selector set with just a solid color.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/assignmentSelectedItem" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/assignmentsListView"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:listSelector="#drawable/assignment_list_selector" />
</LinearLayout>
assignment_list_selector.xml -
<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/transparent"/>
</selector>
In the ListView.Adapter for my list, I have items that have their view's background set to a specific color. My question would be is that why the selector is not working at all in displaying a selected color for my list view? If so, any suggestion of a work around with allowing me to still set the view's background color.
You can use ListView property android:drawSelectorOnTop="true" in your layout .xml file in order to made list view to draw the selector over the list item rather than under them.
If you wonna have item's background + list selector functionality you can try another approach: Use transparent list selector. And modify your selector in order to use as a background for the list item. For example
<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/your_list_item_color"/>
</selector>

Android Selector Problem

i have a listview with a custom item_row.xml. I've defined a selector in this way:
<?xml version="1.0" encoding="utf-8"?>
<item
android:state_pressed="false"
android:drawable="#drawable/list_bg" >
</item>
<item
android:state_pressed="true"
android:drawable="#drawable/header_bg" >
</item>
<item
android:state_focused="false"
android:drawable="#drawable/list_bg" >
</item>
<item
android:state_focused="true"
android:drawable="#drawable/header_bg">
</item>
and then put into item_row.xml in this way:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:orientation="vertical"
android:background="#drawable/list_selector">
I want 2 things:
When i move with arrow keys, the item selected changed its background. It's works fine with the actual implementation of selector.
When i press a item, the item changed its background too, but it doesn't work with the actual selector.
Any idea? I try to set also into the ListView android:listSelector="#drawable/list_selector"
but it doesn't work neither.
Thanks
You should model your StateListDrawable after the one used by Android itself for ListView selectors. You can find this in $ANDROID_HOME/platforms/$VERSION/data/res/drawable/list_selector_background.xml, where $ANDROID_HOME is where your Android SDK is installed and $VERSION is some Android version (e.g., android-2.1). Then, apply the list selector via android:listSelector in the ListView in your layout XML.

Categories

Resources