Samantha Withanage
Samantha Withanage

Reputation: 3851

Disable all elements in a layout - Android

In my Android app there is a requirement that a number of UI elements should be disabled until a button is clicked. Can I disable all the UI elements in a layout by referring the layout without disable them one by one?

Upvotes: 1

Views: 3451

Answers (2)

Dehan Wjiesekara
Dehan Wjiesekara

Reputation: 3182

Use following attribute in your xml layout (as a example textView):

android:visibility="gone"

In button click event:

myText.setVisible(myText.VISIBLE)

You can either use them one by one or you can put all invisible content in a single layout and hide the layout. Then once you want to show them, just VISIBLE the layout, then all will display.

Upvotes: 1

flx
flx

Reputation: 14226

You could disable all views recursively like this. Just pass the layout as view to the method:

private void enableViews(View v, boolean enabled) {
    if (v instanceof ViewGroup) {
        ViewGroup vg = (ViewGroup) v;
        for (int i = 0;i<vg.getChildCount();i++) {
            enableViews(vg.getChildAt(i), enabled);
        }
    } 
    v.setEnabled(enabled);
}

Just run enableViews(view, false) to disable, or enableViews(view, true) to enable again.

Upvotes: 6

Related Questions