最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - How can I lock screen orientation to portrait for tablet? (Android) - Stack Overflow

programmeradmin1浏览0评论

I want to lock screen orientation to portrait for tablet. and I implemented this in onCreate() method. However, the scree orientation doesn't keep portrait and it's causing unnecessary flicker and re-creation of the activity. I don't know why. Please help me. The dialog shows when dialogUiState is not null.

if (DeviceUtils.isTablet(this) && dialogUiState != null) {
    requestedOrientation = (ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT)
   }
object DeviceUtils {
    @JvmStatic
    fun isTablet(context: Context): Boolean {
        val configuration = context.resources.configuration
        val screenLayoutSize = configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK

        return screenLayoutSize > Configuration.SCREENLAYOUT_SIZE_NORMAL
    }
}

I want to know how to lock the screen orientaion to portrait for tablet.

I want to lock screen orientation to portrait for tablet. and I implemented this in onCreate() method. However, the scree orientation doesn't keep portrait and it's causing unnecessary flicker and re-creation of the activity. I don't know why. Please help me. The dialog shows when dialogUiState is not null.

if (DeviceUtils.isTablet(this) && dialogUiState != null) {
    requestedOrientation = (ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT)
   }
object DeviceUtils {
    @JvmStatic
    fun isTablet(context: Context): Boolean {
        val configuration = context.resources.configuration
        val screenLayoutSize = configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK

        return screenLayoutSize > Configuration.SCREENLAYOUT_SIZE_NORMAL
    }
}

I want to know how to lock the screen orientaion to portrait for tablet.

Share Improve this question edited Nov 29, 2024 at 2:04 John asked Nov 28, 2024 at 9:45 JohnJohn 631 silver badge9 bronze badges 2
  • Please elaborate on how it "doesn't work". What is your goal and what's going wrong? – Jan Itor Commented Nov 28, 2024 at 11:37
  • I can actually rotate the screen which I don't want. I want to lock the screen orientation to portrait. What should I set to android:configChanges = and android:screenOrientation=? My goal is that the screen orientation keeps portrait even though I rotate the screen. – John Commented Nov 28, 2024 at 16:18
Add a comment  | 

1 Answer 1

Reset to default 2

You should use SCREEN_ORIENTATION_PORTRAIT to ignore sensor.

According to the docs, SCREEN_ORIENTATION_SENSOR_PORTRAIT can still use the sensor:

Would like to have the screen in portrait orientation, but can use the sensor to change which direction the screen is facing.

发布评论

评论列表(0)

  1. 暂无评论