Introduction
Good layout skills help you arrange app parts neatly. This makes your app easy to use and nice to look at.
Good layout skills help you arrange app parts neatly. This makes your app easy to use and nice to look at.
val layout = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
}orientation to control direction: vertical or horizontal.val layout = LinearLayout(this).apply {
orientation = LinearLayout.HORIZONTAL
}val layout = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
}val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
marginStart = 16
topMargin = 8
}This app shows two text lines stacked vertically with padding around them. It looks clean and easy to read.
import android.os.Bundle import android.widget.LinearLayout import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val layout = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL layoutParams = LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT ) setPadding(32, 32, 32, 32) } val title = TextView(this).apply { text = "Welcome to My App" textSize = 24f } val subtitle = TextView(this).apply { text = "Enjoy a clean and simple layout" textSize = 16f } layout.addView(title) layout.addView(subtitle) setContentView(layout) } }
Always test your layout on different screen sizes to keep it looking good.
Use padding and margins to avoid crowded content.
Mastering layout helps users find what they need quickly and enjoy your app.
Good layouts make apps look neat and professional.
Use layout containers like LinearLayout to arrange views simply.
Spacing and alignment improve user experience and app appeal.
val layout = LinearLayout(this)
layout.orientation = LinearLayout.HORIZONTAL
val button1 = Button(this).apply { text = "One" }
val button2 = Button(this).apply { text = "Two" }
layout.addView(button1)
layout.addView(button2)
setContentView(layout)
What will the user see on the screen?val layout = LinearLayout(this)
layout.orientation = LinearLayout.VERTICAL
val button1 = Button(this).apply { text = "A" }
val button2 = Button(this).apply { text = "B" }
layout.addView(button1)
layout.addView(button2)
setContentView(layout)Pass the test and this pattern moves unknown