• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

UI building without XML or Compose

koje1971

Lurker
Hello,

independent of XML and Compose, there are also other ways to build your UI. I have written some helper classes and put together my UI with a kind of custom DSL. It works almost perfectly. 😊


override fun createLayout(target: FrameLayoutBuilder) {
instance = this
with(target) {
addLinearLayout {
setOrientationVertical()
add(ActivityHeader())
createContent(this)
add(ActivityFooter())
}
}
}


private fun createContent(target: LinearLayoutBuilder) {
with(target) {
addFrameLayout {
setLayoutWeight(1f)
addLinearLayout {
setOrientationVertical()
addFrameLayout {
setLayoutWeight(1f)
addSurfaceView {
setSurface(Playground)
}

addRelativeLayout {
setGravityBottomRight()
addTextView {
setVisibleFalse()
formatHint(this)
addReceiver(Playground.hint) {
setTextId(it)
setVisible(it != R.string.Empty)
}
}
}
}
createInfoLine(this)
}
addFrameLayout {
addReceiver(overlay) {
replace(it)
}
}
}
}
}

fun formatHint(target: TextViewBuilder) {
with(target) {
setTextSizeSP(20)
setTextColorID(R.color.hintText)
setMarginsDP(50, 10, 20, 20)
setPaddingsDP(20, 10)
setGravityRight()
setGradientBackground {
cornerRadii = getCornerRadii(getPixelFromDP(15).toFloat())
setStroke(getPixelFromDP(6), getColorFromID(R.color.black))
setColor(getColorFromID(R.color.hint))
}
}
}

fun createInfoLine(target: LinearLayoutBuilder) {
with(target) {
addLinearLayout {
setOrientationHorizontal()
setPaddingsDP(10, 0)
setBackgroundColorId(R.color.black)
addTextView {
setWidthDP(100)
setLayoutWeight(1f)
setTextSizeSP(16)
setTextColorID(R.color.white)
setFontId(R.font.nunito_bold)
addReceiver(Playground.mode) {
setText(
when (it) {
Play -> "Build Mode"
else -> "Play Mode"
}
)
}
}
addTextView {
setWidthDP(100)
setLayoutWeight(1f)
setGravityCenter()
setTextSizeSP(16)
setTextColorID(R.color.white)
setFontId(R.font.nunito_bold)
setText("V${BuildConfig.VERSION_CODE}")
}
addTextView {
setWidthDP(100)
setLayoutWeight(1f)
setGravityRight()
setTextSizeSP(16)
setTextColorID(R.color.white)
setFontId(R.font.nunito_bold)
addReceiver(Playground.currentBoardIndex) {
setText("Board ${it + 1}")
}
}
}
}

}

eine kleine Person
2
 
Back
Top Bottom