Android Code Snippet Reversing Screen Orientation Using JavaObject

If your activity uses either landscape or portrait orientation, and you want to reverse either orientation, this code snippet will enable you to do so.

B4X:
#If JAVA
 import android.content.pm.ActivityInfo;
 public void changeScreenOrientation(int orientation)
 {
  switch (orientation)
   {
   case 0:
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    break;
   case 1:
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
    break;   
   case 2:
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    break;     
   case 3:
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
    break; 
   default:
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);   
    break;       
   }
 }
#End If

Call this sub to set screen orientation:

B4X:
'#################################################
'0 - SCREEN_ORIENTATION_LANDSCAPE
'1 - SCREEN_ORIENTATION_REVERSE_LANDSCAPE
'2 - SCREEN_ORIENTATION_PORTRAIT)
'3 - SCREEN_ORIENTATION_REVERSE_PORTRAIT
'Any other # - SCREEN_ORIENTATION_UNSPECIFIED 
Note: requires at least API Level 9 
'#################################################

Sub SetScreenOrientation(orientation As Int)
 Dim j As JavaObject
 j.InitializeContext
 j.RunMethod("changeScreenOrientation",Array(orientation))
End Sub
 
Top