Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seperate Jetpack composable functions based on screen density

I'm trying out jetpack compose library how to create separate compose functions based on screen density, screen orientation, locale in an organized way

class Success(private val body: String) : MyViewState() {
    @Composable
    override fun buildUI() {
        Padding(padding = 16.dp) {
            Text(text = body, style = +themeTextStyle { body1 })
        }
    }
like image 371
Arunachalam k Avatar asked Feb 27 '26 19:02

Arunachalam k


1 Answers

I think you can just use something like this:

val resources = LocalContext.current.resources
val density = resources.displayMetrics.density
val densityDpi = resources.displayMetrics.densityDpi
Log.d(TAG, "density: $density - $densityDpi")

And do your logic... You can also compare densityDpi with DisplayMetrics.DENSITY_*. Check this answer here.

like image 186
nglauber Avatar answered Mar 02 '26 13:03

nglauber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!