div
div

Reputation: 1533

Disable click listener for invisible viewgroup

I've a viewgroup which is View.INVISIBLE under some circumstances. In this state, i want the viewgroup or it's children views to NOT respond to any click events.

According to the default implementation of View.INVISIBLE in android, the child views inside the viewgroup are still clickable even when the viewgroup's visibility is invisible. I know i can set the visibility to View.GONE but i don't want the entire viewgroup to be removed.

I tried setting setClickable to true on the viewgroup but it didn't work and the child views are still responding to clicks. I don't want to iterate through all the views in the viewgroups and disable them individually as this is something that i've to do frequently.

Is there a better solution to prevent all the child views in the viewgroup from receiving click events?

Upvotes: 1

Views: 922

Answers (2)

igarasi
igarasi

Reputation: 137

Maybe you can try 'setOnClickListener'.

viewgroup.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true; // return true can intercept click event go down
        }
    });

And you want to receive the viewgroup click event.

viewgroup.setOnTouchListener(null)

Upvotes: 2

SomeKoder
SomeKoder

Reputation: 915

Have you tried setEnabled(false)?

If that doesn't work, you can set all view children to disabled like this:

LinearLayout layout = (LinearLayout) findViewById(R.id.my_layout);
for (int i = 0; i < layout.getChildCount(); i++) {
    View child = layout.getChildAt(i);
    child.setEnabled(false);
}

Upvotes: 0

Related Questions