简体   繁体   中英

How do I detect the user-set screen zoom level in kotlin

I'm trying to find a way in Kotlin to detect the level of display zoom a user has set on their device. I currently have two image buttons with labels off to one side sitting side by side in a LinearLayout. The labels wrap mid-word if the display zoom is set past a certain threshold. What I'd like to do is detect the zoom level (or whatever it's called) and stack them vertically if the text is over a certain size so users with less than ideal eyesight can read them properly. Is this possible in Kotlin?

DisplayMetrics.DENSITY_DEVICE_STABLE exists specifically for this purpose. Compare that to your current densityDpi.

I suggest you use a FlexboxLayout which kind of does what you're welling to achieve here.

However if you insist on using a LinearLayout and invert it's direction depending on weather your text fits there or not, you can use a BoringLayout which helps you to calculate the required width to draw a certain text eg the following code tells how much space SomeText requires to get drawn using a paint with the textSize=20 SP . So depending on this value you can decide how to set the orientation of your LinearLayout

val paint = TextPaint().apply {
            textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20f, context.resources.displayMetrics)
        }
BoringLayout.getDesiredWidth("SomeText", paint)

I was able to find the values I needed in the configuration namespace. There are two user settings that are effecting the layout, Font Scale and Display Zoom, both of which can be set independently from one another.

val config = resources.configuration
val fontScale = config.fontScale
val screenWidth = config.screenWidthDp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM