Saturday, October 9, 2021

Create GradientDrawable Programmatically - Android Kotlin

Create GradientDrawable Programmatically - Android Kotlin

Create MainActivity.kt

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)


        // set image view drawable liner gradient

        imageView.setImageDrawable(linearGradientDrawable())


        // set image view drawable radial gradient

        imageView2.setImageDrawable(radialGradientDrawable())


        // set image view drawable sweep gradient

        imageView3.setImageDrawable(sweepGradientDrawable())

    }

}



// method to generate linear gradient drawable

private fun linearGradientDrawable():GradientDrawable{

    return GradientDrawable().apply {

        colors = intArrayOf(

            Color.parseColor("#008000"),

            Color.parseColor("#8DB600"),

            Color.parseColor("#D0FF14")

        )

        gradientType = GradientDrawable.LINEAR_GRADIENT

        shape = GradientDrawable.RECTANGLE

        orientation = GradientDrawable.Orientation.BOTTOM_TOP


        // border around drawable

        setStroke(5,Color.parseColor("#4B5320"))

    }

}



// method to generate radial gradient drawable

private fun radialGradientDrawable():GradientDrawable{

    return GradientDrawable().apply {

        colors = intArrayOf(

            Color.parseColor("#DA1884"),

            Color.parseColor("#FFF600"),

            Color.parseColor("#800020")

        )

        gradientType = GradientDrawable.RADIAL_GRADIENT

        shape = GradientDrawable.RECTANGLE


        gradientRadius = 350F


        // border around drawable

        setStroke(5,Color.parseColor("#CD5700"))

    }

}



// method to generate sweep gradient drawable

private fun sweepGradientDrawable():GradientDrawable{

    return GradientDrawable().apply {

        colors = intArrayOf(

            Color.parseColor("#00BFFF"),

            Color.parseColor("#00CC99"),

            Color.parseColor("#D70040"),

            Color.parseColor("#F7E7CE"),

            Color.parseColor("#DFFF00")

        )

        gradientType = GradientDrawable.SWEEP_GRADIENT

        shape = GradientDrawable.RECTANGLE


        // border around drawable

        setStroke(5,Color.parseColor("#232B2B"))

    }

}

Create layout activity_main.xml

<?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:id="@+id/constraintLayout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <ImageView

        android:id="@+id/imageView"

        android:layout_width="0dp"

        android:layout_height="150dp"

        android:layout_marginStart="16dp"

        android:layout_marginTop="16dp"

        android:layout_marginEnd="16dp"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        tools:srcCompat="@tools:sample/avatars" />


    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="8dp"

        android:text="Linear Gradient"

        app:layout_constraintEnd_toEndOf="@+id/imageView"

        app:layout_constraintStart_toStartOf="@+id/imageView"

        app:layout_constraintTop_toBottomOf="@+id/imageView" />


    <ImageView

        android:id="@+id/imageView2"

        android:layout_width="0dp"

        android:layout_height="150dp"

        android:layout_marginStart="16dp"

        android:layout_marginTop="24dp"

        android:layout_marginEnd="16dp"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintHorizontal_bias="0.0"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/textView"

        tools:srcCompat="@tools:sample/avatars" />


    <TextView

        android:id="@+id/textView2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="8dp"

        android:text="Radial Gradient"

        app:layout_constraintEnd_toEndOf="@+id/imageView2"

        app:layout_constraintStart_toStartOf="@+id/imageView2"

        app:layout_constraintTop_toBottomOf="@+id/imageView2" />


    <ImageView

        android:id="@+id/imageView3"

        android:layout_width="0dp"

        android:layout_height="150dp"

        android:layout_marginStart="16dp"

        android:layout_marginTop="24dp"

        android:layout_marginEnd="16dp"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintHorizontal_bias="0.0"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/textView2"

        tools:srcCompat="@tools:sample/avatars" />


    <TextView

        android:id="@+id/textView3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="8dp"

        android:text="Sweep Gradient"

        app:layout_constraintEnd_toEndOf="@+id/imageView3"

        app:layout_constraintStart_toStartOf="@+id/imageView3"

        app:layout_constraintTop_toBottomOf="@+id/imageView3" />


</androidx.constraintlayout.widget.ConstraintLayout>

Ouput:

Create GradientDrawable Programmatically

1 comment:


  1. Thanks for this information. it is helpfull and worthy
    Laptop Repair Center offers quality service for your laptop at a reasonable cost. We offer doorstep support, 24*7 onsite support, repair all brand laptops, have an expert team for onsite support, and much more. for more contact us on 7291903784
    laptop repair center in Delhi

    ReplyDelete