user3796130
user3796130

Reputation:

how to centralize a textview inside of imageview android

I have a LinearLayout and i just want to put a text at the middle of my imageview which is a shape.

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/state_area"
            android:layout_width="wrap_content"
            android:textColor="#F000"
            android:layout_height="90dp"
            android:textSize="40sp"
            android:layout_marginBottom="24dp"
            android:layout_gravity="center_horizontal" />

    </LinearLayout>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/shape"
        android:layout_gravity="center" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:src="@drawable/shape"
        android:layout_gravity="center" />

</LinearLayout>

The result so far :

enter image description here

Upvotes: 0

Views: 135

Answers (3)

pipeur10
pipeur10

Reputation: 73

Wrap your image and textview in framelayout 

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <FrameLayout android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:src="@drawable/shape"
                android:layout_gravity="center" />
            <TextView
                android:id="@+id/state_area"
                android:layout_width="wrap_content"
                android:textColor="#F000"
                android:layout_height="90dp"
                android:textSize="40sp"
                android:gravity:"center"
                android:layout_marginBottom="24dp"
                android:layout_gravity="center" />
        </FrameLayout>



        <FrameLayout android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:src="@drawable/shape"
                android:layout_gravity="center" />
            <TextView
                android:id="@+id/state_area2"
                android:layout_width="wrap_content"
                android:textColor="#F000"
                android:layout_height="90dp"
                android:textSize="40sp"
                android:gravity:"center"
                android:layout_marginBottom="24dp"
                android:layout_gravity="center" />
        </FrameLayout>
    </LinearLayout>

Upvotes: 0

Matthew Shearer
Matthew Shearer

Reputation: 2795

you could just use a textview and set the shape as the background.

<TextView
        android:id="@+id/state_area"
        android:layout_width="wrap_content"
        android:textColor="#F000"
        android:layout_height="90dp"
        android:background="@drawable/shape"
        android:textSize="40sp"
        android:layout_marginBottom="24dp"
        android:layout_gravity="center_horizontal" />

Upvotes: 0

proy31
proy31

Reputation: 123

You can use Relative layout to arrange the layout on top of another. Or your can use a custom circle drawable as the background to the textview

Upvotes: 1

Related Questions