bloqueo de la orientación de la pantalla a su orientación predeterminada (natural)

I wanted to lock orientation of screen to its default orinetation. I have having problem on achieving this. Initially i locked screen to portrait from manifest. It works fine for portrait defaultdevices. But many tablets have landscape as default so in these devices locking to portrait is not suitable, i want to detect this default orientation and lock it. I mean if landscape is default orientation i want to lock the orientation to landscape and if its portrait then lock it to port. How to do this. I am stuck in this part. I don't want to support both orientation(auto). Please help

Gracias.

preguntado el 10 de marzo de 12 a las 04:03

... no tablet has landscape as the default. There's no 'default'. Mine is typically in portrait when I'm doing something productive, and in landscape when I'm being entertained. Why don't you want to respect the user's preference? -

android-developers.blogspot.in/2010/09/… "Don’t assume that portrait is the default mode." Many tablets are landscape natural. -

I've read that. There's still no 'default' (noun). Someone with a transformer with a keyboard attached will certainly default (verb) to landscape. Disconnect the keyboard, and it depends on the user, what the user's doing, the phase of the moon, etc. Why don't you want to respect the user's preference? -

see i already mentioned i have my requirement for app. I don't want to support auto orientation. I don't understand why its hard for you to understand term "default"/"natural". -

Phones have buttons along one side. Tablets don't. What you're looking for just doesn't exist. That's why it's important to know why you want this - so you can be given some other answer, like an answer to "how do I force landscape on only tablets?" Apps on the market that force an orientation aren't discovering what's 'natural', but are half-assing the matter just as you must. -

3 Respuestas

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 orientation of the display you get the following values :

enter image description here

said so, what you have to do in your locking method is the following:

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;
            }

        } 

    }
}

Respondido 18 Oct 12, 22:10

try this one lock screen to default

Lockorientationactivity :

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 you must set android:configChanges="orientation" for activity.

respondido 10 mar '12, 04:03

Ok this is how it just worked for me. I don't think so its a very proper solution. I have just put this on my manifest android:screenOrientation="nosensor" Now landscape device is taking landscape specific layout and portrait device is taking portrait specific layout. Plus orientation is locked since i have given "nosensor". But i read on some article that this is still not proper way as it might not work sometimes like when opening soft keypad. I'l go for it for now until i find some other way around. Thanks all. - padam thapa

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

Here's the code (lock and unlock methods), it has been tested in 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);
}

contestado el 16 de mayo de 13 a las 12:05

It doesn't work for me. Just copied&pasted and called lockScreenOrientation inside onCreate. - ALABabs

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.