Search code examples
androidcsswidthpercentage

What is the equivalent of CSS % in android?


I'm new to android development and I am making small, small programs as a practice. I was previously working on web development. In CSS we use % or vw, vh for sizes according to screen width or height. But in android I found only px, dp and sp. I just want to know is there any equivalent of CSS %, vw or vh in android as well. If so how to use them?

Thanks in advance


Solution

  • You can try using android:weight="..." on your views within a LinearLayout. Or the Modifier.weigth(...) in Jetpack Compose in columns and rows.

    Checkout this answer for a more detailed explanation of weights in androids view system: https://stackoverflow.com/a/4517358/16234182

    In Compose the maximum weight of the row/column also will be the sum of the children weights.

    I also encourage you to use ConstraintLayout, where you can position your views relatively (with %).