vinoth kumar vinoth kumar - 4 months ago 16
Android Question

How to align TextView in image center

I am try to display a textview in image center, but not working in LinearLayout.


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

<ImageView
android:id="@+id/kurtis"
android:layout_width="0dp"
android:layout_height="325dp"
android:layout_weight="0.5"
android:scaleType="fitXY"
android:src="@mipmap/kurtis" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_text="Testing" />

<ImageView
android:id="@+id/salwars"
android:layout_width="0dp"
android:layout_height="325dp"
android:layout_marginLeft="10dp"
android:layout_weight="0.5"
android:scaleType="fitXY"
android:src="@mipmap/salwar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_text="Testing"/>




Any one have idea please help me.

I edited my question, this is my problem.

Answer

You could, for example, use RelativeLayout to center your TextView over the ImageView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="325dp"
        android:layout_weight="0.5">

        <ImageView
            android:id="@+id/kurtis"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@android:drawable/btn_star"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_text="Testing"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="325dp"
        android:layout_weight="0.5">

        <ImageView
            android:id="@+id/salwars"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@android:drawable/btn_star"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_text="Testing"/>
    </RelativeLayout>
</LinearLayout>