Lesson 5 Layouts
Lesson 5 Layouts
Layouts
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 5: Layouts
● Layouts in Android
● ConstraintLayout
● Additional topics for ConstraintLayout
● Data binding
● Displaying lists with RecyclerView
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Layouts in Android
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Android devices
● Android devices come in many
different form factors.
● More and more pixels per inch
are being packed into device
screens.
● Developers need the ability to
specify layout dimensions that
are consistent across devices.
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Density-independent pixels (dp)
Use dp when specifying sizes in your layout, such as the width or height of views.
density-independent pixels.
● dp = (width in pixels * 160) 160dp
screen density
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Screen-density buckets
Density qualifier Description DPI estimate
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Android View rendering cycle
Measure
Layout
Draw
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Drawing region
What we see:
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
View margins and padding
View View
View
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
ConstraintLayout
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Deeply nested layouts are costly
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
What is ConstraintLayout?
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
What is a constraint?
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Relative positioning constraints
Can set up a constraint relative to the parent container
Format: layout_constraint<SourceConstraint>_to<TargetConstraint>Of
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Relative positioning constraints
Top
Hello!
Baseline
Bottom
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Relative positioning constraints
Start End
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Simple ConstraintLayout example
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Layout Editor in Android Studio
You can click and drag to add constraints to a View.
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Constraint Widget in Layout Editor
Fixed
Wrap content
Match constraints
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Wrap content for width and height
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Wrap content for width, fixed height
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Center a view horizontally
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Use match_constraint
Can’t use match_parent on a child view, use match_constraint instead
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Chains
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Create a Chain in Layout Editor
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Chain styles
Adjust space between views with these different chain styles.
Spread Chain
Weighted Chain
Packed Chain
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Additional topics for
ConstraintLayout
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Guidelines
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Guidelines in Android Studio
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Example Guideline
<ConstraintLayout>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/start_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="16dp" />
<TextView ...
app:layout_constraintStart_toEndOf="@id/start_guideline" />
</ConstraintLayout>
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Creating Guidelines
● layout_constraintGuide_begin
● layout_constraintGuide_end
● layout_constraintGuide_percent
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Groups
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Example group
<androidx.constraintlayout.widget.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="locationLabel,locationDetails"/>
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Groups app code
override fun onClick(v: View?) {
if (group.visibility == View.GONE) {
group.visibility = View.VISIBLE
button.setText(R.string.hide_details)
} else {
group.visibility = View.GONE
button.setText(R.string.show_details)
}
}
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Data binding
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Current approach: findViewById()
Traverses the View hierarchy each time
MainActivity.kt activity_main.xml
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Use data binding instead
Bind UI components in your layouts to data sources in your app.
MainActivity.kt activity_main.xml
<layout>
<ConstraintLayout … >
Val binding:ActivityMainBinding initialize binding
<TextView
android:id="@+id/name"/>
binding.name.text = … <TextView
binding.age.text = … android:id="@+id/age"/>
binding.loc.text = … <TextView
android:id="@+id/loc"/>
</ConstraintLayout>
</layout>
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Modify build.gradle file
android {
...
buildFeatures {
dataBinding true
}
}
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Add layout tag
<layout>
<androidx.constraintlayout.widget.ConstraintLayout>
<TextView ... android:id="@+id/username" />
<EditText ... android:id="@+id/password" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Layout inflation with data binding
Replace this
setContentView(R.layout.activity_main)
with this
val binding: ActivityMainBinding = DataBindingUtil.setContentView(
this, R.layout.activity_main)
binding.username = "Melissa"
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Data binding layout variables
<layout>
<data>
<variable name="name" type="String"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/textView"
android:text="@{name}" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
In MainActivity.kt:
binding.name = "John"
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Data binding layout expressions
<layout>
<data>
<variable name="name" type="String"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/textView"
android:text="@{name.toUpperCase()}" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Displaying lists with
RecyclerView
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
RecyclerView
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
RecyclerView.Adapter
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
View recycling in RecyclerView
Boston, Massachusetts If item is scrolled
offscreen, it isn’t
Chicago, Illinois
destroyed. Item is put in
Mountain View, California a pool to be recycled.
Miami, Florida
Seattle, Washington
Reno, Nevada
onBindViewHolder binds
Nashville, Tennessee the view with the new
values, and then the view
Little Rock, Arkansas gets reinserted in the list.
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Add RecyclerView to your layout
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Android Development with Kotlin This work is licensed under the Apache 2 license. 47
Create a list item layout
res/layout/item_view.xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Android Development with Kotlin This work is licensed under the Apache 2 license. 48
Create a list adapter
class MyAdapter(val data: List<Int>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>()
{
class MyViewHolder(val row: View) : RecyclerView.ViewHolder(row) {
val textView = row.findViewById<TextView>(R.id.number)
}
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Set the adapter on the RecyclerView
In MainActivity.kt:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Learn more
Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 54