OverlayViewManager is a lightweight Android library that allows you to overlay views on top of other apps, providing a simple interface to manage and customize these overlays. This library is particularly useful for creating floating widgets or overlays that need to be displayed across the entire system, regardless of the activity or app in the foreground.
Try out the sample application on Google Play.
- Easy to Use: Simple API to create and manage overlay views.
- Customizable: Supports various customization options like touchable, draggable, and view positioning.
- Lifecycle Management: Manages the overlay view lifecycle based on activity or application context.
- Permission Handling: Provides methods to check and request overlay permissions.
Step 1. Add the JitPack repository to your build file
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.75py.Android-OverlayViewManager:overlayviewmanager:{version}'
}
Initialize the OverlayViewManager
in your Application
class:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
OverlayViewManager.init(this);
}
}
To create an overlay view, use the OverlayViewManager
to get an instance of OverlayView
:
OverlayViewManager overlayViewManager = OverlayViewManager.getInstance();
TextView overlayTextView = new TextView(this);
overlayTextView.setText("Hello, World!");
OverlayView<TextView> overlayView = overlayViewManager.newOverlayView(overlayTextView);
To display the overlay view, call the show
method:
overlayView.show();
To hide the overlay view, call the hide
method:
overlayView.hide();
You can customize various properties of the overlay view, such as its position, size, and touch behavior:
overlayView
.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT)
.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
.setTouchable(true)
.setDraggable(true)
.setGravity(Gravity.TOP | Gravity.START)
.setX(100)
.setY(100)
.setAlpha(0.8f);
To check if the app has overlay permissions, use the canDrawOverlays
method:
if (!overlayViewManager.canDrawOverlays()) {
// Request permission
overlayViewManager.requestOverlayPermission();
}
To show a permission request dialog:
overlayViewManager.showPermissionRequestDialog(getSupportFragmentManager(), R.string.app_name);
Here is a complete example of how to use the OverlayViewManager
:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize OverlayViewManager
OverlayViewManager.init(getApplication());
// Create a new overlay view
TextView overlayTextView = new TextView(this);
overlayTextView.setText("Hello, Overlay!");
OverlayView<TextView> overlayView = OverlayViewManager.getInstance().newOverlayView(overlayTextView);
// Set properties
overlayView
.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT)
.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
.setTouchable(true)
.setDraggable(true)
.setGravity(Gravity.TOP | Gravity.START)
.setX(100)
.setY(100)
.setAlpha(0.8f);
// Show the overlay view
overlayView.show();
}
}
Copyright 2017 75py
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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.