Skip to main content

Create Multi Window App in android for Nougat

Android Multi Window is the most awaited functionality of android Nougat. Here we will create a multi-window demo application.

For testing of Multi Window Application, you must have a device or emulator running android nougat (Android API Level 24 or above ).

You can watch the demo of Application here.

We will create three Activity here

  • MainActivity

  • FirstActivity

  • SecondActivity


Main Activity is the Main part of the application, which contains two Button for launching the other two Activity in multi window mode.

Here We Go:- 
Create a Project with the name of MultiWindowExample with default empty Activity, Once project will be get created with default main activity you have to create the two more activity with the name of FirstActivity.java and SecondActivity.java.

The main part of making the application to work in multi window mode is the modify the AndroidMainfest.xml files of your project.

So open the AndoridMainfest.xml file and do the following modification inside Application tag.
 <activity  
android:name=".MainActivity"
android:taskAffinity=""
android:label="Main Activity">
<layout
android:defaultHeight="600dp"
android:defaultWidth="400dp"
android:gravity="bottom|end"
android:minHeight="350dp"
android:minWidth="300dp" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FirstActivity"
android:resizeableActivity="true"
android:taskAffinity=""
android:label="First Activity">
<layout
android:defaultHeight="400dp"
android:defaultWidth="400dp"
android:gravity="bottom|end"
android:minHeight="350dp"
android:minWidth="300dp" />
</activity>
<activity
android:name=".SecondActivity"
android:resizeableActivity="true"
android:taskAffinity=""
android:label="Second Activity">
<layout
android:defaultHeight="400dp"
android:defaultWidth="400dp"
android:gravity="bottom|end"
android:minHeight="350dp"
android:minWidth="300dp" />
</activity>


You can see that we used some tag android:resizeableActivity=true this tag enable the activity to be in multi window mode. This tag can be set on application level as well. After this, we added the Layout tag inside our activity declaration, which contains defaultHight, width gravity of the Activity. These parameter only work on Android API level 24 and above, there is no any impact of these tag on lower version of the android and application work on full screen.

After this, we need to add the two button in MainActitivity.java files as normal. I am not showing steps here to add this, if required then you check it in the source file on github on the link given at the end of this demo.

Second important part of this demo is the launching the First and Second Activity from MainActivity.
So for that we already created the two button in our MainActitivity.java file if not added till now then you can add it first. After that, you have to add following code inside onCreate Method of MainAcitity.java
 Button firstBtn=(Button)findViewById(R.id.button);  
firstBtn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
Button secondBtn= (Button) findViewById(R.id.button2);
secondBtn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}});


You can see that we have added the two flag inside onClick method for starting the first and second activity.
First is FLAG_ACTIVITY_LAUNCH_ADJACENT it enable the activity to get adjacent in split-screen multi-window mode to Activity on which is launching it. The second flag is FLAG_ACTIVITY_NEW_TASK this will enable the activity to launch as the new task. If you want to learn more about these flags you can found the more details here

We have done with the coding and now you test the application on the emulator or device. When you run the application it will looks like as given below in the screen shot.

Multi WindowAndroid Multi Window Screen shots



Comments

  1. […] my previous post we show you how to use RecyclerView with the header. In this post, I will show you how to create a […]

    ReplyDelete

Post a Comment

Popular posts from this blog

Flutter How to Start Android Activity from Flutter View

Flutter and Dart is an excellent combination for creating the UI, but for accessing the platform-specific service we need to open platform-specific activity. So lets in this article we will explore how to start an android activity and access the service from Flutter View. Create a Project for this Android Activity Flutter View Demo Create a Project From File menu select the New Flutter Project Enter the project name Select the AndroidX support and click on next After the above, we step click on Finish We will have the following project structure created. Create the Second Activity in Android Just go to the android folder and open it in separate windows. We will have the following project structure. Create the Activity Just right-click on the Kotlin folder and create a blank activity from the menu. If you create the activity then you may be required to upgrade the Gradle and do some import. So Just click on update and wait for the project s

Kotlin Parcelable Array Objects Send To Activity

We know that IPC (Inter Process Communication) between the activity is an extremely important part of any application development. We often required that we need to send some data to other activity. For example, we may be required to send an array of data, data could be an Integer, String, Long, Double, Float or any other custom data objects. So, In this example, we are going to learn how to implement the Kotlin Parcelable Array object to send the data from one activity to second activity. What is Parcel? The parcel class is designed as a high-performance IPC transport. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC, and references to live IBinde r objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel. Create Kotlin Parcelable Array Objects Parcelable is API for placing the arbitrary objects into the Parcel. In Actual in android app development, Parcelable is an interface

Flutter Theme Creation, Programmatic Way

Flutter and Dart is an excellent combination for creating the mobile app with material design with nice GUI. Its lots of widget which help the developer for creating the app UI very fast with respect to the other language. You may face some problem in starting due to comm, curly brace and semicolon but after some time you will find the way how to work with these all stuff and it becomes very easy for you. But in this article, we are not going to discuss all these. In this article, we will handle a very interesting topic on how to create a custom theme or multiple themes and let's user decide which theme to use. Create a flutter project So let's create a flutter project by selecting the appropriate option from the File menu in android studio or Visual Code. If you are new to Flutter and Dart you can check out our recent post to get started Creating Cross-platform app using flutter . Once your project is created you will get the default counter app and you try running the app in