How to disable all TextView in an Activity?

1

Is there any method that will disable all the controls of a particular activity in android?

    
asked by anonymous 01.09.2014 / 19:43

1 answer

2

There is no method in Activity that does this automatically. The only way, generic (without having to disable element by element manually) would be to iterate over all elements of the layout and disable one by one.

I've done a method that does this:

void setEnabledForAll(View root, boolean enabled) {
    // Desabilito a própria View
    root.setEnabled(enabled);

    // Se ele for um ViewGroup, isso é, comporta outras Views.
    if(root instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) root;

        // Percorro os filhos e desabilito de forma recursiva
        for(int i = 0; i < group.getChildCount(); ++i) {
            setEnabledForAll(group.getChildAt(i), enabled);
        }
    }
}

To use the method just call it by passing the View root of your Activity or any "branch" of the tree you wanted to start disabling. For example:

// Usando a raiz da Activity
View raizActivity = findViewById(android.R.id.content);
setEnabledForAll(raizActivity, false);
    
01.09.2014 / 20:05