In 2013, Facebook released the Chat Heads feature for its messenger, which allows you to correspond without opening the messenger itself by clicking on the small round window that always hangs on the display even on top of other applications:
Facebook was one of the first to demonstrate the “Drawing over other apps” implementation. Developers are currently using this feature in various types of applications, from side smart menus to screen recording. In this article, I want to demonstrate the process of writing an application-on-top-other-applications on the example of the Khameleon anti-spyware program.
To begin with, I will outline the purpose of the application, which will be implemented as an example of the application — on top of other applications. Suppose it is necessary that the people standing nearby could not easily see the content of my smartphone while I use it. That is, the opportunity is necessary:
')
- Hide part of the display to see only the required area
- Instead of a hidden area, show any desired content (for example, a given web page)
This might look something like this:

With the functionality of the application decided - now proceed to the tutorial itself. For writing application-on-other-applications, there are two main components:
- Service, through which the main control and application logic is conducted
- Layout, which is actually a GUI
Before you implement these two components, you must obtain permission for application-on-other-applications. To do this in AndroidManifest.xml you need to add:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
And for API> 23, i.e. Android M, you need to request permission in the main Activity:
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, __INT);
After the permission is received, we indicate the Layout that needs to be displayed on the screen (many elements have been removed for demonstration):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black"> <View android:id="@+id/grab" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp"/> </RelativeLayout>
You can put any View's in the root RelativeLayout (at your discretion) as usual. View with id = "@ + id / grab" we need to show how to dynamically resize the Layout. That is, through this View, you can expand and compress the root RelativeLayout.
As soon as we have a minimum Layout, it is necessary to implement the most common Service. The only service feature in adding a previously created Layout:
@Override public void onCreate(){ super.onCreate(); manager = (WindowManager) getSystemService(WINDOW_SERVICE); params = new WindowManager.LayoutParams( screenWidth,
At this stage, our Layout is successfully displayed on top of other applications. Now we will look at how we can dynamically resize our Layout:
rootView.findViewById(R.id.grab).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
If you pull the edge of the Layout up or down, its height is compressed and expanded accordingly.
We covered the minimum steps required to write an application-on-top-other-applications. The full code for the Khameleon application shown above can be found
here .