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 9222475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:50:33+00:00 2026-06-18T03:50:33+00:00

How do I draw a radial gradient button in BlackBerry? I found Drawing Radial

  • 0

How do I draw a radial gradient button in BlackBerry? I found “Drawing Radial Gradients” on the BlackBerry support forums. All I am able to implement on my own is a linear gradient.

  • 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-06-18T03:50:34+00:00Added an answer on June 18, 2026 at 3:50 am

    This is a little tricky. Drawing linear gradients on field backgrounds is easy. Drawing radial gradients on field backgrounds is harder. Doing it on a button is harder still.

    First of all, the example you link to does indeed look really bad. The biggest problem with that code is that it uses Graphics.drawArc() to construct the gradient out of concentric circles (lines). This is not at all smooth.

    The biggest improvement you need to make over that is to use Graphics.fillArc() instead, which will look much smoother (although there may be a performance impact to this …).

    Your question didn’t say anything about how you wanted the button to look when focused, or whether the corners needed to be rounded. That’s where some of the difficulty comes in.

    If you just extend the RIM ButtonField class, you’ll probably have trouble with the default drawing for focus, and edge effects. It’s probably necessary to directly extend the base Field class in a new, written-from-scratch, button field. I wouldn’t necessarily recommend that you do all this yourself, since buttons require focus handling, click handling, etc. You should probably start with something like the BaseButtonField from the BlackBerry AdvancedUI open source library.

    I have prototyped this for you, using that class as a base. (so, you’ll need to download and include that source file in your project if you use this).

    I created a GradientButtonField subclass:

       private class GradientButtonField extends BaseButtonField {
    
          private int startR;
          private int startG;
          private int startB;
    
          private int endR;
          private int endG;
          private int endB;
    
          /** the maximum distance from the field's center, in pixels */
          private double rMax = -1.0;
    
          private int width;
          private int height;
    
          private String label;
          private int fontColor;
    
          /**
           * Create a gradient button field
           * @param startColor the integer Color code to use at the button center
           * @param endColor the integer Color code to use at the button edges
           * @param label the text to show on the button
           * @param fontColor color for label text
           */
          public GradientButtonField (int startColor, int endColor, String label, int fontColor) {         
             // record start and end color R/G/B components, to 
             //  make intermediate math easier
             startR = (startColor >> 16) & 0xFF;
             startG = (startColor >> 8) & 0xFF;
             startB = startColor & 0xFF;
             endR = (endColor >> 16) & 0xFF;
             endG = (endColor >> 8) & 0xFF;
             endB = endColor & 0xFF;
             this.label = label;
             this.fontColor = fontColor;
          }
    
          public String getLabel() {
             return label;
          }
    
          protected void layout(int w, int h) {        
             width = Math.min(Display.getWidth(), w);
             height = Math.min(Display.getHeight(), h);
             if (rMax < 0.0) {
                rMax = Math.sqrt((width * width)/4.0 + (height * height)/4.0);
             }
             setExtent(width, height);  
          }
    
          private int getColor(double scale, boolean highlighted) {
             int r = (int)(scale * (endR - startR)) + startR;
             int g = (int)(scale * (endG - startG)) + startG;
             int b = (int)(scale * (endB - startB)) + startB;
    
             if (highlighted) {
                // just brighten the color up a bit
                r = (int)Math.min(255, r * 1.5);
                g = (int)Math.min(255, g * 1.5);
                b = (int)Math.min(255, b * 1.5);
             }
             return (65536 * r + 256 * g + b);         
          }
    
          protected void paint(Graphics graphics) {
             int oldColor = graphics.getColor();
    
             // we must loop from the outer edge, in, to draw
             //  concentric circles of decreasing radius, and 
             //  changing color
             for (int radius = (int)rMax; radius >= 0; radius--) {
                double scale = ((double)radius) / rMax;
                boolean focused = (getVisualState() == Field.VISUAL_STATE_FOCUS);
                graphics.setColor(getColor(scale, focused));
                int x = width / 2 - radius;
                int y = height / 2 - radius;
                graphics.fillArc(x, y, 2 * radius, 2 * radius, 0, 360);           
             }
    
             String text = getLabel();
             graphics.setColor(fontColor);
             graphics.drawText(text, 
                   (width - getFont().getAdvance(text)) / 2,
                   (height - getFont().getHeight()) / 2);
    
             // reset graphics object
             graphics.setColor(oldColor);
          }         
       }
    

    To use this, the Manager that contains the button will need to constrain the button’s size in its sublayout() implementation. Or, you can edit my GradientButtonField class to hardcode a certain size (via getPreferredWidth(), layout(), etc.), or whatever you want.

      final Field button1 = new GradientButtonField(Color.DARKGRAY, Color.BLUE, 
            "Click Me!", Color.WHITE);
      final Field button2 =  new GradientButtonField(Color.DARKGRAY, Color.BLUE, 
            "Click Me, Too!", Color.WHITE);
    
      Manager mgr = new Manager(Manager.NO_VERTICAL_SCROLL) {
    
         public int getPreferredHeight() {
            return Display.getHeight();
         }
         public int getPreferredWidth() {
            return Display.getWidth();
         }
    
         protected void sublayout(int maxWidth, int maxHeight) {
            setExtent(getPreferredWidth(), getPreferredHeight());
    
            layoutChild(button1, 160, 80);
            setPositionChild(button1, 20, 50);
    
            layoutChild(button2, 120, 60);
            setPositionChild(button2, 20, 150);
         }
      };
    
      button1.setChangeListener(new FieldChangeListener() {
         public void fieldChanged(Field field, int context) {
            Dialog.alert("clicked!");               
         }
      });
    
      mgr.add(button1);
      mgr.add(button2);
      add(mgr);
    

    I did not round the corners, as that’s a bit of work. Depending on what kind of backgrounds you’re putting these buttons on, it might be easiest to create a PNG mask image (in your favorite drawing program), which is mostly transparent, and then just has filled corners that mask off the corners of the gradient below it. Then, use Graphics.drawBitmap() in the paint() method above, after you’ve drawn the radial gradient.

    For focus highlighting, I just put in some simple code to brighten the colors when the button is focused. Again, you didn’t say what you wanted for that, so I just did something simple.

    Here’s the result of the code above. The bottom button is focused:

    enter image description here

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

Sidebar

Related Questions

the pixman image library can draw radial color gradients between two circles. I'd like
How can I draw a sector (filled arc) with radial gradient in objective c
Degrafa newbie here :-). I was able to get com.degrafa.skins.CSSSkin to create linear gradient
On every mouse move I need to draw on my canvas a radial gradient
I would like to animate a radial gradient to shrink and grow the inner
The code below draws a perfect elliptical radial gradient, but does not fill in
Several answers mention to use GradientDrawable.setDither(true) to draw smooth gradients in Android. That has
I draw a triangle using mouse drag. After drawing many different triangles, I wanted
I'm working on a image manipulation app that overlays a radial gradient over an
Is there an easy way to draw a gradient like what is pictured here?

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.