Skip to content

Instantly share code, notes, and snippets.

@Jaosrikate
Last active May 17, 2022 13:52
Show Gist options
  • Save Jaosrikate/62860ed621fecf3bff2f9c8269a31a7f to your computer and use it in GitHub Desktop.
Save Jaosrikate/62860ed621fecf3bff2f9c8269a31a7f to your computer and use it in GitHub Desktop.
Android create gradient drawable programmatically (allow to use more than 3 colors) example
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<View
android:id="@+id/view1"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="platinum_gradient1">#DBDBDB</color>
<color name="platinum_gradient2">#FFFFFF</color>
<color name="platinum_gradient3">#BFBEBE</color>
<color name="platinum_gradient4">#F1F1F1</color>
<color name="platinum_gradient5">#FAFAFA</color>
<color name="gold_card_gradient_1">#A0884D</color>
<color name="gold_card_gradient_2">#CCB06C</color>
<color name="gold_card_gradient_3">#A0884D</color>
<color name="gold_card_gradient_4">#705820</color>
<color name="gold_card_gradient_5">#A0884D</color>
</resources>
import android.content.Context
import android.graphics.drawable.GradientDrawable
import androidx.core.content.ContextCompat
class GradientConstant {
companion object {
fun goldGradient(activity: Context): GradientDrawable {
val colors: IntArray = intArrayOf(
ContextCompat.getColor(activity, R.color.gold_card_gradient_1),
ContextCompat.getColor(activity, R.color.gold_card_gradient_2),
ContextCompat.getColor(activity, R.color.gold_card_gradient_3),
ContextCompat.getColor(activity, R.color.gold_card_gradient_4),
ContextCompat.getColor(activity, R.color.gold_card_gradient_5)
)
val gradientDrawable = GradientDrawable(GradientDrawable.Orientation.TL_BR, colors)
val arr2 = FloatArray(8)
arr2[0] = 0f
arr2[1] = 30f
arr2[2] = 0f
arr2[3] = 30f
arr2[4] = 30f
arr2[5] = 30f
arr2[6] = 30f
arr2[7] = 30f
//{mTopLeftRadius, mTopLeftRadius, mTopRightRadius, mTopRightRadius, mBottomRightRadius, mBottomRightRadius, mBottomLeftRadius, mBottomLeftRadius}
gradientDrawable.cornerRadii = arr2
gradientDrawable.setStroke(
3,
ContextCompat.getColor(activity, R.color.gold_card_gradient_5)
)
return gradientDrawable
}
fun platinumGradient(activity: Context): GradientDrawable {
val colors: IntArray = intArrayOf(
ContextCompat.getColor(activity, R.color.platinum_gradient1),
ContextCompat.getColor(activity, R.color.platinum_gradient2),
ContextCompat.getColor(activity, R.color.platinum_gradient3),
ContextCompat.getColor(activity, R.color.platinum_gradient4),
ContextCompat.getColor(activity, R.color.platinum_gradient5)
)
val gradientDrawable = GradientDrawable(GradientDrawable.Orientation.BR_TL, colors)
val arr2 = FloatArray(8)
arr2[0] = 30f
arr2[1] = 30f
arr2[2] = 30f
arr2[3] = 30f
arr2[4] = 30f
arr2[5] = 30f
arr2[6] = 30f
arr2[7] = 30f
gradientDrawable.cornerRadii = arr2
return gradientDrawable
}
}
}
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val goldBackground = GradientConstant.goldGradient(this)
val view1 = findViewById<View>(R.id.view1)
view1.background = goldBackground
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment