Android Row becomes Unclickable with Button - java

I have a listView, where each row has a button in the row layout. However, this seems to make the row itself unclickable. How can I make both the button and row clickable?
Thanks.

You need to set itemsCanFocus on the list like this:
mList.setItemsCanFocus(true);
To make the button clickable. Then you will need to use your own adapter and in getView return a view which is Clickable and focusable. You will also lose the default highlight states so you need to put them back in with the background resource. So do this:
view.setClickable(true);
view.setFocusable(true);
view.setBackgroundResource(android.R.drawable.menuitem_background);
to your view before returning your view.

Whenever I see posts concerning the android:focusable and android:clickable attributes, I always see them being both set to the same value at once. I figured there must be a reason if they are two separate attributes instead of being one.
It turns out that a much better way of achieving your desired behavior is to set
android:focusable="false"
or
yourButton.setFocusable(false)
on the Button in your View. Once you do that, you'll be both able to set an OnClickListener on the Button, and a click on the row will fire the onListItemClick() method in your OnItemClickListener.

Try to set your widgets to non clickable and non focusable in xml,the click on items will work normally and also the click on button will work normally.
android:clickable="false"
android:focusable="false"
Hope this helps.

Unfortunately I don't think that is possible. You ListView row can either have focusable widgets, like a button, or be clickable, not both. See link.

Related

How to add a view to another view in a certain condition?

I'm working on an app and I want to add a view to the current view in a specific location if he checks a CheckBox but every time I try to add parent.addView(child) it crashes .. so I want to know how can I do this and how to release the view when the user clicks the CheckBox again.
That's a demo of what I want to make
https://xd.adobe.com/view/46d4808d-9888-455b-42ca-9fab587eb55d-30e5/
you can set the visibility in the xml file to gone and then set it back to visible when you click on the check box
xml:
android:visibility="gone"
jav:
view.setVisibility(View.VISIBLE)
You can create a Custom Component with the hidden content and reuse it in each checkbox. In addition, you can use a RecyclerView to add the checkbox. Or you can even create the custom component with the checkbox and hidden content and put all the logic there.

Listview items show they've been clicked when they haven't

Hey so i've got a listview with buttons on it, when one is clicked if you skip down 4 more listview items the fifth shows it's been clicked in the same place the previous one was clicked. When a button is clicked it is set to invisible and the dummy button underneath is shown (which is the one that shows up in every fifth row).
Code that declares button invisible in the onclick command
boolean processClick=true;
if(processClick == true)
{
myButton.setEnabled(false);
myButton.setClickable(false);
myButton.setVisibility(View.INVISIBLE);
}
processClick=false;
}
After looking at some similar questions, I think this has to do with the views being recycled, but still not sure how to remedy this.
Any helps at all is much appreciated.
Thanks!
Yes u are having problem with your code,you need to take processClick for each listView row/item and then on that bases u need to set enabled(false) or something else.You can take reference from this example which is similar work but with checkbox. You need to look at that code and apply with button. It'll solve your problem.
link of sample ex. https://drive.google.com/file/d/0B6C9Pqrvc_CWZHFDcmxKR01rc3c/edit?usp=sharing
I faced a similar problem when I was programatically assigning background color to my listview items. I dont know what caused the problem (probably the getView() was not getting called properly). I solved my problem by using if-else. In your case something like this should work:
if(processClick == true)
{//set desired result in if
myButton.setEnabled(false);
myButton.setClickable(false);
myButton.setVisibility(View.INVISIBLE);
}else{//reset items to original in else
myButton.setEnabled(true);
myButton.setClickable(true);
myButton.setVisibility(View.GONE);
}

CheckBox vs CheckedTextView in a ListView

When I place a CheckBox in the row layout for a ListView, I am no longer able to recieve OnItemClick and OnItemLongClick events. On the other hand, using CheckedTextView allows these events to go through, but I don't know how to automate clicking on them in my JUnit/Robotium tests. Does anyone have any suggestions about what I can do to get the best of both worlds here?
You can make OnItem(Long)Click work if you set up the CheckBox views to non-focusable (i.e. setFocusable(false) and setFocusableInTouchMode(false), or their xml equivalents).
Note: The listeners will fire as long as you tap outside the checkbox itself. Tapping on the checkbox will toggle it only (but I guess this is what you would want).

The tags for the GridView items are applying only when they are visible?

I have a GridView which is scrollable: only some of the items will be displayed and only after scrolling down the GridView. We can see the other items. The problem is, I am setting the tags for all the items in the GridView, but by tracing the LogCat I came to know that it is not accepting to set the tags for the non-visible items in the GridView(I mean the items which are inside the grid, but at that time are not on the screen)
Before scrolling:
After scrolling:
Only after scrolling down the other items the tags are applied.
How can I set the tags for all the items of the grid, even if they are on the screen or off the screen?
Your problem is that you set the Tag in the getView method and this method is only called by the framework when the view need to be displayed.
Don't know exactly what you need to do with the view tag, but I think that instead of trying to get the data from a non-displayed view, you must get it from your ImageAdapter (ia.imageid[]).
EDIT:
To answer more preciselly to your question:
How can I set the tags for all the items of the grid, even if they are on the screen or off the screen?
As soon as the view doesn't always exists when it is not displayed: you cannot do it.
I suggest you to explain why you need those tags... may be you can use an alternative solution than view tag to accomplish your final needs.
Anyway, if you really need to have tag on view that aren't displayed yet:
Initialize a view array in onCreate method.
Populate this array with all your views (and set the tag)
modify getView method of your adapter to return the view from your array of view instead of creating the view.
Please note that I don't recommend this solution since it will consume more resources.
Finally myself i got the solution for my question.Thank you for all who gave their valuable suggestions.
Thank you #ben75
i checked the position of present clicked item and then applied the tag so if the user want to select the item which is presently not on the focus he has to scroll the grid and then have to select the item and then only the tag will be applied.

Disabling focus in TextView

In an Android application I have a ListActivity. After creating it I have a broadcast receiver which has to disable some items in the list. So I have a pice of code ike this one:
View child = getListView().getChildAt(i);
child.setEnabled(false);
It works in the way it changes the color of the disabled views to gray. But I need to avoid this items can be clicked. So I've tried to call
child.setFocusable(false);
or
child.setClickable(false);
but in both cases onListItemClick is called after pressing on a disabled option.
How can I avoid that onListItemClick method is called when these textviews are clicked?
Thanks.
Disabling the views does nothing with regard to the onListItemClick event.
The child must be disabled by the Adapter you are using to populate the list.
See http://developer.android.com/reference/android/widget/ListAdapter.html#isEnabled(int)
This method needs to be overriden to return false for each item you do not want to be clickable. You should also add a method to your adapter like setEnabled(int position, boolean enabled)

Categories

Resources