How to get window width in multiple windows mode of Android 7?

With Android 7.0, we have several window modes. An example in this figure:

enter image description here

The user can change the width of the window.

How can I get the actual width of the window?

+4
source share
1 answer

You can register when changing the appearance by adding a layout observer.

public class MainActivity extends AppCompatActivity {

private ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Log.d("Main Activity", "" + getWindow().getDecorView().getHeight());
        Log.d("Main Activity", "" + getWindow().getDecorView().getWidth());
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
    }
}

...
0
source

Source: https://habr.com/ru/post/1682713/


All Articles