kopikaokao
kopikaokao

Reputation: 508

TextView Center Vertical not working in RelativeLayout

I am having trouble with "Location" TextView not able to align "center_vertical" side-by-side with a LinearLayout that contains a spinner child inside. CustomSpinner is derived from class Spinner to handle custom onItemSelected event.

Attached is the XML code and Screenshots of expected result vs actual result.

Any help would be appreciated !

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusable="true"
android:focusableInTouchMode="true">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:paddingBottom="4dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="8dp">

    <TextView
        android:id="@+id/tvCheckStockStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="6dp"
        android:text="Check Stock Status"
        android:textColor="@color/color_primary"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/tvItemCode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvCheckStockStatus"
        android:layout_marginLeft="3dp"
        android:text="@string/sample_text"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textStyle="bold"
        android:visibility="visible" />

    <EditText
        android:id="@+id/edDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/ivRemark"
        android:layout_below="@+id/tvItemCode"
        android:layout_marginBottom="2dp"
        android:layout_toLeftOf="@+id/ivRemark"
        android:layout_weight="1"
        android:hint="@string/description"
        android:inputType="textCapWords"
        android:selectAllOnFocus="true"
        android:text=""
        android:textSize="17sp" />

    <ImageView
        android:id="@+id/ivRemark"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tvItemCode"
        android:focusableInTouchMode="false"
        android:padding="2dp"
        android:src="@drawable/ic_action_remark"
        android:visibility="visible" />

    <TextView
        android:id="@+id/location"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/edDescription"
        android:layout_gravity="center_vertical"
        android:layout_weight="30"
        android:paddingRight="5dp"
        android:text="@string/location"
        android:textColor="#555555"
        android:textSize="15sp"
        android:visibility="visible"
        android:gravity="center_vertical|right"
        android:layout_alignWithParentIfMissing="false"
        android:allowUndo="false"
        android:layout_alignBottom="@+id/spLocationBlock" />

    <LinearLayout
        android:id="@+id/spLocationBlock"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F5F5F5"
        android:orientation="vertical"
        android:visibility="visible"
        android:layout_alignParentRight="true"
        android:layout_toEndOf="@id/location"
        android:layout_below="@+id/edDescription"
        android:measureWithLargestChild="false">

        <widgets.CustomSpinner
            android:id="@+id/spLocation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginTop="8dp"
            android:visibility="visible" />
    </LinearLayout>
 </RelativeLayout>

Expected Result Expected Result

Actual Result Actual Result

Upvotes: 0

Views: 992

Answers (3)

Rohit Suthar
Rohit Suthar

Reputation: 2693

change android:layout_width="wrap_content" and add android:layout_centerVertical="true"


See in below code.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:paddingBottom="4dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="8dp">

        <TextView
            android:id="@+id/tvCheckStockStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="6dp"
            android:text="Check Stock Status"
            android:textColor="@color/color_primary"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tvItemCode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvCheckStockStatus"
            android:layout_marginLeft="3dp"
            android:text="@string/sample_text"
            android:textColor="#000000"
            android:textSize="16sp"
            android:textStyle="bold"
            android:visibility="visible" />

        <EditText
            android:id="@+id/edDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/ivRemark"
            android:layout_below="@+id/tvItemCode"
            android:layout_marginBottom="2dp"
            android:layout_toLeftOf="@+id/ivRemark"
            android:layout_weight="1"
            android:hint="@string/description"
            android:inputType="textCapWords"
            android:selectAllOnFocus="true"
            android:text=""
            android:textSize="17sp" />

        <ImageView
            android:id="@+id/ivRemark"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/tvItemCode"
            android:focusableInTouchMode="false"
            android:padding="2dp"
            android:src="@drawable/ic_action_remark"
            android:visibility="visible" />

        <TextView
            android:id="@+id/location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_below="@+id/edDescription"
            android:layout_gravity="center_vertical"
            android:layout_weight="30"
            android:paddingRight="5dp"
            android:text="@string/location"
            android:textColor="#555555"
            android:textSize="15sp"
            android:visibility="visible"
            android:gravity="center_vertical|right"
            android:layout_alignWithParentIfMissing="false"
            android:allowUndo="false"
            android:layout_alignBottom="@+id/spLocationBlock" />

        <LinearLayout
            android:id="@+id/spLocationBlock"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F5F5F5"
            android:orientation="vertical"
            android:visibility="visible"
            android:layout_alignParentRight="true"
            android:layout_toEndOf="@id/location"
            android:layout_toRightOf="@id/location"
            android:layout_below="@+id/edDescription"
            android:measureWithLargestChild="false">

            <widgets.CustomSpinner
                android:id="@+id/spLocation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_marginTop="8dp"
                android:visibility="visible" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

Upvotes: 0

Piyush
Piyush

Reputation: 18923

Simply use :

android:layout_centerInParent="true"

for TextView

Upvotes: 0

Jay Rathod
Jay Rathod

Reputation: 11255

It is better to use in Relative Layout android:centerVertical="true" and alignParentRight="true" instead of android:gravity.

Upvotes: 1

Related Questions