0

In my relative layout, I have a circle View and a TextView. I would like to center the TextView exactly within the circle View. How do I do that? The code I have so far is below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <View
        android:id="@+id/firstCircle"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/circle" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/number1" />

</RelativeLayout>

I want the final result to look like:

Screenshot

quartz
  • 69
  • 2
  • 6

6 Answers6

2

try this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:id="@+id/firstCircle"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/circle" >
    </View>

    <TextView
        android:id="@+id/number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="28dp"
        android:text="1" />

</RelativeLayout>
Jenis Kasundra
  • 583
  • 9
  • 19
1

To paste a view on other framelyout is used.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:id="@+id/firstCircle"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/circle" />

        <TextView
            android:id="@+id/number1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:layout_alignBottom="@+id/firstCircle"
            android:layout_alignRight="@+id/firstCircle"
            android:layout_alignEnd="@+id/firstCircle"
            android:layout_alignLeft="@+id/firstCircle"
            android:layout_alignStart="@+id/firstCircle"
            android:layout_alignTop="@+id/firstCircle"
            android:gravity="center" />
    </RelativeLayout>

</FrameLayout>
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
1
<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <View
            android:id="@+id/firstCircle"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/circle" />

        <TextView
            android:id="@+id/number1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:layout_alignBottom="@+id/firstCircle"
            android:layout_alignRight="@+id/firstCircle"
            android:layout_alignEnd="@+id/firstCircle"
            android:layout_alignLeft="@+id/firstCircle"
            android:layout_alignStart="@+id/firstCircle"
            android:layout_alignTop="@+id/firstCircle"
            android:gravity="center" />
    </RelativeLayout>
Ankesh kumar Jaisansaria
  • 1,563
  • 4
  • 26
  • 44
0

Change your view by any one layout as below it will work better,

 <LinearLayout
    android:id="@+id/firstCircle"
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="24dp"
    android:gravity="center"
    android:layout_gravity="center"
    android:background="@drawable/circle">

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="1"
       android:gravity="center"
       android:id="@+id/number1" />
</LinearLayout>
Vickyexpert
  • 3,147
  • 5
  • 21
  • 34
0

I think you want circleview as background of TextView. then why not that view as background as TextView.

Try this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle"
        android:text="1"
        android:gravity="center"
        android:padding="10dp"
        android:id="@+id/number1" />

</RelativeLayout>

Note : you can adjust padding according to your need

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
0

You don't need to use some extra View, simply set circle image as background of TextView and set gravity to center as below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:gravity="center"
            android:background="@drawable/circle"
            android:id="@+id/number1" />

    </RelativeLayout>
Android Geek
  • 8,956
  • 2
  • 21
  • 35