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

  • SEARCH
  • Home
  • 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 1048965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:32:25+00:00 2026-05-16T16:32:25+00:00

I am creating a button programmatically. It is rounded and has a gradient background,

  • 0

I am creating a button programmatically. It is rounded and has a gradient background, and works fine and looks nice, but I couldn’t do two things I wanted:

  1. Set a 1 pixel stroke with a given color. I tried getPaint().setStroke(), but couldn’t figure how to set the stroke color. How should I do it?
  2. Align the gradient to the bottom of the button, no matter what height it has. Is this possible?

For reference, this is the code I’m using:

Button btn = new Button(context);
btn.setPadding(7, 3, 7, 5);
btn.setTextColor(text_color);

// Create a gradient for the button. Height is hardcoded to 30 (I don't know the height beforehand). 
// I wish I could set the gradient aligned to the bottom of the button.
final Shader shader = new LinearGradient(0, 0, 0, 30,
    new int[] { color_1, color_2 },
    null, Shader.TileMode.MIRROR);

float[] roundedCorner = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 }
ShapeDrawable normal = new ShapeDrawable(new RoundRectShape(roundedCorner, null, null));
normal.getPaint().setShader(shader);
normal.setPadding(7, 3, 7, 5);

// Create a state list (I suppressed settings for pressed).
StateListDrawable state_list = new StateListDrawable();
state_list.addState(new int[] { }, normal);

btn.setBackgroundDrawable(state_list);
  • 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-16T16:32:26+00:00Added an answer on May 16, 2026 at 4:32 pm

    In terms of your first question, I struggled with this as well, and it doesn’t look like there are any suitable methods within Drawables themselves (I was using ShapeDrawable) or the Paint class. However, I was able to extend ShapeDrawable and override the draw method, as below, to give the same result:

    public class CustomBorderDrawable extends ShapeDrawable {
        private Paint fillpaint, strokepaint;
        private static final int WIDTH = 3; 
    
        public CustomBorderDrawable(Shape s) {
            super(s);
            fillpaint = this.getPaint();
            strokepaint = new Paint(fillpaint);
            strokepaint.setStyle(Paint.Style.STROKE);
            strokepaint.setStrokeWidth(WIDTH);
            strokepaint.setARGB(255, 0, 0, 0);
        }
    
        @Override
        protected void onDraw(Shape shape, Canvas canvas, Paint fillpaint) {
            shape.draw(canvas, fillpaint);
            shape.draw(canvas, strokepaint);
        }
    
        public void setFillColour(int c){
            fillpaint.setColor(c);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating buttons programmatically. I want to change button background color while touching
I have a scrollview in which i am creating buttons programmatically. On touch, button
I've seen several questions/answers related to entirely creating a button in code, but not
I m creating custom checkbox and radio button in pure css but they are
I am creating 2 buttons programmatically in my view. Both selector methods exist but
Why are we using * character when we are creating button but we aren't
Programmatically I am creating buttons and keeping some background image using setBackgroundImage. I want
I am programatically creating buttons and each button has a tag binary array. While
I'm creating some UIButton s programmatically in a loop but I'm having some problem
I am bit stack with creating two UIViews which are switchable (each UIView 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.