Android: Invisible objects still clickable

Bisclavret picture Bisclavret · Aug 21, 2015 · Viewed 18.6k times · Source

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.

Answer

Rohit5k2 picture Rohit5k2 · Aug 21, 2015

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);