Home » Android » Getting list of all Windows in Android

Getting list of all Windows in Android

Posted by: admin May 14, 2020 Leave a comment

Questions:

Is it possible to get a list of all Windows in my Android app?

If not, is it possible to get notifications on creation of a new View or a Window?

Cheers 🙂

For example: I would like to know if there’s a visible keyboard view on the screen, or if there’s an alert dialog on screen. Is that possible? Can I get the View or Window instance holding it?

How to&Answers:

Yes this is possible in a number of different ways. All views being displayed on the screen are added to a ViewGroup, which are usually layouts such as R.layout.main, LinearLayout, RelativeLayout, etc.

You can access the views at runtime, after the layouts have been built, using a handler such as onWindowFocusChanged:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    int count = myLayout.getChildCount();
    for(int i = 0; i < count; i++)
    {
    View v = myLayout.getChildAt(i);
    ...
    }
}

You can simply set up a thread inside onWindowFocusChanged that would notify you if a keyboard is created by constantly checking the number of children views of the current layout.

Answer:

For the keyboard issue, you can use your own keyboard view instance with KeyboardView in your layout: http://developer.android.com/reference/android/inputmethodservice/KeyboardView.html

Use the same principle for the other views you want to handle: manage them yourself in your layout. I don’t know if you can in the software you plan to do but this is a way which can work.

Answer:

You can only get views which are managed by your application.

This includes all views except the status and navigation bars(for higher than HoneyComb). If you choose to have your own InputMethod, that view can be yours as well but you’ll need to register the proper keyboard views. See this question for more on that.

Otherwise, if you want to get all the views in your window:

ViewGroup decor = (ViewGroup)activity.getWindow().getDecorView();
int count = decor.getChildCount();
for(int i = 0; i < count; i++) {
    View view = decor.getChildAt(i); //voila
}

Answer:

hey use this code this will help you to find if any dialog is created in your activity

class MyActivity extends Activity {

@Override
    public void onAttachedToWindow() {

        super.onAttachedToWindow();

        Log.d("TAG", "New Window ATTACHED");
    }

}

onAttachedToWindow will be called every time user creates new dialog or something