Lesson 7 ConstraintLayout and Data Binding
Lesson 7 ConstraintLayout and Data Binding
ConstraintLayout
and Data Binding
Hell
80d
account. p
Layout
Draw
What we see:
View View
Top
Hello!
Baseline
Bottom
Start End
<TextView
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
This work is licensed under the
app:layout_constraintTop_toTopOf="parent"
Android Development with Kotlin /> 2 license.
Apache 17
Layout Editor in Android Studio
You can click and drag to add constraints to a View.
Wrap content
Match constraints
Spread Chain
Weighted Chain
Packed Chain
app:layout_constraintStart_toEndOf="@id/start_guideline" />
</ConstraintLayout>
This work is licensed under the
Android Development with Kotlin Apache 2 license. 30
Creating Guidelines
● layout_constraintGuide_begin
● layout_constraintGuide_end
● layout_constraintGuide_percent
<androidx.constraintlayout.widget.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="locationLabel,locationDetails"
/>
This work is licensed under the
Android Development with Kotlin 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)
}
}
This work is licensed under the
Android Development with Kotlin Apache 2 license. 34
Data binding
android:id="@+id/loc"/>
</ConstraintLayout>
</layout>
android {
...
buildFeatures {
dataBinding true
}
}
<layout>
<androidx.constraintlayout.widget.ConstraintLayout>
<TextView ... android:id="@+id/username" />
<EditText ... android:id="@+id/password" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
with this
val binding: ActivityMainBinding =
DataBindingUtil.setContentView(
this, R.layout.activity_main)
binding.username = "Melissa"
<androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/textView"
android:text="@{name.toUpperCase()}" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>