[Samsung DeX] Resizable Window
Samsung Developer Program
Samsung DeX is one of the cool feature that enables you to run your android application in desktop like environment. Running your android application in Desktop. Really? Interaction with mouse and keyboard? Multiple application on same screen? Can we resize the screen size of apps? These are the obvious question that comes to your mind when you heard about running you application on DeX (Desktop). The answer is - yes - you can. You can easily interact with your application using mouse and keyboard and even support all keyboard shortcuts and mouse features like selecting text on double click and many more. For more cool features of Samsung DeX you can refer to https://developer.samsung.com/samsung-dex
This blog is about how to make your application running on Samsung DeX which will have screen resizing capabilities. How to do it? Do I have to write lots of code to provide this feature? Does my Application don't run on DeX if I doesn't support resizable feature?
Hey, calm down! Don't panic. It is very simple.
Apps are only designated DeX Enabled Application (DEA) if they comply with a number of Android N's new features. Some code modification may be necessary depending on what SDK version the app targets.
SDK 24 and higher:
Apps targeting SDK 24 and higher automatically support Multi-window as long as they do not explicitly disable it in the Manifest. To enable or disable Multi-Window in Android N, set this attribute in your Manifest android:resizeableActivity= ["true" | "false"]
For example, the following code shows how to set Multi-Window to "true".
<activity
android:resizeableActivity="true">
</activity>
DeX requirement: ensure you do not set android:resizeableActivity="false" in your Manifest.
Yes, you have heard right. No additional coding with apps SDK 24 and higher is required. Isn't it cool? But It is mandatory for you to explicitly declare android:resizeableActivity="true".
Although, if you want your application to be launched in Fixed size window you can explicitly declare android:resizeableActivity="false" in your Manifest.
SDK 23 and lower:
Apps targeting SDK 23 and lower may not automatically support Multi-Window. Typically, if an app does not explicitly set a fixed orientation in the Manifest, it will automatically work in Multi-Window mode.
To check if you have set a fixed orientation, look for this code in your Manifest android:screenOrientation=["portrait" | "landscape"]
In this case your app on DeX doesn't have resizable feature but only have two option to switch i.e. portrait and landscape.
Operation during Resizing
Now I am going to tell you what exactly happens when your application screen size is resized or it is changed from portrait to landscape. Samsung DeX basically destroys your activity and then recreates it every time you resize your activity. So, there might be a chance of data loss during this process.
How to prevent activity recreation during resizing?
If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation.
android:configChanges="orientation|screenSize|smallestScreenSize|keyboardHidden"
If you declare above configChanges then android doesn't kill the activity during resizing.
The following onConfigurationChanged() function will be called as soon as there is change in screen size or screenLayout.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d("ResizableWindow:","Landscape");
screenOrientation.setText("Landscape");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
screenOrientation.setText("Portrait");
Log.d("ResizableWindow:","Portrait");
}
// checks the screenSize
if(newConfig.screenHeightDp > 0 || newConfig.screenHeightDp >0){
screenHeight.setText(Integer.toString(newConfig.screenHeightDp));
screenWidth.setText(Integer.toString(newConfig.screenWidthDp));
// Log.d("ResizableWindow:","Screen Width Dp:"+Integer.toString(newConfig.smallestScreenWidthDp));
}
}
Using the object of Configuration class you can easily set different layout or additional feature on particular Screen Width or Screen Height.
You can also refer to https://developer.android.com/guide/topics/resources/runtime-changes.html for more information about handling runtime changes.