One of the big new features in Android Nougat is the Multi Window support. We have seen this feature in Samsung and LG phones before but now its part of Android stock with the new Android Nougat update. This allows our app to run side by side with other supported apps and even share information between them. Let’s see how to configure our app to support this new feature and customize user experience.
Create a new project and choose the minimum SDK to be Android 7.0 as the previous versions do not support this feature. Open Android Manifest file where we set the attributes to use multi screen mode. There are two attributes mainly that go in this file, resizableActivity (under application): To enable resizing of activity feature. supportsPictureInPicture (under activity): To enable videos to be played over another app. This can’t be true unless resizableActivity is set to true.
There is also option to add layout instructions here as well, where we can set minimun height, default height, width, etc.
Now we have successfully enabled multi window mode. But we can also add specific code to MainActivity.java as well. Here we can call the method isInMultiWindowMode() to check if we are in multi window mode by doing a simple if.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(isInMultiWindowMode()) { //We are in multi window mode } if(isInPictureInPictureMode()) { //We are in picture in picture mode } }
We can also use onMultiWindowModeChanged method and this will be called whenever the mode changes. Then we can call isInMultiWindowMode() to see id the user has moved in or out of multi window mode or picture in picture mode.
@Override public void onMultiWindowModeChanged(boolean isInMultiWindowMode) { super.onMultiWindowModeChanged(isInMultiWindowMode); //Multi window mode has changed }
That’s it. To test it out you need a virtual device that’s running Android Nougat. So, install one if you don’t have one already and start it up. The right hand side soft-key which is normally used for multitasking, is used to activate multi window in this case. On tapping on that squared button our app is split into half and hangs on the upper side of the screen. Another app can be chosen from the list of recent apps and can be seen on the second half of the screen.