Skip to content

Instantly share code, notes, and snippets.

@grndvl1
Last active March 26, 2019 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grndvl1/16b8558fd7da19bd741bfa6072f9226b to your computer and use it in GitHub Desktop.
Save grndvl1/16b8558fd7da19bd741bfa6072f9226b to your computer and use it in GitHub Desktop.
Custom LinearLayout
This is how you set up a custom layout in kotlin, with a linearlayout and 3 textviews but these are embedded so a little
trick here to get the properties setters and getters needs to be done. May not be the most efficient way but it works
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DetailSectionHeaderText">
<attr name="title" format="string" />
<attr name="info" format="string" />
<attr name="details" format="string" />
</declare-styleable>
</resources>
row_detail_header_with_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll_activity_detail_section_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@color/colorPrimary"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_activity_detail_section_header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Title"
android:textColor="#ffffff" />
<TextView
android:id="@+id/tv_activity_detail_section_header_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Info: Awesome"
android:textColor="#ffffff"/>
</LinearLayout>
<TextView
android:id="@+id/tv_activity_detail_section_header_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
</LinearLayout>
DetailSectionHeaderText.kt
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import com.matthewscorp.detail.R
import kotlinx.android.synthetic.main.row_detail_header_with_text.view.*
class DetailSectionHeaderText(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) {
var title: String by Delegates.observable("") {
_, _, new ->
titleView.text = new
invalidate()
}
var info: String by Delegates.observable("") {
_, _, new ->
infoView.text = new
if (new == "") {
infoView.visibility = View.GONE
titleView.gravity = Gravity.CENTER_HORIZONTAL
} else {
infoView.visibility = View.VISIBLE
}
invalidate()
}
var detail: String by Delegates.observable("") {
_, _, new ->
detailView.text = new
invalidate()
}
private lateinit var titleView: TextView
private lateinit var infoView: TextView
private lateinit var detailView: TextView
init {
context?.let {
inflate(context, R.layout.row_detail_header_text, this)
val titleView = tv_activity_detail_section_header_title
val infoView = tv_activity_detail_section_header_info
val detailView = tv_activity_detail_section_header_details
val attributes = context.obtainStyledAttributes(attrs, R.styleable.DetailSectionHeaderText)
title = attributes.getString(R.styleable.DetailSectionHeaderText_title)
info = attributes.getString(R.styleable.DetailSectionHeaderText_info)
detail = attributes.getString(R.styleable.DetailSectionHeaderText_details)
attributes.recycle()
}
}
}
Because of the title, info and detail vars you are able to set these properties easily by calling in the activity code
using kotlinx the id name of the layout. Say you insert the layout and use the id detailview_activity_details_section_one
then you would set the properties dynamically in your activity by calling:
detailview_activity_details_section_one.title = "Some Title"
detailview_activity_details_section_one.info = "Some Info"
detailview_activity_details_section_one.detail = "Some Long detail description"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment