19

I have an activity that has buttons and images that can appear and disappear depending on user interactions.

What I am finding is that objects at the back, which have been set to invisible, are still triggering clicks, sort of. They are not processing the code related to being clicked, but they sort of momentarily reappear, and then disappear again instantly when clicked on.

They also appear to be interfering somewhat with buttons laid over the top of them. These buttons become very fiddly and difficult to click at times, when there is an invisible object behind them.

I am using simply:

object.setVisibility(View.VISIBLE);

And:

object.setVisibility(View.INVISIBLE);

To make my items appear and disappear. Is this not what I should be doing?

EDIT:

People keep asking me for the exact same code that they are giving me. This is the code I have been given, and that I am using currently.

        btnLifePlus5.setVisibility(View.GONE);
        btnLifePlus5.setFocusableInTouchMode(false);
        txtLifePlus5.setVisibility(View.GONE);
        txtLifePlus5.setFocusableInTouchMode(false);
        btnLifePlus1.setVisibility(View.GONE);
        btnLifePlus1.setFocusableInTouchMode(false);
        txtLifePlus1.setVisibility(View.GONE);
        txtLifePlus1.setFocusableInTouchMode(false);
        btnLifeMinus5.setVisibility(View.GONE);
        btnLifeMinus5.setFocusableInTouchMode(false);
        txtLifeMinus5.setVisibility(View.GONE);
        txtLifeMinus5.setFocusableInTouchMode(false);
        btnLifeMinus1.setVisibility(View.GONE);
        btnLifeMinus1.setFocusableInTouchMode(false);
        txtLifeMinus1.setVisibility(View.GONE);
        txtLifeMinus1.setFocusableInTouchMode(false);

This makes no difference to just setting them as invisible.

Bisclavret
  • 1,327
  • 9
  • 37
  • 65

7 Answers7

25

Making any View invisible don't prevent us to trigger their listeners. It's just you can not see it every other thing would be same as if it was visible.

If you don't want to use it at all change it to View.GONE

Difference in View.INVISIBLE and View.GONE: Invisible objects keep on utilizing the space assigned to it while object set as View.GONE would leave the space of space as if its not on screen.

Use

object.setVisibility(View.GONE);

rather than

object.setVisibility(View.INVISIBLE);
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • 1
    Thank you for your response. I have replaced all instances of "INVISIBLE" with "GONE" in the activity. I have relaunched the app and tested again. The same behaviour exists. No change has occurred. When I click the space where a GONE object is known to exist, it flashes back on the screen momentarily. – Bisclavret Aug 21 '15 at 12:56
  • 1
    Then problem is with something else. Please post your code. – Rohit5k2 Aug 21 '15 at 12:58
  • The code I am using is exactly as discussed above. commanderFrame.setVisibility(View.GONE); to make it disappear, and commanderFrame.setVisibility(View.VISIBLE); to make it come back. This activity is 467 word document pages of code. This code is unrelated to these two lines. Clearly INVISBLE and GONE are not working correctly or there is a bug in Android when dealing with it. It seems I require an alternative to both of these methods. – Bisclavret Aug 21 '15 at 13:04
  • Are you using any animation while hiding the view? – Rohit5k2 Aug 21 '15 at 13:06
  • No animations are used anywhere in the app. – Bisclavret Aug 21 '15 at 13:51
  • i am using and animation to hide my panel and none of the solutions works (GONE or serClickable(false) ). Any ideia? – rmpt Sep 21 '18 at 15:26
9

Use object.setVisibility(View.GONE); instead object.setVisibility(View.INVISIBLE);

View.GONE Means This view is invisible, and it doesn't take any space for layout purposes.

View.GONE This view is invisible, and it doesn't take any space for layout purposes.

View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.

I hope it will helps you .

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 2
    Thank you for your response. I have replaced all instances of "INVISIBLE" with "GONE" in the activity. I have relaunched the app and tested again. The same behaviour exists. No change has occurred. When I click the space where a GONE object is known to exist, it flashes back on the screen momentarily. – Bisclavret Aug 21 '15 at 12:56
  • @Bisclavret Please create different layout for this and apply this rule – IntelliJ Amiya Aug 21 '15 at 12:57
  • 1
    You mean use a different layout type? Which layout type do you suggest will make a GONE object actually GONE? Surely this is unrelated. – Bisclavret Aug 21 '15 at 13:02
  • @Bisclavret Then set setEnabled(false) – IntelliJ Amiya Aug 21 '15 at 13:03
  • setFocusableInTouchMode(false); – IntelliJ Amiya Aug 21 '15 at 13:04
  • 1
    I will try the setFocusableInTouchMode method shortly and let you know, thank you. – Bisclavret Aug 21 '15 at 13:05
  • 1
    It still makes no difference at all. Code pasted above in original question, please note that it is the exact code you have given me. When I click the empty space where these items were, they all reappear for half a second and then are gone again. This seems more and more like a bug with the android OS. – Bisclavret Aug 21 '15 at 13:52
5

Try: object.setClickable(false); As an alternative, you can: object.setEnabled(false);

Fustigador
  • 6,339
  • 12
  • 59
  • 115
  • Have tried both of these methods. Neither of them solves the issue. Thanks for trying though, this feels like an OS bug. – Bisclavret Aug 21 '15 at 13:56
2

The only way I managed to solve this ridiculous issue was to create a sort of view panel that sits between the objects in my activity. This view panel is the size of the screen, uses the same colour background, and starts invisible.

Normally, I make object A disappear, and make object B appear. When I click the space previously occupied by object A, it momentarily flashes back onto the screen and then disappears again. It looks terrible. Subsequent clicks do not reproduce this bug until the next time I make object A disappear.

The fix is to make object A disappear, make the new view panel visible on top of it, and then make object B appear on top of the view panel. So the panel acts as a sort of barrier between the hidden object, and user interactions. The user is not aware that this view panel even exists, as it blends in to the standard background, but when I now click the space where object A would be, it now no longer flashes back onto the screen momentarily. While this is a poor solution to have to use, this OS is bugged and I am left with no choice.

My activity now looks as though it is perfectly stable and working perfectly. I don't like it, but it works.

Thanks a lot, google.

Bisclavret
  • 1,327
  • 9
  • 37
  • 65
1

Try object.setVisibility(View.GONE)

View.GONE prevents view to draw its layout bounds (width and height) whereas View.INVISIBLE draws it.

Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
  • 1
    Thank you for your response. I have replaced all instances of "INVISIBLE" with "GONE" in the activity. I have relaunched the app and tested again. The same behaviour exists. No change has occurred. When I click the space where a GONE object is known to exist, it flashes back on the screen momentarily. – Bisclavret Aug 21 '15 at 12:56
0

You should use:

object.setVisibility(View.GONE);

View.GONE removes your view completely from the layout, but View.INVISIBLE only makes your view invisible but still found in your layout, thus clickable.

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
0

In my case, I was animating a layout and then set it hidden. However, I was using View.GONE but still, the layout views were taking clicks. Then I found that in the animation I was using anim.setFillAfter = true

changing it to "False" fixed the clicks issue for me.

Hope it helps someone banging their head.

SaadurRehman
  • 622
  • 8
  • 20