Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Compass in android view system #504

Closed
padmalcom opened this issue Jul 10, 2024 · 2 comments
Closed

Use Compass in android view system #504

padmalcom opened this issue Jul 10, 2024 · 2 comments

Comments

@padmalcom
Copy link

Hi, our app is still partially based on android view system and not yet fully migrated to compose. Is it possible to define the compass composable in an xml?

@rchintapalli
Copy link
Collaborator

Technically, it should be possible using the ComposeView, but haven't tried it myself. Here is how Android recommends using compose in views
https://developer.android.com/develop/ui/compose/migrate/interoperability-apis/compose-in-views

@nishankhadka09
Copy link

nishankhadka09 commented Aug 29, 2024

Declare a composable view in the xml

  <androidx.compose.ui.platform.ComposeView
        android:id="@+id/view_compass"
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
      " />

then you can do something like this:

binding.viewCompass.apply {
            // Dispose of the Composition when the view's LifecycleOwner
            // is destroyed
            setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
            setContent {
                // In Compose world
                val rotation = mapView.mapRotation.collectAsState()
                val scope = rememberCoroutineScope()
                AppTheme {
                    Compass(rotation.value, autoHide = false, onClick = {
                        scope.launch {
                            mapView.setViewpointRotation(0.0)
                        }
                    })
                }
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants