How to place a texView inside a Circle

Asked

Viewed 637 times

1

Gentlemen I would like to leave richer a textView leaving it within a Circle, is a Product List with the name of the product and the value and this that would be within the circle, I tried to make a Shape and use it as background more did not get cool, as you can see my rounded_shape.xml bass

<?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"

But the effect was horrible

  • Thanks to all who helped me, Luc’s response was the one that best suited to what I wanted because the textView inside a Layout and this already with the Background in the form of a circle was exactly what I wanted, seeking to put a background in the textView I was not finding the desired result, it may even be that with an adjustment got what needed. Thank you all.

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 edge or, if you prefer, you can remove it.

After having created the drawable apply as a background 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>

Upshot

inserir a descrição da imagem aqui

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 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"/>

So that the circle fits the text dimensions include this code in the 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);
         }
     });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.