LaBird
LaBird

Reputation: 299

Android: Radius of Circle in TextView Always Fixed by TextView Width and Height

I'd like to display a row of numbers, each printed at the center of a different circle. However, the size of the set is determined by user input. What I have achieved is this:

            RelativeLayout myLayout =  (RelativeLayout)findViewById(R.id.layout1);

            // Creating a new TextView
            TextView[] tv = new TextView[size+1];
            TextView temptv;

            for (i = 1; i <= size; i++) {
                temptv = new TextView(MainActivity.this);
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

                if (i > 1) {
                    lp.addRule(RelativeLayout.BELOW, R.id.textView5);
                    lp.addRule(RelativeLayout.RIGHT_OF, tv[i-1].getId());
                }
                else {
                    lp.addRule(RelativeLayout.BELOW, R.id.textView5);
                    lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.textView5);
                }

                // width of Text
                temptv.setWidth(60);
                temptv.setHeight(60);
                // size of Text
                temptv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
                temptv.setGravity(Gravity.CENTER|Gravity.CENTER_VERTICAL);
                temptv.setLayoutParams(lp);
                myLayout.addView(temptv, lp);

                answer = "<font color='#000000'>" + mynum[i] + "</font>";

                // Update the label with our dynamic answer
                temptv.setText(Html.fromHtml(answer));

                temptv.setBackgroundResource(R.drawable.circle1);

                tv[i] = temptv;
                tv[i].setId(i);

            }

It works fine, but there is a problem. The radius of the circles is always half the width or height of the TextView. So in the above case, the radius is always 30, just like this picture: pic1

If the width and height of the TextView are different (e.g. using temptv.setWidth(75); and temptv.setHeight(50);), then I get ovals, like this picture: pic2 http://i841.photobucket.com/albums/zz337/lilpengi/number2_zps767ec27f.png

irrespective of the shape settings in my circle1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="oval">
<corners android:radius="25dip"/>
<stroke android:color="#c00000" android:width="5dip"/>
<solid android:color="#ffffff"/>
</shape>

Whatever I set at the radius, 10, 25, 50, the result is the same. What is going wrong with my code?

Upvotes: 0

Views: 1691

Answers (1)

adriannieto
adriannieto

Reputation: 352

With the standard Android widgets it's seems to be impossible, due you are drawing a "oval" around the component.

An oval could become a circle as a circle is an specialization where height is equal to width. My advice is to reimplement the way of component size is computed. Just square the layout always by overriding onMeasure (here is the more simple way to do, please click on the link in the bottom):

class SquareTextView extends TextView{

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = 0;
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        if (width > height) {
            size = height;
        } else {
            size = width;
        }
        setMeasuredDimension(size, size);
    }
}

http://www.jayway.com/2012/12/12/creating-custom-android-views-part-4-measuring-and-how-to-force-a-view-to-be-square/

Upvotes: 2

Related Questions