Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3499126
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:34:25+00:00 2026-05-18T12:34:25+00:00

I’d like to create a shape that’s like the following image: Notice the top

  • 0

I’d like to create a shape that’s like the following image:

alt text

Notice the top half gradients from color 1 to color 2, but theres a bottom half that gradients from color 3 to color 4. I know how to make a shape with a single gradient, but I’m not sure how to split a shape into two halves and make 1 shape with 2 different gradients.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:34:26+00:00Added an answer on May 18, 2026 at 12:34 pm

    I don’t think you can do this in XML (at least not in Android), but I’ve found a good solution posted here that looks like it’d be a great help!

    ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient lg = new LinearGradient(0, 0, width, height,
                new int[]{Color.GREEN, Color.GREEN, Color.WHITE, Color.WHITE},
                new float[]{0,0.5f,.55f,1}, Shader.TileMode.REPEAT);
            return lg;
        }
    };
    
    PaintDrawable p=new PaintDrawable();
    p.setShape(new RectShape());
    p.setShaderFactory(sf);
    

    Basically, the int array allows you to select multiple color stops, and the following float array defines where those stops are positioned (from 0 to 1). You can then, as stated, just use this as a standard Drawable.

    Edit: Here’s how you could use this in your scenario. Let’s say you have a Button defined in XML like so:

    <Button
        android:id="@+id/thebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press Me!"
        />
    

    You’d then put something like this in your onCreate() method:

    Button theButton = (Button)findViewById(R.id.thebutton);
    ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient lg = new LinearGradient(0, 0, 0, theButton.getHeight(),
                new int[] { 
                    Color.LIGHT_GREEN, 
                    Color.WHITE, 
                    Color.MID_GREEN, 
                    Color.DARK_GREEN }, //substitute the correct colors for these
                new float[] {
                    0, 0.45f, 0.55f, 1 },
                Shader.TileMode.REPEAT);
             return lg;
        }
    };
    PaintDrawable p = new PaintDrawable();
    p.setShape(new RectShape());
    p.setShaderFactory(sf);
    theButton.setBackground((Drawable)p);
    

    I cannot test this at the moment, this is code from my head, but basically just replace, or add stops for the colors that you need. Basically, in my example, you would start with a light green, fade to white slightly before the center (to give a fade, rather than a harsh transition), fade from white to mid green between 45% and 55%, then fade from mid green to dark green from 55% to the end. This may not look exactly like your shape (Right now, I have no way of testing these colors), but you can modify this to replicate your example.

    Edit: Also, the 0, 0, 0, theButton.getHeight() refers to the x0, y0, x1, y1 coordinates of the gradient. So basically, it starts at x = 0 (left side), y = 0 (top), and stretches to x = 0 (we’re wanting a vertical gradient, so no left to right angle is necessary), y = the height of the button. So the gradient goes at a 90 degree angle from the top of the button to the bottom of the button.

    Edit: Okay, so I have one more idea that works, haha. Right now it works in XML, but should be doable for shapes in Java as well. It’s kind of complex, and I imagine there’s a way to simplify it into a single shape, but this is what I’ve got for now:

    green_horizontal_gradient.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
        <corners
            android:radius="3dp"
            />
        <gradient
            android:angle="0"
            android:startColor="#FF63a34a"
            android:endColor="#FF477b36"
            android:type="linear"
            />    
    </shape>
    

    half_overlay.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
        <solid
            android:color="#40000000"
            />
    </shape>
    

    layer_list.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list
        xmlns:android="http://schemas.android.com/apk/res/android"
        >
        <item
            android:drawable="@drawable/green_horizontal_gradient"
            android:id="@+id/green_gradient"
            />
        <item
            android:drawable="@drawable/half_overlay"
            android:id="@+id/half_overlay"
            android:top="50dp"
            />
    </layer-list>
    

    test.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        >
        <TextView
            android:id="@+id/image_test"
            android:background="@drawable/layer_list"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:gravity="center"
            android:text="Layer List Drawable!"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="26sp"     
            />
    </RelativeLayout>
    

    Okay, so basically I’ve created a shape gradient in XML for the horizontal green gradient, set at a 0 degree angle, going from the top area’s left green color, to the right green color. Next, I made a shape rectangle with a half transparent gray. I’m pretty sure that could be inlined into the layer-list XML, obviating this extra file, but I’m not sure how. But okay, then the kind of hacky part comes in on the layer_list XML file. I put the green gradient as the bottom layer, then put the half overlay as the second layer, offset from the top by 50dp. Obviously you’d want this number to always be half of whatever your view size is, though, and not a fixed 50dp. I don’t think you can use percentages, though. From there, I just inserted a TextView into my test.xml layout, using the layer_list.xml file as my background. I set the height to 100dp (twice the size of the offset of the overlay), resulting in the following:

    alt text

    Tada!

    One more edit: I’ve realized you can just embed the shapes into the layer list drawable as items, meaning you don’t need 3 separate XML files any more! You can achieve the same result combining them like so:

    layer_list.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list
        xmlns:android="http://schemas.android.com/apk/res/android"
        >
        <item>
            <shape
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle"
                >
                <corners
                    android:radius="3dp"
                    />
                <gradient
                    android:angle="0"
                    android:startColor="#FF63a34a"
                    android:endColor="#FF477b36"
                    android:type="linear"
                    />    
            </shape>
        </item>
        <item
            android:top="50dp" 
            >
            <shape
                android:shape="rectangle"
                >
                <solid
                    android:color="#40000000"
                    />
            </shape>            
        </item>
    </layer-list>
    

    You can layer as many items as you like this way! I may try to play around and see if I can get a more versatile result through Java.

    I think this is the last edit…: Okay, so you can definitely fix the positioning through Java, like the following:

        TextView tv = (TextView)findViewById(R.id.image_test);
        LayerDrawable ld = (LayerDrawable)tv.getBackground();
        int topInset = tv.getHeight() / 2 ; //does not work!
        ld.setLayerInset(1, 0, topInset, 0, 0);
        tv.setBackgroundDrawable(ld);
    

    However! This leads to yet another annoying problem in that you cannot measure the TextView until after it has been drawn. I’m not quite sure yet how you can accomplish this…but manually inserting a number for topInset does work.

    I lied, one more edit

    Okay, found out how to manually update this layer drawable to match the height of the container, full description can be found here. This code should go in your onCreate() method:

    final TextView tv = (TextView)findViewById(R.id.image_test);
            ViewTreeObserver vto = tv.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    LayerDrawable ld = (LayerDrawable)tv.getBackground();
                    ld.setLayerInset(1, 0, tv.getHeight() / 2, 0, 0);
                }
            });
    

    And I’m done! Whew! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.