Reusable Components in Android
Learn how to create a layout that's going to be reused by other layouts to display data.
Creating layout fragment
Create a new fragment
class from the file menu by clicking the blank fragment
, and the class will look like the one below. After that, change the name of the activity from MainActivity.kt
to NewsActivity.kt.
Create a package called ui
, and inside it, create two packages called activities
and fragments.
Drag the MainActivity
class to the activities
package and the BreakingNews
Fragment
class to the fragment
package as illustrated below.
Reusable components
Reusable components refers to the creation of one component or layout that’s going to be embedded in our layouts. This is useful because we don’t have to recreate it again and again… We use <include/>
or <merge/>
to embed them in the layouts that will use it.This provides a powerful way of creating complex layouts since it’s managed separately and provided to required layouts.
This is ...