Android - Why setClicable(true) sets item non-clicable and vice versa? - android

According to developers' site View.setClicable(true) should
make the view clickable, false otherwise
, so why is it reversed in real? Only setClicable(false) makes my View clicable. Have I found a mistake in API description?
EVIDENCE:
gridview.getChildAt(position).setClickable(true);
Feel free to try it at home. After this the child in gridview won't be clicable.

I assume the clicking you're referring to is what you can listen to with an OnItemClickListener. This is handled entirely by the GridView (the GridView is also what draws the selector on the view), not the child view. You can control which items the GridView should consider as clickable with isEnabled in your adapter.
When you make the child view clickable it will handle all touch events over it. Because of this the GridView will not be notified of touch events on that view, and as such can't handle the click event.
So your view is perfectly clickable, you'd just have to use an OnClickListener to get notified of the clicks.

I think you have misunderstood what is is saying. It says exactly what you said. You pass true to make the view clickable or false otherwise (i.e to make it not clickable).

Related

How to attach onClickListener to an invisible view

I want to attach an onClickListener to an ImageView which is made INVISIBLE. I know I could set it to the TRANSPARENT color but due to certain reasons I don't want to do that and an INVISIBLE ImageView is not listening to clicks. Is there any way to achieve the required thing?
No, INVISIBLE views don't receive touch events. However there are a few alternatives you can use:
Set the view's alpha to 0. This would make it fully transparent.
Create a 2nd view of the exact same size above the view and put the click handler on that.
Put a touch handler on the parent and check if in the area of the invisible view when you detect a click.
Least work is probably top to bottom on that list.

Android: Detect if user touches a view inside parent view

I have 40 ImageViews inside a GridLayout, they have different colors and I want to know if user touched the desirable image or somewhere else(for example if user touched red image). How should I do that?
Set an OnClickListener for each view and store the views. In onClick you can check the view and know what ImageView was clicked. You could also think about changing the type to ImageButtons!
If you have further problems with your Grid not being able to be clicked, check this out: http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/
TLDR: Disable focusing of the views in your layout.
If you manage your images in a collection maybe think about switching to gridview and implement an adapter with an itemClickListener. So depend on which item is clicked do you action.
How GridLayout items come from the Adapter(List/Image) in Android App

Make a listview programmatically selectable only

I have a Listview in a Fragment, and when I click an item a new Fragment is shown.
The problem is that for few milliseconds i can see the item selected by the touch. Is there a way to make these Listview programmatically selectable only? For example when I return to the Fragment and something connected is running (i already know how to select an item in Java, i just need to know how to disable touch clicks).
The item is colored with a selector and the Listviews are set to choice mode single.
One way to do it would be to implement the ontouchListener() of the fragment, and rewrite the onTouch() method with a simple:
return false
to consume the event.
You can achieve this quite easily. The two ways that are on top of my head are :
Disable touch on the list.
Put your ListView in a RelativeLayout (rootLayout) in the RelativeLayout(rootLayout) first item should be your ListView and the second can be another RelativeLayout(coverLyt) with height and width set as match_parent and clickable set to true. This will make the coverLyt take the touch events instead of your ListView. When you want the listView touch events to work set coverLyt's visibility to gone and visible when vice versa.

Setting the onClick method of a view in a Gallery disables the Gallery's fling

Each of the Gallery's panes hold more than a single view and I need to differentiate between their clicks - AdapterView.OnItemClickListener() just indicates that the Gallery was clicked. What's the best way to get both Gallery flinging and view clicking simultaneously?
In your click listener, return false. Returning false means that the event was not consumed and to propagate the event to other views who may be watching the same event.
Some part of your item layout shouldn't contain a clickable item. If you touch that part and try swiping, it should work.
This does exactly what I needed:
https://github.com/pakerfeldt/android-viewflow

Android route / bubble events because a control is consuming the onClick event

I have a custom view with an ImageView and a TextView on it and implemented the onClickListener for my custom view.
The problem is, that the ImageView is consuming the onClick-event (I just want the user be able to click on my control, no matter where). I could listen to the onClick of the Image/TextView too, but it seems dirty to me.
Is there a way to bubble / route Events in Android? Or possible another good solution?
View.onClick() event does not bubble. Two possible solutions:
Register OnCLickListener on you child views and then pass on the event by calling performClick() on parent.
Use OnTouchListener which bubbles up: just return false in child view's onTouch() method. This is more work as you have to account for touch-down & lift-up in order to emulate click.
Have you set the onClickListener in your custom view?
Set your custom view as clickable.
I don't recommend on setting any click listener in the child views.
Does it work now?

Categories

Resources