Farid
Farid

Reputation: 2562

Cannot disable clicks on LinearLayout with child views

So I've this XML structure:

<LinearLayout
            android:id="@+id/action_info"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="0.25"
            android:orientation="vertical"
            android:padding="@dimen/padding_activity_map_bottom_dialog_buttons"
            android:layout_marginBottom="@dimen/margin_bottom_activity_map_bottom_dialog_content"
            android:clickable="true"
            android:background="?attr/selectableItemBackground">

            <ImageView
                android:src="@drawable/ic_action_info"
                android:layout_width="@dimen/dimens_activity_map_bottom_dialog_button_image"
                android:layout_height="@dimen/dimens_activity_map_bottom_dialog_button_image"
                android:layout_gravity="center"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/label_activity_map_info"
                android:textSize="@dimen/size_activity_map_bottom_dialog_marker_actions_button_label"
                android:gravity="center"/>

        </LinearLayout>

And trying to disable the LinearLayout clicks with (by setting enable=false):

private void disableEnableControls(boolean enable, ViewGroup vg){
        for (int i = 0; i < vg.getChildCount(); i++){
            View child = vg.getChildAt(i);
            child.setEnabled(enable);
            child.setClickable(enable);
            if (child instanceof ViewGroup){
                disableEnableControls(enable, (ViewGroup)child);
            }
        }
    }

But problem persists, still all clicks on LinearLayout triggers.

Upvotes: 0

Views: 3962

Answers (2)

Farid
Farid

Reputation: 2562

Solved changing

private void disableEnableControls(boolean enable, ViewGroup vg){
        for (int i = 0; i < vg.getChildCount(); i++){
            View child = vg.getChildAt(i);
            child.setEnabled(enable);
            child.setClickable(enable);
            if (child instanceof ViewGroup){
                disableEnableControls(enable, (ViewGroup)child);
            }
        }
    }

to

private void disableEnableControls(boolean enable, ViewGroup vg){
        vg.setEnabled(enable); // the point that I was missing
        for (int i = 0; i < vg.getChildCount(); i++){
            View child = vg.getChildAt(i);
            child.setEnabled(enable);
            child.setClickable(enable);
            if (child instanceof ViewGroup){
                disableEnableControls(enable, (ViewGroup)child);
            }
        }
    }

Upvotes: 2

My Question
My Question

Reputation: 13

Please remove this line and check your are getting output as expected android:clickable="true"

Upvotes: 0

Related Questions