Multi-Window Simple Examples: Part 1 – Screen changes
Introduction
As far as you know, from Android Nougat we will be able to run two different applications simultaneously. This functionality is called Multi-Window (or split-screen) mode. Don’t worry, I won’t rewrite the documentation, but of course I strongly encourage you to read it: Multi-Window Support. All the theory that you need is there.
What I’d like to present today are three very simple examples of what we can do and what we should care about during split-screen mode:
- Screen changes.
- Drag and drop.
- Launching another application.
This article simply explains how we can handle our application’s layouts when an app user changes the screen size.
Oh… I’ve almost forgotten. All examples take place in Pokémon world! And what’s more, in Part 2 of this article you will be able to catch Pokémon with another app!
To get Pokémon images I used Pokéapi.
Choose configuration changes events
First of all in AndroidManifest.xml file we should specify “configuration changes that the activity will handle itself” in android:configChanges attribute.
1 2 3 4 5 6 7 8 9 10 11 | <activity android:name="com.example.makor.multiwindowcatch.ui.main.MainActivity" android:configChanges="screenSize|screenLayout|smallestScreenSize|orientation"> <layout android:minHeight="@dimen/activity_min_size" android:minWidth="@dimen/activity_min_size" /> //... </activity> |
Note that in each of the named events (screenSize, orientation and so on) activity will not restart. In <layout> tag you can specify for example min. and default width and height of this activity.
Now we have to implement Activity.onConfigurationChanged() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class MainActivity extends AppCompatActivity { //... @Override public void onConfigurationChanged(final Configuration newConfig) { super.onConfigurationChanged(newConfig); setupRecyclerView(getRecyclerViewOrientation(newConfig.orientation)); } private int getRecyclerViewOrientation(final int orientation) { return isOrientationPortrait(orientation) ? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL; } private boolean isOrientationPortrait(final int orientation) { return orientation == Configuration.ORIENTATION_PORTRAIT; } private void setupRecyclerView(final int orientation) { mRecyclerView.setLayoutManager(new LinearLayoutManager(this, orientation, false)); mRecyclerView.setAdapter(mAdapter); } } |
All that I’m doing during the screen changes is checking if the orientation of layout is still portrait or was changed to landscape. Android framework does all the calculation, so if you change screen height in a way that width will be larger, you will receive Configuration.ORIENTATION_LANDSCAPE value in the new configuration. I created list item layouts for portrait and landscape orientation and as you can see I’m changing orientation also in RecyclerView.
And if I don’t want my application to run in split-screen mode?
Fortunately, there is a special tag for it – android:resizeableActivity.
1 2 3 | <activity android:name="com.example.makor.multiwindowcatch.ui.main.MainActivity" android:resizeableActivity="false"/> |
For applications with targetSdkVersion version lower than N the default value is false, otherwise, it is true. Remember, it’s very important!
Summary
Screen changes during Multi-Window mode are almost the same that we have before N. There is nothing new about attribute android:configChanges or method Activity.onConfigurationChanged(). It is simple to support screen changes in a basic way and we should consider split-screen mode in our app designs.
My two complete projects can be found in MultiWindowCatch and MultiWindowDropAdjacent repositories.
Ready to take your business to the next level with a digital product?
We'll be with you every step of the way, from idea to launch and beyond!