0

How can I get the border width of a stand Android button programmatically? I simply need to resize the text to fit to the gray are, but I cannot do that without knowing the size of the border. Thanks http://s4.postimg.org/9w7idof4d/screenshot_Border_Width.png

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
  • Did you try to use padding? Somethink like this: [link](http://stackoverflow.com/questions/9685658/add-padding-on-view-programmatically) – Vyacheslav Jun 04 '13 at 17:34
  • I have the padding set to 0. Also, I would like to completely avoid XML because I don't even know where to begin in making XML work dynamically. – Dan Bray Jun 04 '13 at 17:52
  • try to change the values in code using setPadding method. [setPadding](http://developer.android.com/reference/android/view/View.html#setPadding%28int,%20int,%20int,%20int%29) – Vyacheslav Jun 04 '13 at 19:22
  • I already did that. cmdView.setPadding(0, 0, 0, 0); That does not work. – Dan Bray Jun 04 '13 at 19:26
  • The text will fit correctly if I can set it to the correct size. I just need to find the width of the actual button not including its border, and pass it to my SetTextSize function. – Dan Bray Jun 04 '13 at 19:30
  • The image is just a drawable shape. Try my answer. – Vyacheslav Jun 04 '13 at 19:41
  • possible duplicate of [How to get the button's border size in Android](http://stackoverflow.com/questions/17374977/how-to-get-the-buttons-border-size-in-android) – Karthik Balakrishnan Jul 05 '13 at 03:36

3 Answers3

1

ShapeDrawable bgShape = (ShapeDrawable )btnbck.getBackground();

    Rect padding = null;
    bgShape.getPadding(padding );

The image of button is just a drawable object. So you have to get the value of it. Anyway it can do with the shape anything you want.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • This won't work. This line of code: ShapeDrawable bgShape = (ShapeDrawable) cmdView.getBackground(); prevents my program from running – Dan Bray Jun 04 '13 at 21:33
  • 1
    I changed Rect padding = null; to Rect padding = new Rect(); and ShapeDrawable to Drawable and my program no longer crashes, however the padding returned is completely the wrong value. I read somewhere that the standard buttons are use NinePatchDrawable images. I think thats the problem. I've found no way to get the background as a NinePatchDrawable – Dan Bray Jun 12 '13 at 02:52
1

I've finally found a solution. The default padding for spinners happens to be the same as the border of a button.

int paddingLeft = mySpinner.getPaddingLeft();

Hopefully it will be the same on all devices.

Edit: It is not the same on all devices. I still don't have a working solution

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
0

This gets the width and height of the button's border:

final Button button = new Button(this);
params = new RelativeLayout.LayoutParams(50, 50);
layout.addView(button, params);
button.post(new Runnable()
{
    @Override
    public void run()
    {
         button.buildDrawingCache();
         Bitmap viewCopy = button.getDrawingCache();

         boolean stillBorder = true;
         PaddingLeft = 0;
         PaddingTop = 0;
         while (stillBorder)
         {
             int color = viewCopy.getPixel(PaddingLeft, button.getHeight() / 2);
             if (color != Color.TRANSPARENT)
                 stillBorder = false;
             else
             PaddingLeft++;
         }              
         stillBorder = true;
         while (stillBorder)
         {
             int color = viewCopy.getPixel(button.getWidth() / 2, PaddingTop);
             if (color != Color.TRANSPARENT)
                 stillBorder = false;
             else
                 PaddingTop++;
         }
     }
});
Dan Bray
  • 7,242
  • 3
  • 52
  • 70