android View with View.GONE still receives onTouch and onClick

AndroidViewOnclickVisibility

Android Problem Overview


This is confusing me:

As far as I have read, a view with setVisibility(View.GONE); should not receive any more touch- or click events. My layout has two parts, which will be visible or gone so that only one of them is visible and usable at a time but View.GONE doesn't do the trick. I can't see the GONE view, as expected, but it still reacts to onClick (and consumes the event the other view should get).

Can you help me?

Maybe of interest: When I start my project one view is GONE, the other visible. This time it will work as expected (the GONE view is basically ignored), but after setting View.GONE through the code it'll stop working.

Android Solutions


Solution 1 - Android

Do you maybe use animations to show/hide the views? I get this behaviour when I use animations that have android:fillEnabled="true" android:fillAfter="true" Don't understand it, and seems like a bug - if I use animations without fillEnabled/fillAfter, all works as expected...

Solution 2 - Android

If you set setVisibility(View.GONE) after some animation (fade out, for example), then try clearing the animation with clearAnimation(). This is what helped me.

Solution 3 - Android

Try setting clickable property to false using setClickable(false) after setVisibility(View.GONE)

Solution 4 - Android

Yes,mview.clearAnimation() have some issuses but amination.setFillAfter(false);and mview.setClickable(false); WORKS perfect .

Solution 5 - Android

What I expect is happening is that you make a view invisible, but that views children still respond to clicks (ie your view is a ViewGroup). You could do something along the lines of:

private void hideTheChildren(View v){
	if(v instanceof ViewGroup) {
		int count = ((ViewGroup)v).getChildCount();
		for(int k = 0 ; k < count ; k++) {
			hideTheChildren(((ViewGroup)v).getChildAt(k));
		}
		v.setVisibility(View.GONE);
	}
	else {
		v.setClickable(false);
		v.setVisibility(View.GONE);
	}
}

of course then you also have to do the opposite

private void showTheChildren(View v){
	if(v instanceof ViewGroup) {
		int count = ((ViewGroup)v).getChildCount();
		for(int k = 0 ; k < count ; k++) {
			showTheChildren(((ViewGroup)v).getChildAt(k));
		}
		v.setVisibility(View.VISIBLE);
	}
	else {
		v.setClickable(true);
		v.setVisibility(View.VISIBLE);
	}
}

This has worked for me in the past. I don't currently know of a better way to do this.

Solution 6 - Android

I would have post this as a comment, but unfortunately I was not able to post a comment. As it could be a possible solution for you, i post it that way:

As you write "onClick" I assume you're using the onClick attribute in your XML layout. Try to set an OnClickListener with setOnClickListener instead of the onClick attribute. Maybe this helps...

Solution 7 - Android

For those that did the answers above and still didn't solve their problems, I recommend removing the view from the parent view. If you need display the view again, just make a copy and add it to the parent view.

This might seem overkill but I was hiding / showing whole view groups in your case it might be a button, textview or image, this solution will still work.

Solution 8 - Android

try adding .clearAnimation() in the onAnimationEnd override.

Solution 9 - Android

if have a animation at the view, you should call view.clearAnimation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShadowMareView Question on Stackoverflow
Solution 1 - AndroidedovinoView Answer on Stackoverflow
Solution 2 - AndroidPavel AlexeevView Answer on Stackoverflow
Solution 3 - AndroidAnilView Answer on Stackoverflow
Solution 4 - AndroidHemant ShoriView Answer on Stackoverflow
Solution 5 - AndroidSiebeView Answer on Stackoverflow
Solution 6 - AndroidM.E.View Answer on Stackoverflow
Solution 7 - AndroidKim MontanoView Answer on Stackoverflow
Solution 8 - AndroiddoodlleusView Answer on Stackoverflow
Solution 9 - AndroidlingyfhView Answer on Stackoverflow