How to Make a Floating Window Application in Android?
Well, on our desktop computers we can easily restore down windows, do something in the background, and maximize the window whenever we want. But we don’t see this feature in android apps that much. Nowadays we can see android is offering Split Screen, but that’s a feature provided by OS, not the app’s individual feature. Let’s make an app that can minimize and maximize itself on just a button click. This feature can help users, in a lot of ways. Suppose you are reading some pdf document with some mathematical calculations and then a minimized calculator over the pdf viewer app will be very helpful. There are a lot of apps that use this feature like Clipboard, MI Calculator, and many more. Here’s a demo of the final app of this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Let’s make the working platform
- Add new Colors for the App: Go to values -> colors.xml. Any custom color can be added here. We have added these two colors.
- Remove the ActionBar: In Android Studio 4.1, go to values -> themes. There are two themes XML file, one for light mode and one for dark mode. In both of the XML, in the style block change the parent attribute to Theme.MaterialComponents.DayNight.NoActionBar.
- Change primary Theme color for the App: In the same file, the first item block must be about the primary color of the app. Here the newly added colors are added. In the item block add @color/gfgTheme or @color/gfgThemeTwo.
Step 3: Let’s make all the Layouts first
3.a: Start working on the activity_main.xml file
This XML file makes the layout of the Main Activity of the app. The layout is not so complicated. There’s only a Button, TextView, EditText, and another Button one after another inside a ConstraintLayout. Here is the XML code.
3.b: Start working on the floating_layout.xml file
Go to res -> layout. Right-click on layout -> New -> Layout Resource File. Add the name of the layout (floating_layout for here). This XML file makes the layout of the floating window. It has the same components just as the Main Layout but with slightly different size constraints. Here is the XML code.
Step 4: Start working on the Java programs
We made 3 classes here. Obviously, the first one is the MainActivity. The second one is FloatingWindowGFG for the floating window service and the last one is a Common class for two common variables.
4.a: Let’s make the class for the common variables
- For this first, let’s make a package first-named Common. Right Click on project package path (com.wheic.floatingedittext for here) -> New -> Package.
- A window will pop up. The intended package name is written(Common for me). A new package will be created.
- Right-Click on the newly created package -> New -> Java Class. The intended class name is written(Common for here).
- Two public String variables are created, one is currentDesc and another is savedDesc. Both are initiated with empty Strings.
- Here is the code for the Common.java class.
4.b: Start working on the MainActivity.java file
- First, the reference for the component classes is created. Two Buttons, one AlertDialog, and one EditText reference is created.
- Before going to onCreate(), some other methods are created.
- isMyServiceRunning(): This method helps to find whether is the floating window service of this app is running. This function is needed when the same app is opened when already the floating window is visible cause then the floating window service is needed to stop. Here is the code with line by line explanation :
- requestOverlayDisplayPermission(): The method helps to redirect the application to the Settings to enable ‘Display over other apps‘. Though for that an extra line needs to be added to the AndroidManifest.xml file. For that go to app -> manifests -> AndroidManifest.xml. Add this line before the application block:
- checkOverlayDisplayPermission(): This method actually checks that if the API level is more than 23 then whether the ‘Display over other apps‘ is enabled in settings. Here is the code of this function:
- Now here is the complete code for the MainActivity.java file.
4.c: Start working on the FloatingWindowGFG.java file
- Another class is created named FloatingWindowGFG. Go to project package path (com.wheic.floatingedittext for me) -> New -> Java class and then any intended name is given(FloatingWindowGFG for me).
- This class inherits the Service class.
- Now as this class is inherited from the Service class, then this class can be used as a service in the Manifest file. So in the AndroidManifest.xml add this line after the activity block and before the application block ends.
- Now here is the code for FloatingWindowGFG.java. The comments have been added for a better explanation:
Finally, the project is ready. You can check this project in this GitHub link.