Home to make android application to run in portrait mode only in mobile, where as in tablet it allow both portrait and landscape orientation? Thanks in advance
Asked
Active
Viewed 843 times
2
-
check this http://stackoverflow.com/questions/5832368/tablet-or-phone-android and this http://stackoverflow.com/questions/8180764/how-do-i-lock-screen-orientation-for-phone-but-not-for-tablet-android – M S Gadag Jan 10 '15 at 08:24
2 Answers
1
First check in your activity or slpash screen that the application must be run in which device.
Check the below code to your mainActivity or splash screen.
Intent intent;
if (isTablet(DeciderActivity.this))
{
// for Tablet
intent = new Intent(this, TabletSplashActivity.class);
startActivity(intent);
}
else
{
// for Phone
intent = new Intent(this, PhoneSplashActivity.class);
startActivity(intent);
}
Please declare the isTablet method
public static boolean isTablet() {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
float yInches = metrics.heightPixels / metrics.ydpi;
float xInches = metrics.widthPixels / metrics.xdpi;
double diagonalInches = Math.sqrt(xInches * xInches + yInches * yInches);
if (diagonalInches >= 7) {
// 7inch device or bigger
return true;
} else {
// smaller device
return false;
}
}
I hope this will work for you, it was working fine with my code.

Parth Bhayani
- 1,894
- 3
- 17
- 35
0
In the manifest, set this for all your activities:
<activity android:name=".YourActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
Let me explain:
- With
android:configChanges="orientation"
you tell Android that you will be responsible of the changes of orientation. android:screenOrientation="portrait"
you set the default orientation mode.
You can call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ine the onCreate method of your Activity if the device is a phone. To determine if the device is a phone or a tablet, you can have a look at this question.

Community
- 1
- 1

Himanshu Agarwal
- 4,623
- 5
- 35
- 49