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

WordPress Android App with REST API And Retrofit

Retrofit is the library which converts your JSON data into Java object. In this tutorial, we will learn about how to implement the Retrofit to create a WordPress Mobile App. As we all know, WordPress is the one of the biggest CMS (Content Management System) of the world. Wordpress full fill the requirements for all most every type of the website. So, in this tutorial, I am going to learn how to create an Android App using the WordPress REST API and Retrofit. What is Retrofit? If you want to know about Retrofit, I would like to give you just a simple one-line explanation of the Retrofit. It is a library which converts your REST HTTP request to Java interface. We will learn how we can do all these stuff using some very simple steps. Further, you can check the Retrofit Library here . So, In this tutorial, we will use the Retrofit to retrieve all post and there except ( A short Description of Post) form a WordPress website. Prerequisite for using the Retrofit for WordPress Android App Ther

How to Read and Write JSON data in Kotlin with GSON

Kotlin is now official language for Android development and it is well supported in Android Studio. So here in this tutorial, we are going to learn about how to read and write JSON data in Kotlin using GSON. If you would like to learn the java version of this tutorial check out the last tutorial " How to Read and Write JSON data using GSON ". Introduction In this tutorial, we will write two methods. In the first method, we will create a JSON file and in second method we will read the file and print in a text box.  If you like to know more about the basic entity of JSON you can check out Basic JSON entity here . What is JSON? JSON stands for J ava S cript O bject N otation JSON is a lightweight data-interchange format It is "self-describing" and easy to understand JSON is language independent and can we used in any language JSON Syntax Rules Data is in name/value pairs Data is separated by commas Curly braces hold objects Square brackets hold