Home > other >  What is the equivalent of CSS % in android?
What is the equivalent of CSS % in android?

Time:10-15

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

CodePudding user response:

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 %).

CodePudding user response:

There are few Libraries that can be used to deal with responsiveness.

  1. implementation 'com.intuit.ssp:ssp-android:1.0.6' (This size unit scales with the screen size based on the sp size unit (for texts))
  2. implementation 'com.intuit.sdp:sdp-android:1.0.6' (An android lib that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size.)
  3. Also, use the constraint layout to give the size in percentage to the view respective to the screen size.

Some useful quick references of constraint layout:

  1. Use Guidelines that accept percent then you can use them as anchor points to another view.
  2. Use constraintWidth_percent and constraintHeight_percent to give value percentage-based.

Good Luck!

  • Related