Monday, December 15, 2014

Android enable and disable orientation (Portrait or Landscape or Full Mode)

In case of if you would like enable or disable the orientation:

The one of decent way is to create a method that enabling and disabling the orientation base on input argument:
See the source code below as reference

//set up a method to enable or disable rotation

protected void controlForOrientation(string rotatedmod)
 {     
 if(rotatedmod.equalsIgnoreCase("portrait"))
 {
         // Set the screen orientation to only allow Portrait modes.
 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
 }
        else if(rotatedmod.equalsIgnoreCase("landscape"))
 {
        //Set the screen orientation to only allow Landscape modes.
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
 }
 else
 {
        // Set the screen orientation to allow both Portrait and Landscape modes.
 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);  
        }
}

//call for the method above

//only allow portrait
 controlForOrientation("portrait");
//only allow landscape
 controlForOrientation("landscape");
//both allow
 controlForOrientation("release");

No comments:

Post a Comment