In my project, I have to lock the orientation of an activity. (I cannot recreate the activity)
I want to display a message when the user change the orientation of th
You can simply use OrientationEventListener to take orientation from sensor (written in Kotlin):
class MainActivity : AppCompatActivity() {
private var orientationEventListener: OrientationEventListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
orientationEventListener = object : OrientationEventListener(context) {
override fun onOrientationChanged(orientation: Int) {
// orientation is in degrees
// Show "The View is locked in this position ..." message if orientation is between 45° and 315°
viewModel.showViewIsLocked(orientation in 45..315)
}
}
}
override fun onResume() {
super.onResume()
orientationEventListener?.enable()
}
override fun onPause() {
orientationEventListener?.disable()
super.onPause()
}
}