How to put a texView inside a circle

1

Gentlemen I would like to make a textView richer by leaving it inside a Circle, it is a Product List with the product name and the value and this one that would be inside the circle, I tried to make a shape and use it as background was not cool, as you can see my rounded_shape.xml down

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="2dp">

<solid android:color="@color/amarelo2"/>
<stroke android:color="@color/branco" android:width="2dp"/>
<corners android:radius="2dp"/>

and making use of it

 <TextView
    android:textColor="@color/colorPrimarytext"
    android:id="@+id/tv_valor"
    android:background="@drawable/rounded_shape"

Plus the effect was horrible

    
asked by anonymous 10.02.2017 / 17:04

2 answers

0
  

layout_rounded_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape>
            <solid android:color="#694CA7"/>
            <stroke android:width="1dp" android:color="#694CA7" />
            <corners android:radius="100dp" />
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape>
    </item>
</selector>

To edit the color change the line: <solid android:color=".." and also do not forget the border or, if you prefer, you can remove it.

Once you have created drawable apply as background to container :

<FrameLayout
        android:background="@drawable/rounded"
        android:layout_width="56dp"
        android:layout_height="56dp">

    <TextView
            android:id="@+id/tv_valor"
            android:text="LC"
            android:textSize="14sp"
            android:textColor="#fff"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</FrameLayout>

Result

    
10.02.2017 / 17:28
0

Create a circular shape:

circular_textview.xml

<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#ffffff" />    
    <stroke
        android:width="2dp"
        android:color="#000000" />
    <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />

</shape>

Declare TextView in this way

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="texto"
    android:gravity="center"
    android:background="@drawable/circular_textview"/>

For the circle to fit the dimensions of the text include this code in onCreate() :

final View circularTextView = findViewById(R.id.textView);
circularTextView.getViewTreeObserver().addOnGlobalLayoutListener(new

     ViewTreeObserver.OnGlobalLayoutListener() {
         @Override
         public void onGlobalLayout() {

            //Remove o listenner para não ser novamente chamado.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                circularTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                //noinspection deprecation
                circularTextView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

             //Coloca a largura igual à altura
             int tvWidth = circularTextView.getWidth();
             ViewGroup.LayoutParams tvLayout = circularTextView.getLayoutParams();
             tvLayout.height = tvLayout.width = tvWidth;
             circularTextView.setLayoutParams(tvLayout);
         }
     });
    
10.02.2017 / 17:38