English | 中文
"Drawable Maker" is a utility library for Android that simplifies the creation and usage of Drawables. Its primary goal is to streamline the process of building XML layouts, eliminating the need for frequent switches between file editing tags. This tool aims to enhance the coding experience and slightly reduce APK size, as each Drawable file typically consumes around 200 bytes, and having 100 of them would add up to 20KB.
In the root directory build.gradle add:
allprojects {
repositories {
// ...
maven { url 'https://www.jitpack.io' }
}
}
Add dependencies and config:
android {
buildFeatures {
dataBinding = true
viewBinding = true
}
}
dependencies {
implementation("com.github.Remielpub:DrawableMaker:1.0.0-alpha07")
}
The principle is quite simple. It works in conjunction with ViewBinding and BindingAdapter. By using custom attributes, it sets the generated Drawable to the View's background property. Currently, we've only considered scenarios where the background property needs to be set. If anyone has other use cases where Drawables are needed, please feel free to raise an issue.
ColorDrawable & GradientDrawable :Drawable with rounded corners | border (or dashed border) |colored.
StateListDrawable :The Drawable corresponding to the Selector is currently supported for common attributes:
-
android.R.attr.state_selected(selected or not)
-
android.R.attr.state_enabled(enabled or not)
-
android.R.attr.state_pressed(pressed or not)
-
android.R.attr.state_checked(Check whether)
-
android.R.attr.state_focused(Get focus or not)
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
android:layout_marginTop="60dp"
bg_drawable="@{dm.drawable().withColor(@color/purple_200).make()}" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
style="@style/text_style"
android:layout_marginTop="60dp"
bg_drawable='@{dm.drawable().withColor("#FF018786").make()}'/>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="c"
type="com.childdddd.libdrawalemaker.utils.Constants" />
<import type="com.childdddd.libdrawalemaker.factory.PropertyFactory"
alias="pf"/>
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
style="@style/text_style"
android:layout_marginTop="60dp"
bg_withCorners='@{pf.cornerProperty(0, "#FF018786", c.RECTANGLE)}'/>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
android:layout_marginTop="30dp"
bg_drawable="@{dm.drawable().withColor(@color/purple_200, c.OVAL).make()}"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
android:layout_marginTop="30dp"
bg_drawable="@{dm.drawable().withCorner(10, @color/purple_200).make()}"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv4"
style="@style/text_style"
android:layout_margin="30dp"
bg_drawable='@{dm.drawable().withCorner(10, "#FF018786").make()}'/>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="c"
type="com.childdddd.libdrawalemaker.utils.Constants" />
<import type="com.childdddd.libdrawalemaker.factory.PropertyFactory"
alias="pf"/>
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv4"
style="@style/text_style"
android:layout_margin="30dp"
bg_withCorners='@{pf.cornerProperty(10, "#FF018786", c.RECTANGLE)}'/>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<import
alias="d"
type="com.childdddd.libdrawalemaker.utils.DrawableUtil" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv6"
style="@style/text_style"
android:layout_margin="30dp"
bg_drawable="@{dm.drawable().withCorner(d.cornerRadius(0, 10, 10, 0), @color/purple_200).make()}" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<import
alias="d"
type="com.childdddd.libdrawalemaker.utils.DrawableUtil" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
android:layout_margin="30dp"
bg_drawable='@{dm.drawable().withCorner(d.cornerRadius(0, 10, 10, 0), "#FF018786").make()}' />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="c"
type="com.childdddd.libdrawalemaker.utils.Constants" />
<import type="com.childdddd.libdrawalemaker.factory.PropertyFactory"
alias="pf"/>
<import
alias="d"
type="com.childdddd.libdrawalemaker.utils.DrawableUtil" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
android:layout_margin="30dp"
bg_withCorners='@{pf.cornerProperty(d.cornerRadius(0, 10, 10, 0), "#FF018786", c.RECTANGLE)}'/>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
android:layout_margin="30dp"
bg_drawable="@{dm.drawable().withCorner(10).withStroke(1, @color/purple_200).make()}" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="c"
type="com.childdddd.libdrawalemaker.utils.Constants" />
<import type="com.childdddd.libdrawalemaker.factory.PropertyFactory"
alias="pf"/>
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv4"
style="@style/text_style"
android:layout_margin="30dp"
bg_withCorners='@{pf.cornerProperty(10, "#FF018786", c.RECTANGLE)}'
bg_withStroke="@{pf.strokeProperty(1, @color/purple_200)}"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<import
alias="d"
type="com.childdddd.libdrawalemaker.utils.DrawableUtil" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
bg_drawable="@{dm.drawable().withCorner(d.cornerRadius(10, 10)).withStroke(1, @color/purple_200).make()}"
android:layout_marginTop="30dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
bg_drawable="@{dm.drawable().withStroke(1, @color/purple_200, c.OVAL).make()}"
android:layout_marginTop="30dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
bg_drawable="@{dm.drawable().withCorner(10).withDash(2, @color/purple_200, 10, 10).make()}"
android:layout_marginTop="30dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
bg_drawable="@{dm.drawable().withDash(2, @color/purple_200, 10, 10, c.OVAL).make()}"
android:layout_marginTop="30dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="c"
type="com.childdddd.libdrawalemaker.utils.Constants" />
<import type="com.childdddd.libdrawalemaker.factory.PropertyFactory"
alias="pf"/>
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
android:layout_marginTop="30dp"
bg_withCorners='@{pf.cornerProperty(10, "#FF018786", c.OVAL)}'
bg_withStroke="@{pf.dashProperty(3, @color/purple_200, 5, 5)}"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
bg_drawable="@{dm.drawable().withCorner(10, @color/purple_200).withStroke(2, @color/teal_200).make()}"
android:layout_marginTop="30dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<import
alias="d"
type="com.childdddd.libdrawalemaker.utils.DrawableUtil" />
<import
alias="o"
type="android.graphics.drawable.GradientDrawable.Orientation" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
bg_drawable="@{dm.drawable().withCorner(10).withGradient(d.colors(@color/purple_200, @color/teal_200), o.LEFT_RIGHT).make()}"
android:layout_marginTop="30dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="c"
type="com.childdddd.libdrawalemaker.utils.Constants" />
<import
alias="d"
type="com.childdddd.libdrawalemaker.utils.DrawableUtil" />
<import type="com.childdddd.libdrawalemaker.factory.PropertyFactory"
alias="pf"/>
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
android:layout_marginTop="30dp"
bg_withCorners='@{pf.cornerProperty(10, "#FF018786", c.RECTANGLE)}'
bg_withStroke="@{pf.strokeProperty(1, @color/purple_200)}"
bg_withGradient="@{pf.gradientProperty(d.colors(@color/purple_200, @color/teal_200), c.ORIENTATION_LEFT_RIGHT)}"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<import
alias="d"
type="com.childdddd.libdrawalemaker.utils.DrawableUtil" />
<import
alias="o"
type="android.graphics.drawable.GradientDrawable.Orientation" />
<import
alias="gd"
type="android.graphics.drawable.GradientDrawable" />
<import
alias="c"
type="com.childdddd.libdrawalemaker.utils.Constants" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
bg_drawable="@{dm.drawable().withCorner(10).withGradient(d.colors(@color/purple_200, @color/teal_200), o.TOP_BOTTOM, c.OVAL, gd.RADIAL_GRADIENT, 30, d.center(0.5F, 0.5F)).make()}"
android:layout_marginTop="30dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<import
alias="d"
type="com.childdddd.libdrawalemaker.utils.DrawableUtil" />
<import
alias="o"
type="android.graphics.drawable.GradientDrawable.Orientation" />
<import
alias="gd"
type="android.graphics.drawable.GradientDrawable" />
<import
alias="c"
type="com.childdddd.libdrawalemaker.utils.Constants" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
bg_drawable="@{dm.drawable().withCorner(10).withGradient(d.colors(@color/purple_200, @color/teal_200), o.TOP_BOTTOM, c.OVAL, gd.SWEEP_GRADIENT).make()}"
android:layout_marginTop="30dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<import
alias="d"
type="com.childdddd.libdrawalemaker.utils.DrawableUtil" />
<import
alias="o"
type="android.graphics.drawable.GradientDrawable.Orientation" />
<import
alias="gd"
type="android.graphics.drawable.GradientDrawable" />
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/text_style"
bg_drawable="@{dm.drawable().withCorner(10).withGradient(d.colors(@color/purple_200, @color/teal_200), o.LEFT_RIGHT, c.RECTANGLE, gd.LINEAR_GRADIENT, true, 1000).make()}"
android:layout_marginTop="30dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
Result:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<variable
name="vm"
type="com.remiel.drawablemaker.StateListDrawableViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
style="@style/text_style"
android:layout_marginTop="60dp"
bg_drawable="@{dm.stateDrawable().withSelected(@color/teal_200, @color/purple_200).make()}" />
</LinearLayout>
</layout>
Result
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="com.remiel.drawablemaker.R"/>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<variable
name="vm"
type="com.remiel.drawablemaker.StateListDrawableViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv2"
style="@style/text_style"
android:layout_marginTop="60dp"
bindSelected="@{vm.selected}"
bg_drawable="@{dm.stateDrawable().withSelected(R.drawable.shape_normal, R.drawable.shape_selected).make()}" />
</LinearLayout>
</layout>
Result
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<data>
<import type="com.remiel.drawablemaker.R"/>
<variable
name="vm"
type="com.remiel.drawablemaker.StateListDrawableViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv3"
style="@style/text_style"
android:layout_marginTop="30dp"
bindSelected="@{vm.selected}"
bg_drawable="@{dm.stateDrawable().withSelected(R.mipmap.image_normal, R.mipmap.image_selected).make()}" />
</LinearLayout>
</layout>
Result
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<variable
name="vm"
type="com.remiel.drawablemaker.StateListDrawableViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv4"
style="@style/text_style"
android:layout_marginTop="30dp"
text_normalColor="@{@color/teal_200}"
text_selectedColor="@{@color/purple_200}"
bindSelected="@{vm.selected}"
bg_drawable="@{dm.stateDrawable().withSelected(R.mipmap.image_normal, R.mipmap.image_selected).make()}" />
</LinearLayout>
</layout>
Result
1.2.5 Add text [Selected] status & Color background & dynamically generated rounded corner drawable [Enabled] Status selector
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="dm"
type="com.childdddd.libdrawalemaker.DrawableMaker" />
<import type="com.childdddd.libdrawalemaker.utils.ColorStateListUtil"
alias="c"/>
<variable
name="vm"
type="com.remiel.drawablemaker.StateListDrawableViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv5"
style="@style/text_style"
android:layout_marginTop="30dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
bg_drawable="@{dm.stateDrawable().withEnabled(dm.drawable().withColor(@color/teal_200).make(), dm.drawable().withColor(@color/purple_200).make()).make()}"
bindEnabled="@{vm.selected}"
text_state2='@{c.textStateColor2(1, "#FFBB86FC", "#FF03DAC5")}'/>
</LinearLayout>
</layout>
Result
val drawable = GradientDrawableUtil.drawable(10, Color.RED, 5, Color.BLUE)
view.background = drawable
This is done in the same way that you create Drawable objects in xml, so I won't go into details here.