Skip to content

Instantly share code, notes, and snippets.

@Nilzor
Created April 23, 2020 06:14
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 Nilzor/ef5ba6d747640e4fc0ca8f7a36c8f8bc to your computer and use it in GitHub Desktop.
Save Nilzor/ef5ba6d747640e4fc0ca8f7a36c8f8bc to your computer and use it in GitHub Desktop.
DAtabinding to custom prop
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="@dimen/small_margin"
android:background="@drawable/themed_button_white_elevated"
android:clickable="true"
android:elevation="4dp"
android:focusable="true"
android:gravity="center"
android:orientation="horizontal"
tools:layout_height="200dp">
<ImageView
android:id="@+id/ticket_icon"
android:layout_width="@dimen/ticket_icon_size"
android:layout_height="@dimen/ticket_icon_size"
tools:src="@drawable/ic_print_ticket_medium"
android:scaleType="fitCenter" />
<FrameLayout
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="32dp"
android:background="#777" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/ticket_title"
style="@style/Text.XXLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
tools:text="Billett" />
<TextView
android:id="@+id/ticket_description"
style="@style/Text.Small.Secondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
tools:text="Egenskaper" />
</LinearLayout>
</LinearLayout>
class BuyTicketButton(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : this(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(context, attrs)
init {
val view = LayoutInflater.from(context)!!.inflate(R.layout.button_ticket, this, true)
val attributes = context.obtainStyledAttributes(attrs, R.styleable.BuyTicketButton)
val imageSrc = attributes.getResourceId(R.styleable.BuyTicketButton_imageSrc, 0)
view.findViewById<ImageView>(R.id.ticket_icon).setImageResource(imageSrc)
view.findViewById<TextView>(R.id.ticket_title).text = attributes.getString(R.styleable.BuyTicketButton_title)
view.findViewById<TextView>(R.id.ticket_description).let {
it.text = attributes.getString(R.styleable.BuyTicketButton_description)
it.visibility = if (it.text.isNullOrEmpty()) GONE else View.VISIBLE
}
attributes.recycle()
}
}
<no.ruter.sales.shared.view.BuyTicketButton
android:id="@+id/default_media_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/small_margin"
app:layout_constraintStart_toEndOf="@id/travel_card_button"
app:layout_constraintEnd_toEndOf="parent"
android:onClick="@{fragment::clickDefaultSalesMediaBtn}"
app:imageSrc="5"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment