I have a ListView with a custom list item layout and a selection state for that layout, also i have a checkbox inside a item layout, the problem is,the checkbox is avoiding list item be selected when is presented in layout, but when i change the visibility to gone of checkbox the layout for item respond to selectors.
selector_list.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CustomTextView
android:id="#id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp_8"
android:textColor="#color/gray_light_d2"
android:textSize="#dimen/sp_12"
custom:typeface="robotoBold" />
<CheckBox
android:id="#android:id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
Add the below attributes to the check box
android:focusable="false"
android:focusableInTouchMode="false"
or add the below to the relative layout an try.
android:descendantFocusability="blocksDescendants"
This happens because the checkbox takes focus when you click on the list item. So try the above suggestions
Related
I am using below layout xml as spinner row element (custom spinner). I see items (textView and ImageButton) getting displayed in spinner, but the item is not clickable (disabled), though i can click on ImageButton. I would like to have item especially text to be shown in the spinner when text is selected.
I am using android api 28.
Please suggest on how to do this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="#+id/buttonIcon"
android:scaleType="centerCrop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_remove"/>
</LinearLayout>
I'm trying to have a ListView where each row has some text and a checkbox. When I click on list view it will check or uncheck the check this is done but I want to disable the focus of checkbox i.e. the priority is given to list items not the checkbox here the code of checkbox and listview.
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkbox"
android:layout_margin="#dimen/login_field_margin"
android:layout_alignParentRight="true"
android:focusable="false"
android:selectAllOnFocus="false"
android:focusableInTouchMode="false"
/>
<ListView android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#id/listView"
android:dividerHeight="0.25dp"
android:focusable="true"
android:divider="#color/colorDarkgrey"
android:background="#color/colorwhite"> </ListView>
In your item layout xml , you can add this attr in root view
android:descendantFocusability="blocksDescendants"
try this. and here is the api for this attr.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<CheckBox
...
></CheckBox>
</LinearLayout>
Your item layout should like this.
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 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"
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.