Well I'm trying to add a lock orientation button, but when I call
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
the screen goes to normal portrait. What am I doing wrong?
public void onSensorChanged(SensorEvent event) { x = event.values[0]; y = event.values[1]; z = event.values[2]; orientation.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if (x > 5){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else if (x < -5){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } else if (y > 5){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else if (y < -5){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); } } }); }
2 Answers
Answers 1
Well I solved it changing
android:targetSdkVersion="15"
to this
android:targetSdkVersion="11"
in the manifest, I don't really think that this was the problem, but now its working. Just wanted to post it in case anyone have the issue.
Answers 2
Due to the bounty requesting an update:
to lock it:
int orientation; public void lockOrientation(){ orientation = getRequestedOrientation();//if it is inside the activity class. If not add a reference to your activity class Button lock = new Button(R.id.bLock); lock.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int rotation = ((WindowManager) getSystemService(//if it is inside the activity class. If not add a reference to your activity class Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; default: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; } setRequestedOrientation(orientation);//if it is inside the activity class. If not add a reference to your activity class } }); }
to unlock it:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);//if it is inside the activity class. If not add a reference to your activity class
0 comments:
Post a Comment