0

I'm using a Fragment on multiple Activities to show the ShowCaseView, everything is fine, but I want to disable the controls that has been outlined by ShowCaseView during the presentation, prevent them from being clicked before the presentation is over.

Like that:

getActivity().getWindow().getDecorView().findViewById(android.R.id.content).findViewById(R.id.btnJournal).setEnabled(false);

I can disable one element that I know about, but I want to get all childs of that View and make them all disabled without explicitly writing their ids, but what I get with getActivity().getWindow().getDecorView().findViewById(android.R.id.content) is a View and not ViewGroup or LinearLayout, if I cast it to ViewGroup - it can't find any child, if I cast it to LinearLayout, then, well:

`Caused by: java.lang.ClassCastException: com.android.internal.policy.impl.PhoneWindow$DecorView cannot be cast to android.widget.LinearLayout`

How can I iterate through it's children?

Movsar Bekaev
  • 888
  • 1
  • 12
  • 30

1 Answers1

0

I found the next solution:

Firstly I used the code snippet from here - thanks to MH. to get the all child Views:

private List<View> getAllChildrenBFS(View v) {
    List<View> visited = new ArrayList<View>();
    List<View> unvisited = new ArrayList<View>();
    unvisited.add(v);

    while (!unvisited.isEmpty()) {
        View child = unvisited.remove(0);
        visited.add(child);
        if (!(child instanceof ViewGroup)) continue;
        ViewGroup group = (ViewGroup) child;
        final int childCount = group.getChildCount();
        for (int i=0; i<childCount; i++) unvisited.add(group.getChildAt(i));
    }

    return visited;
}

So, in my Fragment's code, I wrote to get reference to Content View and to set controls disabled the next code:

ViewGroup mRootView = (ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
    for (View v : getAllChildrenBFS(mRootView)) v.setEnabled(false);

And then a similar code to set them enabled after the job is done:

ViewGroup mRootView = (ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
    for (View v : getAllChildrenBFS(mRootView)) v.setEnabled(true);

So now I'm cool and happy that I didn't spent all day for nothing))

Community
  • 1
  • 1
Movsar Bekaev
  • 888
  • 1
  • 12
  • 30