I am creating an App, in which I am showing information from a database in a ListView, using a custom row layout, which is populated using a SimpleCursorAdapter. I also handle onItemClickListner for the ListView.
My Layout of the custom rows is as follows:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:focusableInTouchMode="true"
android:padding="10dip" >
<ImageView android:id="#+id/imageButtonAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:src="#drawable/icon_account" />
<TextView
android:id="#+id/TextViewAccount"
style="#style/textView_normal_bold_style"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dip"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="#dimen/text_size_large" />
</LinearLayout>
But I have one issue, which follows:
When I click above content of ListView row, it performs only click event, but what I want is when I click any where in a row then a click action should be performed. But it does not behave like this.
The XML of your row should have the attribute
android:layout_width="fill_parent"
Related
I've got a ListView with a custom Adapter inheriting from ArrayAdapter. I'm using a custom Checkable subclass of LinearLayout to highlight the last selected item in the list where it basically maps the checked state to selected.
The layout used by the Adapter for list items is as follows:
<com.wordlistdictionary.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<LinearLayout
android:id="#+id/entry_text"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:duplicateParentState="true"
/>
<TextView
android:id="#+id/entry_number_of_occurences_1"
android:textSize="9pt"
android:typeface="monospace"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/item_text_color_selector"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:duplicateParentState="true"
/>
<TextView
android:id="#+id/entry_frequency_1"
android:textSize="9pt"
android:typeface="monospace"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/item_text_color_selector"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:duplicateParentState="true"/>
<ImageButton
android:id="#+id/delete_entry_button_1"
android:src="#drawable/delete_button_selector"
android:layout_height="40dp"
android:layout_width="40dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="15dp"
android:duplicateParentState="true"
/>
</com.wordlistdictionary.CheckableLinearLayout>
It works as I expect it with the text color changing for the last touched item until I'm adding the last ImageButton view and it stops highlighting the text from that point.
Has anybody encountered this and what was your solution?
Currently I'm thinking of a workaround with manually propagating the selected state to all child views from my custom layout as opposed to just changing the selected state of the layout view itself and relying on the duplicateParentState mechanism.
It turns out when added to a list item a Button or ImageButton "swallows" the touched event so it doesn't propagate to the list view so I had to set imageButton.setTouchable(false) in getView() in my custom Adapter.
Note that I first tried to set android:touchable='falase' in my layout file but it didn't work so I had to set it programmatically.
I've found the answer in these 2 SO questions:
ListView setOnItemClickListener not working by adding button
Click is not working on the Listitem Listview android
I have a ListView that inflate for each row a xml that contain a CheckBox and more TextViews that are in a RelativeLayout.
The problem is that I can't figure out how to pass onClick events from checkbox to back row.
I want to achieve this behavior: The user press the checkbox and the whole list row gets pressed. I saw this behavior if I inflate for each row android.R.layout.simple_list_item_multiple_choice but I can't figure out how to do that without this android specific layout.
Can anybody give me an idea or direction?
Below is the code:
Listview :
<ListView
android:id="#+id/include_sent_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:cacheColorHint="#color/white"
android:layout_alignParentTop="true"/>
And the xml that is inflated for each row:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="#+id/checked_radio_button"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:focusable="false"
/>
<TextView
android:id="#+id/account_number_text"
android:layout_width="100dp"
android:layout_height="fill_parent"
style="#style/config_simple_small_text_view_style"
android:paddingTop="15dp"
android:layout_toRightOf="#id/checked_radio_button"
/>
<TextView
android:id="#+id/account_name_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/config_simple_small_text_view_style"
android:paddingTop="15dp"
android:layout_toRightOf="#id/account_number_text"/>
</RelativeLayout>
The Checkbox consumes focus for the List item. You need to set this in your layout file:
<!-- Must make this non-focusable otherwise it consumes -->
<!-- events intended for the list item (i.e. long press) -->
<CheckBox
android:id="#+id/item_entry_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
/>
I have ListView with layout for row like which I inflate at adapter ( extend BaseAdapter ) at getView method
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="#+id/chk"
android:button="#drawable/q_list_check_box"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
/>
<TextView
android:id="#+id/txtChoice"
android:textColor="#color/white"
android:layout_gravity="center_vertical|left"
android:gravity="center_vertical|left"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I put at ListView tag android:choiceMode="singleChoice". How to make list single choice, that only one row can be checked at time ?
When I had to implement selection with my custom row., I used an arraylist of boolean to keep the state of the row. The size of boolean arraylist is same as rows of the listview. You can select/deselect the row via changing the value of corresponding boolean in OnListItemClick(). Anyways its just an idea. Here is a link that can help to understand this:
Custom List row with checkbox
I have this ListView whose items i'd like to hide depending on the selection of a RadioGroup. Currently I'm passing a boolean to the ListAdapter due to the RadioGroup having only two options. My items contain a checkbox and i want to either show the entire list or just the ones with the check boxes checked. I'm succeeding at hiding the items but the dividers still show, how can i fix this?
Look how it looks like
http://www.mediafire.com/i/?wa2s0ngq027vjwr
http://www.mediafire.com/i/?9i6ggj2fdsns2da
(I'm new, so i can't upload images here)
The xml for my row would be:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="1dip" android:gravity="center_vertical"
android:background="#FFF">
<CheckBox android:id="#+id/dispositivo_tv"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#000000" android:textSize="15dip"
android:layout_alignParentLeft="true" />
<LinearLayout android:id="#+id/botones"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentRight="true" android:gravity="center_vertical">
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/button_foto"
android:src="#drawable/camera" android:background="#FFF"
android:paddingRight="15dip" android:visibility="invisible"></ImageButton>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/button_comentario"
android:src="#drawable/comment_add" android:background="#FFF"
android:paddingRight="15dip"></ImageButton>
</LinearLayout>
</RelativeLayout>
and the xml block for the ListView would be:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:padding="5dip" android:background="#layout/list_box">
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:cacheColorHint="#00000000"
android:headerDividersEnabled="false" android:footerDividersEnabled="false
</ListView>
</LinearLayout>
and what i use to hide the row when the boolean i told you about is set FALSE is:
wrapper.getDispositivo().setVisibility(View.GONE);
wrapper.getFoto().setVisibility(View.GONE);
wrapper.getComentario().setVisibility(View.GONE);
PS: wrapper is the instance of the class where i have all the elements of the row, i.e. the checkbox (getDispositivo()), and a couple of image buttons (getFoto(), getComentario())
Thanks in advance...
How about using custom dividers in your relative layout and setDivider(null); so once you hide the layout the dividers are hidden as well. I wanted to actually add this as a comment. But it comes only after 50 reps so had to put it as a answer.
I have a ListActivity working as expected. When a list item is clicked the app responds properly. I want to add a checkbox on the far right side of the screen, next to each list item. When I attempted to do that by updating the XML (see below) I lost the onListItemClick functionality. When I clicked on a list item nothing happened.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="#+id/table1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<TextView android:id="#+id/tvProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingLeft="10sp"
android:paddingRight="10sp" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:textColor="#606060" />
</LinearLayout>
</LinearLayout>
I am trying to add the checkbox next to the tvProduct object.
Any help is appreciated.
When a ListView has focusable (clickable) children, it loses focusablitity itself. You can either have a checkbox (button, etc) by clickable inside of a ListView, or have the entire row be clickable.
If you are trying to allow the user to select multiple rows and show the selection state in a checkbox, you can use the choiceMode CHOICE_MODE_MULTIPLE.
Here is an example of it in use.
In the adapter in the getview method, set the checkbox use checkbox.setFocusable(false) and checkbox.setFocusableInTouchMode(false). You can still click the checkbox, but clicking anywhere else gets the list item. Both work fine.
I know there was already an answer, but this has worked for me in the past and is easy to implement so I wanted to add it.