Learning Android Development

Building Custom Component with Kotlin

Making Custom Component with XML Layout

Assuming you want to build a page that consists of some repetitive common views. You might not want to duplicate the views and the code that handles it. So a better way is to build a custom component that group views together to be reused.

Example

Here, I will show you how to build the below example, in Kotlin (yeah, the first-class language of Android!!) .. in case you have a problem following the example here, do check out this link.

As you could see, each Section will have a TextView as Title, an EditView, and a Switch. Hence the three views could be grouped into a custom component.

Build Steps

Step 1. Create the common layout

Build the layout, as you would do for a normal layout. But just for the single component.

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"…

--

--