Reputation: 159
My app has rotation enabled for mobiles but I don't want landscape mode in tablet.
I have tried
<activity
android:screenOrientation="portrait" />
but this does't specify portrait mode for tablets only.
Upvotes: 1
Views: 155
Reputation: 75635
You can set it up from code but you can also have it in XML file only, but the trick is to use resource qualifiers and either have separate XML for what you call tablet or have separate style for tablets and apply that style to the activity.
Upvotes: 1
Reputation: 2322
I think you will need to do it programmatically, I don't think is any property for the manifest to set it up.
First step detect app installed in a tablet, something like this:
Java:
public boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
Kotlin
fun isTablet(context: Context): Boolean {
val xlarge = context.resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK === Configuration.SCREENLAYOUT_SIZE_XLARGE
val large =
context.resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK === Configuration.SCREENLAYOUT_SIZE_LARGE
return xlarge || large
}
Kotlin extension:
fun Context.isTablet(): Boolean {
val xlarge = resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK === Configuration.SCREENLAYOUT_SIZE_XLARGE
val large =
resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK === Configuration.SCREENLAYOUT_SIZE_LARGE
return xlarge || large
}
Second step, change it in onCreate:
Java
if(isTablet(this)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Kotlin extension
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(isTablet()){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
Upvotes: 3