TheDevMan
TheDevMan

Reputation: 5954

TextView on top of ImageView in Android

I am trying to insert a textView on top on ImageView. Image is a circle and inside relativeLayout.

I am not able insert the TextView in the center of Circle.

Snapshot:

Text Inside Circle

Please take a look at the snapshot and the code.

<RelativeLayout
            android:id="@+id/circlenumberlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/layoutbackground" >

            <ImageView
                android:id="@+id/numberCircle"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:padding="5dp"
                android:src="@drawable/circle"
                tools:ignore="RtlHardcoded" />
            
            <TextView
                android:id="@+id/numberText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/numberCircle"
                android:layout_marginRight="16dp"
                android:gravity="center"
                android:text="200"
                android:textStyle="bold" 
                android:textColor="@color/whiteText"
                android:textSize="14sp"/>

        </RelativeLayout>

Can somebody help me to get the textView centered to the imageCircle. I have been working on this past one hour?

Thanks!

Upvotes: 0

Views: 968

Answers (2)

Gagan
Gagan

Reputation: 1497

If your requirement is just to have a TextView with a border such that the text used for TextView is always at the center then you can achieve it using below code instead of having a RelativeLayout and an ImageView.

<TextView
    android:id="@+id/numberText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="@drawable/circle"
    android:text="200"
    android:textStyle="bold"
    android:textColor="@color/whiteText"
    android:textSize="14sp"/>

Upvotes: 0

tachyonflux
tachyonflux

Reputation: 20211

Change the RelativeLayout width and height to wrap_content.

Use android:layout:centerInParent="true" in your TextView.

Upvotes: 2

Related Questions