user10888288
user10888288

Reputation:

How to programmatically create gradient color background for a View?

strong textThere is an interface component View (simple rectangle) let's call it "my_view". ...

View myView = (View) findViewById(R.id.my_view);

... I want to programmatically create linear gradient color background for a myView. The two colors of the gradient are set by variables that will change frequently. So I need to find an optimized way. I tried to do this:

myView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
    {
        @Override
        public boolean onPreDraw()
        {
            myView.getViewTreeObserver().removeOnPreDrawListener(this);
            int view_height = myView.getHeight();
            ShapeDrawable myDrawable = new ShapeDrawable(new RectShape());
            myDrawable.getPaint().setShader(new LinearGradient(0, 0, 0, view_height, color1, color2, Shader.TileMode.CLAMP));
            myView.setBackgroundDrawable(myDrawable);
            return false;
        }
    });

Everything is working. This code should be executed each time when the progress of the Seekbar changes. After testing, I realized that this is a very bad method.Everything works as it should, but lags are noticeable.


Upd: The problem was solved! The best method is to create a Custom Drawable! Thanks to all forum members who gave me tips, especially pskink, who gave the best advice.

Upvotes: 7

Views: 12745

Answers (4)

Hitesh Sahu
Hitesh Sahu

Reputation: 45042

With Kotlin you can do that in just 2 lines

Change color values in the array

              val gradientDrawable = GradientDrawable(
                    GradientDrawable.Orientation.TOP_BOTTOM,
                    intArrayOf(Color.parseColor("#008000"),
                               Color.parseColor("#ADFF2F"))
                );
                gradientDrawable.cornerRadius = 0f;

               //Set Gradient
               linearLayout.setBackground(gradientDrawable);

Result

enter image description here

Upvotes: 12

sudesh regmi
sudesh regmi

Reputation: 546

use can select color dynamically and use like this.

    int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};

 //create a new gradient color
 GradientDrawable gd = new GradientDrawable(
   GradientDrawable.Orientation.TOP_BOTTOM, colors);

 gd.setCornerRadius(0f);
 //apply the button background to newly created drawable gradient
 view.setBackground(gd);

Upvotes: 16

Farhana Naaz Ansari
Farhana Naaz Ansari

Reputation: 7944

You may do something like this and change Orientation and color accordingly.

public void perform_action(View v)
{
 Button btn = (Button) findViewById(R.id.push_button);

 //Color.parseColor() method allow us to convert
 // a hexadecimal color string to an integer value (int color)
 int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};

 //create a new gradient color
 GradientDrawable gd = new GradientDrawable(
   GradientDrawable.Orientation.TOP_BOTTOM, colors);

 gd.setCornerRadius(0f);
 //apply the button background to newly created drawable gradient
 btn.setBackground(gd);
}

in XML like this

<Button
 android:id ="@+id/push_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Button Gradient Background"
 android:padding="15dp"
 android:onClick="perform_action"
 />

here and here is an example

Upvotes: 1

sudesh regmi
sudesh regmi

Reputation: 546

you can create a shape in the XML file and use this as background drawable.

  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:type="linear" android:gradientRadius="10"
        android:startColor="#E9E9E9" android:endColor="#D4D4D4" />
</shape>

Upvotes: 0

Related Questions