This library makes it simple to track the orientation of the phone. It's based on the Google Training TiltSpot-Solution repository.
The following steps enable you to use this library.
Refer to jitpack's documentation for instructions.
Add the following lines to your app/build.gradle
dependencies:
// https://github.com/johnnylambada/johnnylambada-orientation
implementation "com.github.johnnylambada:johnnylambada-orientation:1.0.0"
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView azimuthView = findViewById(R.id.azimuth);
final TextView pitchView = findViewById(R.id.pitch);
final TextView rollView = findViewById(R.id.roll);
getLifecycle().addObserver(new OrientationReporter(this, (azimuth, pitch, roll) -> {
azimuthView.setText(getString(R.string.float_value,azimuth));
pitchView.setText(getString(R.string.float_value,pitch));
rollView.setText(getString(R.string.float_value,roll));
}));
}
Review the MainActivity,
it shows you how to use the OrientationReporter
from your activity.