Snap the screen orientation to its default (natural) orientation

I wanted to lock the default screen orientation. I have a problem achieving this. Initially, I locked the screen before the portrait from the manifest. It works great for defaultdevices portraits. But many tablets have a default landscape, so portrait lock is not suitable in these devices, I want to define this default orientation and lock it. I mean, if the default orientation is the default - I want to lock the orientation to the landscape, and if her portrait then will lock it to the port. How to do it. I am stuck at this part. I don't want to maintain orientation (auto). Please, help

Thank.

+3


source to share


3 answers


There is a default orientation for different devices, for example, the default orientation on a Galaxy 10 tablet is different from a Nexus 7 tablet. When you get the display orientation, you get the following values:

enter image description here



said this what you should do in your blocking method:

public void mLockScreenRotation(FA_Padre actividad){
    int buildVersionSDK = Build.VERSION.SDK_INT;
    Display display = ((WindowManager) actividad.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int orientation=0;
    if(buildVersionSDK >= 8){
        orientation=display.getRotation();
    }
    /****************Phone*************************************/
    if(buildVersionSDK < 8){// older Android versions with only two orientations
        switch (actividad.getResources().getConfiguration().orientation){

        case Configuration.ORIENTATION_PORTRAIT:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        case Configuration.ORIENTATION_LANDSCAPE:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            break;

        }
    }else if((buildVersionSDK > 7 ) && (!GlobalInfo.isTablet())){// Newer Android phones with more than two orientations

        switch(orientation){

        case Surface.ROTATION_0:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        case Surface.ROTATION_90:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            break;
        case Surface.ROTATION_180:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        case Surface.ROTATION_270:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            break;

        }
    /****************TABLET*************************************/
    }else{
        int width = 0;
        int height = 0;
        switch(orientation){
            /*If the default orientation of the device is landscape Rotation_0 and rotation_180 will be the case if the device is being held in landscape. if the default orientation of the device is portrait rotation_0 or 180 will only be the case if the device is in portrait mode*/ 
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:  
                width = display.getWidth();
                height = display.getHeight();
                break;
          /*the opposite in here*/
            case Surface.ROTATION_90: //      
            case Surface.ROTATION_270:
                width = display.getHeight();
                height = display.getWidth();
                break;
            default:
                break;
        }
        if(width > height){//Default ORIENTATION = LANDSCAPE, lock the screen in the current orientation
            switch(orientation){

            case Surface.ROTATION_0:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case Surface.ROTATION_90:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                break;
            case Surface.ROTATION_180:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                break;
            case Surface.ROTATION_270:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
            } 

        } else {//Default ORIENTATION = PORTRAIT, lock the screen in the current orientation
            switch(orientation){

            case Surface.ROTATION_0:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
            case Surface.ROTATION_90:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case Surface.ROTATION_180:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                break;
            case Surface.ROTATION_270:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                break;
            }

        } 

    }
}

      

+1


source


try this default lock screen

Blocking activity :



public class Lockorientationactivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int getConf=this.getResources().getConfiguration().orientation;

       if(getConf==Configuration.ORIENTATION_PORTRAIT)
       {
           this.setRequestedOrientation(
                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
           Toast.makeText(getBaseContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
       }
       else
       {
           Toast.makeText(getBaseContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
           this.setRequestedOrientation(
                    ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
       }

           setContentView(R.layout.main);
    }
}

      

But to be active, you must install android:configChanges="orientation"

.

0


source


To lock the screen by code, you must use the actual rotation of the screen (0, 90, 180, 270), and you must know its natural position, in a smartphone the natural position will be a portrait and in a tablet it will be a landscape.

Here's the code (lock and unlock), it has been tested on some devices (smartphones and tablets) and it works great.

public static void lockScreenOrientation(Activity activity)
{   
    WindowManager windowManager =  (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);   
    Configuration configuration = activity.getResources().getConfiguration();   
    int rotation = windowManager.getDefaultDisplay().getRotation(); 

    // Search for the natural position of the device    
    if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE &&  
       (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) ||  
       configuration.orientation == Configuration.ORIENTATION_PORTRAIT &&   
       (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270))   
    {   
        // Natural position is Landscape    
        switch (rotation)   
        {   
            case Surface.ROTATION_0:    
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    
                break;      
            case Surface.ROTATION_90:   
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
            break;      
            case Surface.ROTATION_180: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
                break;          
            case Surface.ROTATION_270: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
                break;
        }
    }
    else
    {
        // Natural position is Portrait
        switch (rotation) 
        {
            case Surface.ROTATION_0: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
            break;   
            case Surface.ROTATION_90: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
            break;   
            case Surface.ROTATION_180: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
                break;          
            case Surface.ROTATION_270: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
                break;
        }
    }
}

public static void unlockScreenOrientation(Activity activity)
{
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}

      

0


source







All Articles