12

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?

gilm
  • 7,690
  • 3
  • 41
  • 41

4 Answers4

1

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.

Wait what
  • 90
  • 1
  • 8
  • 2
    but if I'm not mistaken, all keyboard views and dialog and alertdialogs are Views rendered on a different Window. Can I enum Windows? or Views in other Windows? – gilm Sep 06 '12 at 14:26
  • Also, your code is for an Activity or Window.Callback? Regardless, what's myLayout? How do you assign it, via LayoutInflater? – gilm Sep 06 '12 at 14:35
0

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.

Vince
  • 1,036
  • 1
  • 10
  • 17
0

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
}
Community
  • 1
  • 1
hwrdprkns
  • 7,525
  • 12
  • 48
  • 69
0

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

Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
  • 1
    Can you get the View that was attached? – gilm Sep 20 '12 at 13:05
  • for that you have to design your custom view where you will override this onAttachedToWindow method & if onAttachedToWindow called pass that view id or object throw message to handler & do what you want in that handler – Vishal Pawar Sep 20 '12 at 13:32