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>
Related
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 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
I have used SwipeListView to display some contents. i want the color of my Listview to turn to black when it is touched,swiped and till some other row is selected. How can i do that? I have tried the following but it did not help.
main.xml
<com.fortysevendeg.swipelistview.SwipeListView
android:id="#+id/example_swipe_lv_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
swipe:swipeFrontView="#+id/front"
swipe:swipeBackView="#+id/back"
android:background="?android:attr/activatedBackgroundIndicator"
android:listSelector="#drawable/list_selector"
android:choiceMode="singleChoice"
swipe:swipeDrawableChecked="#drawable/choice_selected"
swipe:swipeDrawableUnchecked="#drawable/choice_unselected"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeMode="both"
/>
#drawable/list_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="#drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/list_item_bg_pressed" android:state_activated="true"/>
<item android:drawable="#drawable/list_item_bg_normal" android:state_activated="true" ></item>
</selector>
On Scrolling Listview items(Views) are reused. To keep the existing state of the View you have to use a ViewHolder concept.
Check out the links below:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
findViewById vs View Holder Pattern in ListView adapter
Store the selected item in an Array. Keep checking if the item exists and change the background color in getView()
I have a ListFragment containing a multiple choice ListView. If I set the layout in my CursorAdapter to android.R.layout.simple_list_item_multiple_choice, I will be able to see the checked items in my ListView. But I want to use a custom layout and change the background color when the item is checked. It seemed rather simple but I couldn't find the answer to my problem. I thought about setting the color in the bindView of my adapter but I'm not sure it's where it belongs, since my adapter doesn't know which items are checked. So I tried to use a selector and apply it on the background of my textview like the following :
row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="6dp"
android:background="#drawable/list_selector"/>
list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed -->
<item android:state_pressed="true"
android:drawable="#drawable/list_pressed" />
<!-- Selected -->
<item android:state_selected="true"
android:drawable="#drawable/list_active" />
<!-- Checked -->
<item android:state_checked="true"
android:drawable="#drawable/list_active" />
<!-- Normal -->
<item android:drawable="#drawable/list_normal" />
</selector>
The pressed state is working but not the checked one. I wonder if my TextView is even checked at all. But if it's not, how does the ListView keep track of the checked items?
The source for android.R.layout.simple_list_item_multiple_choice uses a CheckedTextView, so you are on the right path. Just change the TextView in your layout to a CheckedTextView.
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>