ankita gahoi
ankita gahoi

Reputation: 1562

android:layout_centerHorizontal="true" is not working in android

I am using this layout to make image and text in center.but its not working .i am simply using android:layout_centerHorizontal="true".why its not working i dont understand. can someone please help to solve this problem?

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff">


        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="70dp"
            android:background="@drawable/image_tutorial1" />

        <TextView
            android:textSize="14dp"
            android:id="@+id/title"
            android:layout_below="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/info4"
            android:textColor="#000000"
            android:textStyle="bold" />

    </RelativeLayout>

Upvotes: 3

Views: 11107

Answers (4)

Amna Shaheen
Amna Shaheen

Reputation: 41

With RelativeLayout use:

android:layout_centerInParent="true"

with

android:layout_width="wrap_content"

Upvotes: 4

Reeman Adel
Reeman Adel

Reputation: 51

you using this wrong for your RelativeLayout

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

it should be

android:layout_height="match_parent"
android:layout_width="match_parent"

Upvotes: 0

s.d
s.d

Reputation: 29436

In the layout, RelativeLayout's width is set to wrap-content, its not taking the entire available width at all. It's contents are being centered but their parent View is shrinking to fit around them.

try fill_parent for root:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"

Also, a LinearLayout with orientation="vertical" and gravity="center_horizontal" will do the same, more easily.

Upvotes: 6

Rohit
Rohit

Reputation: 3408

use [android:layout_centerHorizontal="true"] under RelativeLayout layout, then it will work

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

Upvotes: 0

Related Questions