A library for creating floating views in Android
dependencies {
implementation 'com.xlythe:floating-view:2.1.5'
}The following permissions are required in your AndroidManfiest.xml
<uses-permissionandroid:name="android.permission.POST_NOTIFICATIONS" />
<uses-permissionandroid:name="android.permission.SYSTEM_ALERT_WINDOW"android:maxSdkVersion="29" />
<uses-permissionandroid:name="android.permission.FOREGROUND_SERVICE"android:maxSdkVersion="29" />
<uses-permissionandroid:name="android.permission.VIBRATE"android:maxSdkVersion="29" />Extend FloatingViewService (<R) and and FloatingViewActivity (R+) to implement the floating window.
publicclassFloatingNotesServiceextendsFloatingViewService {
@NonNull@OverridepublicViewinflateButton(@NonNullViewGroupparent) {
returnLayoutInflater.from(getContext()).inflate(R.layout.floating_icon, parent, false);
}
@NonNull@OverridepublicViewinflateView(@NonNullViewGroupparent) {
returnLayoutInflater.from(getContext()).inflate(R.layout.floating_notes, parent, false);
}
@NonNull@OverrideprotectedNotificationcreateNotification() {
Intentintent = newIntent(this, FloatingNotes.class).setAction(ACTION_OPEN);
returnnewNotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText("Tap to open")
.setContentIntent(PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setPriority(NotificationCompat.PRIORITY_MIN)
.build();
}
}publicclassFloatingNotesActivityextendsFloatingViewActivity {
@OverridepublicvoidonCreate(@NullableBundlesavedInstanceState) {
super.onCreate(savedInstanceState);
}
}Extend OpenShortcutActivity to create the entrypoint for the floating window. This Activity will prompt the user for the necessary permissions, and will subsequently launch the floating window.
publicclassOpenActivityextendsOpenShortcutActivity {
@OverridepublicIntentcreateServiceIntent() {
returnnewIntent(this, FloatingNotesService.class);
}
@OverridepublicIntentcreateActivityIntent() {
returnnewIntent(this, FloatingNotesActivity.class);
}
@RequiresApi(29)
@OverrideprotectedNotificationcreateNotification() {
NotificationChannelnotificationChannel = newNotificationChannel(FloatingNotesService.CHANNEL_ID, getString(R.string.app_name), NotificationManager.IMPORTANCE_MIN);
notificationChannel.setAllowBubbles(true);
NotificationManagernotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
Intentintent = newIntent(ACTION_OPEN).setPackage(getPackageName());
returnnewNotificationCompat.Builder(this, FloatingNotesService.CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.floating_notification_description))
.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setPriority(NotificationCompat.PRIORITY_MIN)
.build();
}
}Your FloatingViewService, FloatingViewActivity, and OpenShortcutActivity components must be declared in AndroidManfiest.xml
<activityandroid:name=".FloatingNotesActivity" />
<serviceandroid:name=".FloatingNotesService" />
<activityandroid:name=".OpenActivity" />Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.