83

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.

Mateus Gondim
  • 5,362
  • 6
  • 31
  • 51
ShadowMare
  • 2,087
  • 2
  • 25
  • 27
  • 1
    Is this happening immediately after calling View.GONE? Only for a limited amount of time? It might have to do with the fact that UI changes don't take affect immediately. – Cheryl Simon Jan 18 '11 at 21:11
  • 1
    We need some more info. Try using the `hierarchyviewer` tool to inspect the state of your view hierarchy at runtime. This will let you see visibility state and much more when debugging view issues. – adamp Jan 19 '11 at 00:17
  • Thank you. This is happening immediately after calling View.GONE. The hierarchyviewer tool says that the view is GONE but it is still getting onClick's. My plattform is cyanogenmod 2.2 (but I expect this is still a problem in my code :D) – ShadowMare Jan 20 '11 at 20:11
  • I have the same problem without animations. Even when setting every element in a ViewGroup to GONE, I can still select the content of a GONE EditText and get the Keyboard. – philipp Mar 26 '14 at 23:26
  • After View.GONE, element of layout receive onClick() event. This has Animation View and Gone. How to block this? – CleanCoder Jun 27 '20 at 06:12

9 Answers9

122

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...

edovino
  • 3,315
  • 2
  • 22
  • 22
  • 2
    Is there any work-around available ? I'm using nineoldandroids and seeing this behavior on API10 - I think it uses fillEnabled to simulate the 3.0 API behavior of modifying view properties after animation. – Rafael Nobre Feb 08 '13 at 14:20
  • 1
    I'm having this problem with Android Google 4.4.2 APIs too. – GuilhE Sep 30 '14 at 17:02
  • 1
    Thanks for hint, removing android:fillAfter="true", attach animation listener with `code @Override public void onAnimationEnd(Animation animation) { /* Android will make an exception when you change the view hierarchy * in animationEnd. http://stackoverflow.com/a/7445557/587186 */ new Handler().post(new Runnable() { public void run() { if (interventionPreview != null) { interventionPreview.setVisibility(View.GONE); } } }); } ` did the thing, hope it helps. – fox Feb 04 '16 at 14:46
  • Thanks for saving me from a minor headache – vanlooverenkoen Jul 10 '16 at 11:03
  • But fillAfter need for stable view after complete. Then what should I do ? – W00di Sep 09 '16 at 08:31
  • Works for me, after so many hours trying to fix this problem... Thanks! – AliMola Feb 16 '17 at 12:51
  • 1
    This issue is still there in 2022. Unbelievable! Thanks. – dazed Mar 16 '22 at 17:53
113

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.

shyam
  • 596
  • 9
  • 18
Pavel Alexeev
  • 6,026
  • 4
  • 43
  • 51
  • 3
    This is a better answer. There are scenarios where you want to use `android:fillAfter` and `android:fillEnabled` in your animations and you want the view to stop responding. – Mick Byrne Sep 28 '12 at 23:13
  • 1
    This one worked for me, but since my animation will last for one or two seconds then `clearAnimation()` stops the animation and I cannot see the fade in fade out animation. Instead of that one, I used `alpha.setFillAfter(false);` within my alpha animation and it worked exactly as I expected. – osayilgan Dec 18 '12 at 14:59
  • 9
    This is the best aswer, thank you! @osayilgan , in this case you just need to set an onEndAnimation listener and clearAnimation() there, after the animation finishes. – Rafael Nobre Feb 08 '13 at 14:31
  • I believe this answer is better than the accepted one, since the fillAfter behaviour may be desired. Thank you, you saved me a world of hurt. – ssawchenko Aug 01 '13 at 16:46
  • 2
    @osayilgan you should put `clearAnimation()` call into `onAnimationEnd()` method. Then it works as expected. – Saran Feb 17 '14 at 14:34
  • Yeah, this solution works like a charm and looks more elegant and safer than the accepted solution. – mdelolmo Jan 06 '15 at 18:08
  • Perfect. With my upvote, this answer has a higher score than accepted answer (for a better visibility) ;) – mrroboaat Aug 14 '15 at 13:15
  • Worked great for me. I can't believe how long I spent on this bug! – GONeale Jul 11 '16 at 05:42
  • Is it only me that spent several hours struggling in the same damn android bug that has upvoted its solution many years ago?! – Hamzeh Soboh Jul 12 '17 at 05:49
  • Call `clearAnimation()` on whichever `View` you called `startAnimation()`. – nimi0112 May 08 '18 at 12:32
8

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

Anil
  • 91
  • 2
6

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

Hemant Shori
  • 2,463
  • 1
  • 22
  • 20
2

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.

Siebe
  • 320
  • 1
  • 3
  • 11
  • 1
    After 8 years this is still an issue. This will work as long as you don't have another view behind. – Tamoxin Mar 21 '19 at 13:08
1

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.

Kim Montano
  • 2,185
  • 4
  • 26
  • 39
  • 2
    This is actually the only thing that worked for me. NOTE: I was using a view to cover a another view with `BottomSheetBehavior` while loading data. With all other answers, the view kept intercepting the touches/gestures. And this broke my `BottomSheetBehaviour` | Android 8.0 with design-support-lib 27.0.2 – Entreco Jan 25 '18 at 20:54
1

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...

M.E.
  • 536
  • 2
  • 9
  • 19
0

try adding .clearAnimation() in the onAnimationEnd override.

doodlleus
  • 575
  • 4
  • 14
0

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

lingyfh
  • 1,363
  • 18
  • 23