Skip to content

Instantly share code, notes, and snippets.

@slaviboy
Created January 19, 2022 07:45
Show Gist options
  • Save slaviboy/8c3e12619862dd689fd0ffa55f1962c7 to your computer and use it in GitHub Desktop.
Save slaviboy/8c3e12619862dd689fd0ffa55f1962c7 to your computer and use it in GitHub Desktop.
Android Jetpack Compose set values as Percentage (dw-display width, dh-display height)
import android.content.res.Resources
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.*
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
hideSystemBars()
setContent {
Column(
modifier = Modifier
.background(Color("#ff00ff"))
.width(0.5.dw)
.height(0.5.dh)
) {
Text("hello", fontSize = 0.08.sw)
Text("world")
}
}
}
private fun hideSystemBars() {
val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView) ?: return
windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}
}
/**
* This extension method converts a double value set as percentage of device width to Dp object
* @example (0.1.dw) 10% of the device width
*/
inline val Double.dw: Dp
get() = Resources.getSystem().displayMetrics.let {
Dp(value = ((this * it.widthPixels) / it.density).toFloat())
}
inline val Int.dw: Dp get() = this.toDouble().dw
inline val Float.dw: Dp get() = this.toDouble().dw
/**
* This extension method converts a double value set as percentage of device height to Dp object
* @example (0.1.dh) 10% of the device height
*/
inline val Double.dh: Dp
get() = Resources.getSystem().displayMetrics.let {
Dp(value = ((this * it.heightPixels) / it.density).toFloat())
}
inline val Int.dh: Dp get() = this.toDouble().dh
inline val Float.dh: Dp get() = this.toDouble().dh
/**
* This extension method converts a double value set as percentage of device width to TextUnit object
* @example (0.1.sw) 10% of the device width
*/
@OptIn(ExperimentalUnitApi::class)
inline val Double.sw: TextUnit
get() = Resources.getSystem().displayMetrics.let {
TextUnit(((this * it.widthPixels) / it.scaledDensity).toFloat(), TextUnitType.Sp)
}
inline val Int.sw: TextUnit get() = this.toDouble().sw
inline val Float.sw: TextUnit get() = this.toDouble().sw
/**
* This extension method converts a double value set as percentage of device width to TextUnit object
* @example (0.1.sh) 10% of the device height
*/
@OptIn(ExperimentalUnitApi::class)
inline val Double.sh: TextUnit
get() = Resources.getSystem().displayMetrics.let {
TextUnit(((this * it.heightPixels) / it.scaledDensity).toFloat(), TextUnitType.Sp)
}
inline val Int.sh: TextUnit get() = this.toDouble().sh
inline val Float.sh: TextUnit get() = this.toDouble().sh
/**
* Method that converts HEX string to Color object
* @param hex string in format "#RRGGBB" or "#AARRGGBB"
*/
fun Color(hex: String = "#000000"): Color {
val colorInt = android.graphics.Color.parseColor(hex)
return Color(colorInt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment