Reputation: 4827
I have created orientation detection function, but it is not working properly, I need to get notification when orientation changed left or right. I specifically made this with OrientationEventListener because I do not wish to use OnConfigurationChanged because of reasons.
int orentation; // 0 - Portariat; 1 - Right; 2 - Left
int new_orentation;
orentation = 0;
new_orentation = 0;
myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){
@Override
public void onOrientationChanged(int arg0) {
orent.setText("Orientation: " + String.valueOf(arg0));
switch (orentation) {
case 0:
if (arg0 > 60)
new_orentation = 1;
else if (arg0 < 300)
new_orentation = 2;
break;
case 1:
if (arg0 < 30)
new_orentation = 0;
else if (arg0 < 300)
new_orentation = 2;
break;
case 2:
if (arg0 > 330)
new_orentation = 0;
else if (arg0 > 300)
new_orentation = 1;
break;
default:
break;
}
if (orentation!=new_orentation)
{
switch (new_orentation) {
case 0:
Toast.makeText(getApplicationContext(), "Normal", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
orentation = new_orentation;
}
}};
Upvotes: 0
Views: 480
Reputation: 2889
It doesn't seem that the param declares difference but absolute position. It seems it should be something like the switch/case you are using is not appropriate. Maybe something more like (adjust accordingly for how you want to exactly define each case):
if(arg0 <= 90){
new_orentation = 1;
}
else if(arg0 <=180){
//this is reversePortrait
new_orentation = 0;
}
else if(arg0 <= 270){
new_orentation = 2;
}
else{
new_orentation = 0;
}
Upvotes: 1