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
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.
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
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.


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.
Source Code is available at https://github.com/debugandroid/MutiWindowExample
[…] 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