Android: disable screen rotation when screen is smaller than x

Does anyone know how to turn off screen rotation in an Android app when the screen is smaller (e.g. 480 pixels)? I am creating an application using phonegap that will target tablet devices, but you can also run it on smartphones. Unfortunately, the application only displays correctly when the application is displayed in landscape orientation ...

+4
source share
3 answers

Perhaps controlled by "if"

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

in onCreate function?

+4
source

Did this!:)

Here's how (explanatory):

int width; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.loadUrl("file:///android_asset/www/index.html"); Display display = getWindowManager().getDefaultDisplay(); width = display.getWidth(); if(width <= 480) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if(width <= 480) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } }' 

and a bit of import:

 import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.view.Display;' 
+4
source

combining the @Howard Hodson solution in this thread as well How to determine the screen size of a device (small, regular, large, xlarge) using code?

I am using this code:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); disableLandscapeInSmallDevices(); } private void disableLandscapeInSmallDevices() { if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } 
0
source

All Articles