user924941
user924941

Reputation: 1011

Inserting a textview in the middle of a imageview android

I need to put a text view in the middle of a imageview so i have this code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/menu_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="1" />

<ImageView
    android:id="@+id/menu_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerInParent="true"
    android:layout_marginBottom="@dimen/menu_icon_margin_top_bottom"
    android:layout_marginRight="@dimen/menu_icon_margin_right"
    android:layout_marginTop="@dimen/menu_icon_margin_top_bottom" />

<TextView
    android:id="@+id/notif_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/menu_icon"
    android:layout_alignLeft="@id/menu_icon"
    android:layout_alignRight="@id/menu_icon"
    android:layout_alignTop="@id/menu_icon"
    android:lines="1"
    android:text="69"
    android:textColor="@android:color/white" />

and i need the last textview to be always in center of the menu_icon ImageView but my methdot doesnt work it just puts it in the top left corner.

Upvotes: 7

Views: 9785

Answers (3)

div
div

Reputation: 1533

FrameLayout is the best way to go. You can use a RelativeLayout also, btw.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="40dp"
    android:layout_height="40dp">

    <ImageView
        android:id="@+id/marker_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        tools:src="@drawable/ic_marker_selected" />

    <TextView
        android:id="@+id/marker_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="14sp"
        tools:text="A" />

</FrameLayout>

Upvotes: 1

Leonidos
Leonidos

Reputation: 10518

As your ImageView not fills all RelativeLayout you cant achieve desireable effect. I suggest you to place ImageView and TextView in FrameLayout.

<FrameLayout
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerInParent="true">

 <TextView
    android:id="@+id/menu_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:lines="1" />

 <ImageView
    android:id="@+id/menu_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="@dimen/menu_icon_margin_top_bottom"
    android:layout_marginRight="@dimen/menu_icon_margin_right"
    android:layout_marginTop="@dimen/menu_icon_margin_top_bottom" />
</FrameLayout>

Upvotes: 19

Damian
Damian

Reputation: 8072

You just need to add:

android:layout_centerInParent="true"

to your last TextView

Upvotes: 3

Related Questions