0

I need to run different behavior based on screen size. So I got different types of the screens. If screen is small the variable in calculation must be 40. As it grows the variable number need to be decreased.

Example

Very Small screen width - 480 | variable = 40

Small screen width - 720 | variable = 30

Medium screen width - 1600 | variable = 20

Big screen width - 2560+ | variable = 10

Here is some of my code

 val displayMetrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(displayMetrics)

    val width = displayMetrics.widthPixels

    val percentage = width / 100 * 10 //<----this 10 must be calculated dynamically
    val rightBorder = width - percentage

    when {
        rawX.toInt() in 0..percentage -> {
            params.x = 0
        }
        rawX.toInt() in rightBorder..width -> {
            params.x = width
        }
Bob Redity
  • 529
  • 5
  • 14
  • 2
    You can use resources for different screen sizes. Check out this answer https://stackoverflow.com/a/32861248/3569545 – Demigod Feb 07 '22 at 14:32
  • @Demigod no that's not what I need. I need dynamic calculation and programmatically get those results in my code – Bob Redity Feb 07 '22 at 14:51

1 Answers1

0

You need to define multiple xml layouts for each screen minimum width with the same file name but with different IDs for the root layout enter image description here enter image description here

And use the if/else statement in the onCreate method to give different values for your variable as the the android system will choose the appropriate layout automatically based on the screen size

if(findViewById(R.id.android_me_linear_layout) != null) {}
Hamzeh
  • 26
  • 2