┌─────────────────────────────┐ │ Profile Card │ │ ┌───────────────┐ │ │ │ [Image] │ │ │ └───────────────┘ │ │ Name: John Doe │ │ Title: Android Developer │ │ │ │ [Follow] [Message] │ └─────────────────────────────┘
Why layout mastery creates professional UIs in Android Kotlin - Build It to Prove It
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintSet import android.widget.Button import android.widget.ImageView import android.widget.TextView class ProfileActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val layout = ConstraintLayout(this).apply { id = ConstraintLayout.generateViewId() setPadding(32, 32, 32, 32) } val imageView = ImageView(this).apply { id = ImageView.generateViewId() // TODO: Set a placeholder image resource } val nameText = TextView(this).apply { id = TextView.generateViewId() text = "" // TODO: Set text size and style } val titleText = TextView(this).apply { id = TextView.generateViewId() text = "" // TODO: Set text size and style } val followButton = Button(this).apply { id = Button.generateViewId() text = "" } val messageButton = Button(this).apply { id = Button.generateViewId() text = "" } layout.addView(imageView) layout.addView(nameText) layout.addView(titleText) layout.addView(followButton) layout.addView(messageButton) setContentView(layout) // TODO: Add constraints to arrange views } }
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintSet import android.widget.Button import android.widget.ImageView import android.widget.TextView import android.graphics.Color import android.view.ViewGroup.LayoutParams class ProfileActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val layout = ConstraintLayout(this).apply { id = ConstraintLayout.generateViewId() setPadding(32, 32, 32, 32) layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) } val imageView = ImageView(this).apply { id = ImageView.generateViewId() setImageResource(android.R.drawable.sym_def_app_icon) // placeholder image layoutParams = LayoutParams(200, 200) scaleType = ImageView.ScaleType.CENTER_CROP } val nameText = TextView(this).apply { id = TextView.generateViewId() text = "John Doe" textSize = 20f setTextColor(Color.BLACK) } val titleText = TextView(this).apply { id = TextView.generateViewId() text = "Android Developer" textSize = 16f setTextColor(Color.DKGRAY) } val followButton = Button(this).apply { id = Button.generateViewId() text = "Follow" } val messageButton = Button(this).apply { id = Button.generateViewId() text = "Message" } layout.addView(imageView) layout.addView(nameText) layout.addView(titleText) layout.addView(followButton) layout.addView(messageButton) val set = ConstraintSet() set.clone(layout) // ImageView constraints set.connect(imageView.id, ConstraintSet.TOP, layout.id, ConstraintSet.TOP) set.connect(imageView.id, ConstraintSet.START, layout.id, ConstraintSet.START) // Name TextView constraints set.connect(nameText.id, ConstraintSet.TOP, layout.id, ConstraintSet.TOP) set.connect(nameText.id, ConstraintSet.START, imageView.id, ConstraintSet.END, 16) set.connect(nameText.id, ConstraintSet.END, layout.id, ConstraintSet.END) // Title TextView constraints set.connect(titleText.id, ConstraintSet.TOP, nameText.id, ConstraintSet.BOTTOM, 8) set.connect(titleText.id, ConstraintSet.START, nameText.id, ConstraintSet.START) set.connect(titleText.id, ConstraintSet.END, nameText.id, ConstraintSet.END) // Follow Button constraints set.connect(followButton.id, ConstraintSet.BOTTOM, layout.id, ConstraintSet.BOTTOM) set.connect(followButton.id, ConstraintSet.START, layout.id, ConstraintSet.START) // Message Button constraints set.connect(messageButton.id, ConstraintSet.BOTTOM, layout.id, ConstraintSet.BOTTOM) set.connect(messageButton.id, ConstraintSet.END, layout.id, ConstraintSet.END) // Buttons horizontal alignment set.constrainWidth(followButton.id, ConstraintSet.WRAP_CONTENT) set.constrainWidth(messageButton.id, ConstraintSet.WRAP_CONTENT) set.applyTo(layout) setContentView(layout) } }
This solution uses ConstraintLayout to arrange the profile card elements precisely. The ImageView is placed at the top-left with a fixed size and a placeholder image. The TextView for the name is aligned to the right of the image with larger text size and black color for emphasis. The title text is below the name with smaller, darker gray text for subtlety.
Two buttons, "Follow" and "Message," are aligned side by side at the bottom left and right corners respectively, creating a balanced layout. Padding and margins ensure spacing between elements, making the UI clean and professional.
Mastering layouts like this helps create interfaces that look polished and are easy to use, which is key for professional apps.
┌─────────────────────────────┐ │ Profile Card │ │ ┌───────────────┐ │ │ │ [Image] │ │ │ └───────────────┘ │ │ Name: John Doe │ │ Title: Android Developer │ │ │ │ [Follow] [Message]│ └─────────────────────────────┘
Practice
Solution
Step 1: Understand the role of layouts in UI
Layouts arrange views and control how the app looks on screen.Step 2: Connect layout quality to user experience
Good layouts make the app neat, easy to use, and professional.Final Answer:
Because good layouts make the app look neat and easy to use -> Option DQuick Check:
Good layouts = neat, easy to use app [OK]
- Thinking layouts only change colors
- Confusing layouts with animations
- Believing layouts replace app logic
Solution
Step 1: Identify the attribute for orientation
The attribute to set layout direction is android:orientation.Step 2: Choose the correct value for vertical layout
android:orientation="vertical" sets views stacked top to bottom.Final Answer:
<LinearLayout android:orientation="vertical" ... > -> Option CQuick Check:
Orientation="vertical" means vertical stack [OK]
- Using layout_width or layout_height instead of orientation
- Confusing horizontal and vertical values
- Missing quotes around attribute values
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?Solution
Step 1: Analyze the LinearLayout orientation
Orientation is set to HORIZONTAL, so children are side by side.Step 2: Check added views and their labels
Two buttons labeled "One" and "Two" are added to the layout.Final Answer:
Two buttons side by side horizontally labeled 'One' and 'Two' -> Option AQuick Check:
Horizontal layout + 2 buttons = side by side buttons [OK]
- Assuming default vertical stacking
- Thinking only one button shows
- Expecting a crash without layout params
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)Solution
Step 1: Check orientation and view addition
Orientation is vertical and both buttons are added correctly.Step 2: Identify missing layout parameters
Buttons need layout_width and layout_height to size properly; missing these causes them not to span the full width.Final Answer:
Buttons do not span the full width because layout_width and layout_height are missing for buttons -> Option BQuick Check:
Missing layout params cause narrow buttons [OK]
- Confusing orientation with layout params
- Thinking setContentView called twice causes improper sizing
- Assuming views not added cause improper sizing
Solution
Step 1: Choose layout for vertical stacking
LinearLayout with vertical orientation stacks buttons top to bottom.Step 2: Center buttons horizontally and space evenly
Set gravity to CENTER_HORIZONTAL to center buttons; add margins for spacing.Final Answer:
Use LinearLayout with vertical orientation, set gravity to CENTER_HORIZONTAL, and add equal margins to buttons -> Option AQuick Check:
Vertical LinearLayout + center gravity + margins = professional UI [OK]
- Using horizontal orientation for vertical layout
- Ignoring gravity for alignment
- Not adding margins for spacing
Ready to prove it?
Pass the test and this pattern moves unknown
