How to disable all Textview of an Activity?

Asked

Viewed 229 times

1

There is some method that disables all controls of a certain activity on android?

  • 1

    Herik, there’s no method to do this automatically. You can do it manually, or if you want something generic, I suggest you go through the elements of your View and disable the elements.

1 answer

3


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

I made 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 his Activity or any "branch" of the tree that you wanted to start disabling. For example:

// Usando a raiz da Activity
View raizActivity = findViewById(android.R.id.content);
setEnabledForAll(raizActivity, false);
  • Your algorithm perfectly met my need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.