Create Floating View like Facebook Message app in Android

Tram Ho

1. Introduction

If you are a Facebook fan, you must be familiar with the Messages FB application, it appears as a floating chat bubble on the phone screen. You can drag and drop chat view onto other applications easily.

In this article, I will guide you to create an application that has the same view as the Message application of FB.

2. Get started

2.1. Create a new Project

You open Android Studio, create a new application with an activity: MainActivity and activity_main.xml layout as follows:

activity_main.xml

In MainActivity, we will have 2 buttons START, STOP to open and close the view like Facebook Message.

2.2 “Draw over other apps” permission

In order for the view to be dragged and dropped on other applications, you must ask your application to use permison ACTION_MANAGE_OVERLAY_PERMISSION. Therefore, handle in MainActivity as follows:

Additionally, you need to add permissions in AndroidManifest.xml as follows:

Now, when you run the application, the open app will switch to the setting screen, so you can allow your application to be displayed over other applications as follows:

Please enable this permission so we can continue with the following sections.

2.3. Service

We will create a Service to control the view we want. Create a new class, name it MyService .

Remember to declare it in AndroidManifest.xml as follows:

In MyService , we will create an ImageView, display the icon of the app, and handle the logic when the user drag and drop as follows:

2.4. Processed in MainActivity

We will handle when clicking START, STOP button in MainActivity as follows:

Run the application, when you click on START, a view will appear as the app’s icon. You can drag and drop it full screen, even when exiting the app as follows:

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo