Library for creating global floating windows based on jetpack compose on Android.
Global Floating Window Framework based on Jetpack Compose
build.gradle
of your app.repositories {
maven { url 'https://jitpack.io' }
}
dependencyResolutionManagement {
repositories {
maven { url 'https://jitpack.io' }
}
}
compose-floating-window
Dependencydependencies {
implementation "com.github.only52607:compose-floating-window:1.0"
}
Add to AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
val floatingWindow = ComposeFloatingWindow(applicationContext)
floatingWindow.setContent {
FloatingActionButton(
modifier = Modifier.dragFloatingWindow(),
onClick = {
Log.i("")
}) {
Icon(Icons.Filled.Call, "Call")
}
}
floatingWindow.show()
See Sample App.
Use the Modifier.dragFloatingWindow()
modifier on the component you want to make draggable. Example:
FloatingActionButton(
modifier = Modifier.dragFloatingWindow()
) {
Icon(Icons.Filled.Call, "Call")
}
ComposeFloatingWindow
Using LocalComposeFloatingWindow to retrieve, here's an example:
val floatingWindow = LocalComposeFloatingWindow.current
When the Context of the floating window is set to Application, using AlertDialog and Dialog in the Compose interface of the floating window may result in a 'token is null' exception. In such cases, you can use the SystemAlertDialog or SystemDialog components, which can be used in the same way as the built-in AlertDialog and Dialog components.
Example:
SystemAlertDialog(
onDismissRequest = { showDialog = false },
confirmButton = {
TextButton(onClick = { showDialog = false }) {
Text(text = "OK")
}
},
text = {
Text(text = "This is a system dialog")
}
)
You can access the ViewModel from any Composable by calling the viewModel() function.
class MyViewModel : ViewModel() { /*...*/ }
@Composable
fun MyScreen(
viewModel: MyViewModel = viewModel()
) {
// use viewModel here
}
See https://developer.android.com/jetpack/compose/libraries#viewmodel
Apache 2.0 License