You can do it using data binding and BindingAdapter. I will show an example using Kotlin.
- To enable data binding add to Your Gradle:
apply plugin: 'kotlin-kapt' // only need when You use Kotlin
...
android {
...
buildFeatures {
dataBinding = true
}
...
}
- Crete BindingAdapter (I am adding it to MainActivity.kt to simplify code):
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import androidx.databinding.DataBindingUtil
import com.myniprojects.teststackjava.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity()
{
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(
this, R.layout.activity_main)
}
}
// Here is binding adapter. It takes 2 colors, background and stroke. If You want more customization add more parameters
@BindingAdapter(value = ["colorStoke", "colorBack"], requireAll = true)
fun setBackground(textView: TextView, @ColorRes colorStoke: Int, @ColorRes colorBack: Int)
{
val stroke = ContextCompat.getColor(textView.context, colorStoke)
val back = ContextCompat.getColor(textView.context, colorBack)
val gd = GradientDrawable()
// setting background
gd.colors = intArrayOf(
back,
back
)
// here change parameters as You want
gd.gradientType = GradientDrawable.LINEAR_GRADIENT
gd.shape = GradientDrawable.RECTANGLE
gd.cornerRadius = 15f;
// setting stroke width and color
gd.setStroke(5, stroke)
textView.background = gd
}
To customize GradeintDrawable check this docs
- In Your MainActivity.xml do this:
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="com.myniprojects.teststackjava.R" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtVTest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:gravity="center"
android:text="TextView Text"
android:textSize="40sp"
app:colorBack="@{R.color.color_back}"
app:colorStoke="@{R.color.color_stroke}"/>
</FrameLayout>
</layout>
I am importing R to be able to pass resources. (Maybe there is another way without import but I couldn't pass color like @{@color/name} so I just import it). In Your TextView You have to call app:colorBack and app:colorStoke with colors that You want to set as stroke and back.
- Of course in res/values/colors:
<color name="color_stroke">#B71C1C</color>
<color name="color_back">#33691E</color>
- And final effect to show that everything works:

To generate ActivityMainBinding You first have to create a data binding layout. If You added Your dependency correctly to convert fast Your layout to data binding layout use: Alt + Enter ➡ Convert to data binding layout*

So Your layout should look like this:
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</FrameLayout>
</layout>
And now after rebuilding Your project You should get ActivityMainBinding class.