Drawing an outlined RoundRect

by TheFluffyOne » Thu, 08 Jul 2010 02:06:33 GMT

Sponsored Links

I have a method that, when passed some text, draws an outlined round
rectangle with that text inside, viz:


At the moment I'm making two draw calls in order to draw this outlined
round rectangle, and see slight bulging at the corners that I assume
is due to the use of the same radius for the corners for both the
inner and outer rectangle. Is there a better way? Main code is below.


        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        Rect textBounds = new Rect();
        paint.getTextBounds(nodeText, 0, nodeText.length(), textBounds);
        FontMetricsInt fm = paint.getFontMetricsInt();
        int textWidth = textBounds.width();

        float[] outerR = new float[] { radius, radius, radius, radius,
radius, radius, radius, radius };
        RectF   inset = new RectF(linewidth, linewidth, linewidth,
        float[] innerR = new float[] { radius, radius, radius, radius,
radius, radius, radius, radius };

        ShapeDrawable back = new ShapeDrawable(new RoundRectShape(outerR,
null, null));
        back.setBounds(x - border, y + fm.ascent - border, x + textWidth +
border, y + fm.descent + border);

        ShapeDrawable outline = new ShapeDrawable(new RoundRectShape(outerR,
inset, innerR));
        outline.setBounds(x - border, y + fm.ascent - border, x + textWidth +
border, y + fm.descent + border);

        canvas.drawText(nodeText, x, y, paint);


Drawing an outlined RoundRect

by blcooley » Thu, 08 Jul 2010 02:33:51 GMT

 I don't know the answer to your question directly, but you can set the
stroke of a shape drawable specified in xml. I don't see a method for
that in the API docs, but it doesn't seem like you need both the back
and outline ShapeDrawables if you go that route. Here's a link:

 http://developer.android.com/guide/topics/resources/drawable-resource.html #Shape

Good luck!

Best regards,
Brian Cooley


Sponsored Links

Drawing an outlined RoundRect

by blcooley » Thu, 08 Jul 2010 02:37:31 GMT

 Or, maybe you could try a GradientDrawable? Looks like that's what you
are after. It has a setStroke method.


Best regards,
Brian Cooley


Other Threads

1. Display indeterminate progressbar in Status bar on Android


I am trying to display an animated graphic identical to the
indeterminate progress bar (not the horizontal one, but the circular
one) in the status bar while my on-going notification is alive.

I tried to find the resource ID corresponding to the indeterminate
progress bar, but found that it is animated via code.

I tried setting the icon ID in my Notification instance to an animated
GIF, but only the first frame of the GIF is displayed in the Status

If I set the icon ID to
android.R.drawable.progress_indeterminate_horizontal, the graphic
animates perfectly. So, my question is- how is the animation achieved
in this case? Through iconLevel? How can I set an animated icon
without requiring to animate it periodically myself?

Any pointers will be really appreciated.




2. I'm stuck - global variable problem

Hi all,

so I searched the net and found a nice way to make a global variable
by creating a class that extends Application class. I did that. But I
am having problems with it. So this is the situation:

public class Group {
 public String mName;
 public int mID;
 public ArrayList<Items> mItems;

public class Item {
 public String mName;

public class MyApplicationApp extends Application {
 private ArrayList<Group> mGroups;

public ArrayList<Group> getGlobalArrayVariable() {
                return mGroups;

        public synchronized void setGlobalArrayVariable(ArrayList<Group> s) {
                mGroups = s;

So these are the classes I believe are cruical for this question. Next
this is that in my Activity classes I set and get this ArrayList
variable. So I set it in my initial Activity class that is called when
the application is run. There I fill the array and just to be sure I
read it with Log functionality after filling the array in the
MyApplicationApp class and everything is ok.

Then I try to read this from another Activity class where I go to
after a menu choice. In that activity I successfully read the
application, meaning I get a handle to MyApplicationApp object (I
guess). This is how I do it:

private MyApplicationApp mMyApp;
private ArrayList<Group> mGroupsList;

protected void onCreate(Bundle savedInstanceState) {

                mMyApp = (MyApplicationApp)this.getApplication();
                if (mMyApp == null) Log.i(TAG,"mMyApp == null");

                mGroupsList = (ArrayList<Group>)mMyApp.getGlobalArrayVariable();
                if (mGroupsList == null) Log.i(TAG,"1. mGroupsList == null");

                mGroupsList =
                if (mGroupsList == null) Log.i(TAG,"2. mGroupsList == null");

So mMyApp is not null and that is fine I guess, but mGroupsList is
null and I presume that is way my app crashes after this last log
entry. I guess it crashes because my variable is null?? Anyway even if
the reason for crashing is other then this variable being null I am
still puzzled why am I not able to get my global variable in this

The reason for two different ways of getting something into
mGroupsList variable is because I was trying to see if I am doing it
incorrectly but it is the same as far as I know...

Any ideas or pointers what should I do?



3. Bluetooth Walkie Talkie

4. smime on android

5. always show divider of ListView?

6. tasks and activity

7. Listview in a listview without ExpandableList