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.