Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Relative Layout Align Center

I am trying to create a List Activities Item Layout as follows

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent">     <ImageView          android:contentDescription="ss"         android:id="@+id/place_category_icon"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:paddingRight="15dp"         android:paddingTop="10dp" android:src="@drawable/marker"/>      <TextView         android:id="@+id/place_distance"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentRight="true"         android:text="320" />      <TextView         android:id="@+id/place_title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_toRightOf="@+id/place_category_icon"         android:text="Place Name"         android:textColor="#FFFF00"         android:textSize="14sp"         android:textStyle="bold" /> </RelativeLayout> 

I want the Layout to be displayed as follows.

enter image description here

i want to align it center Horizontal

enter image description here

like image 498
Harsha M V Avatar asked Dec 10 '12 08:12

Harsha M V


People also ask

How do I center align in relative layout?

If you want to make it center then use android:layout_centerVertical="true" in the TextView. my Relative Layout has fill_parent for both height and width. Since this is the layout for an Item inside a ListView i want many of them to stack on top of each other.

How do you center a button in RelativeLayout?

If you have a single Button in your Activity and you want to center it the simplest way is to use a RelativeLayout and set the android:layout_centerInParent=”true” property on the Button.

How do I center text in RelativeLayout android?

android:layout_centerHorizontal="true" android:gravity="center" I think it's correct. so that TextView will be centered in Layout, and text will be centered in TextView.


2 Answers

I hope this will work

EDITED

  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:paddingRight="15dp" >      <ImageView         android:id="@+id/place_category_icon"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerVertical="true"         android:contentDescription="ss"         android:paddingTop="10dp"         android:src="@drawable/ic_launcher" />      <TextView         android:id="@+id/place_distance"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentRight="true"         android:layout_centerVertical="true"         android:text="320" />      <TextView         android:id="@+id/place_title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerVertical="true"         android:layout_marginLeft="15dp"         android:layout_toRightOf="@+id/place_category_icon"         android:text="Place Name"         android:textColor="#FFFF00"         android:textSize="14sp"         android:textStyle="bold" />  </RelativeLayout> 
like image 123
Faizan Avatar answered Sep 22 '22 21:09

Faizan


If you want to make it center then use android:layout_centerVertical="true" in the TextView.

like image 21
Paresh Mayani Avatar answered Sep 22 '22 21:09

Paresh Mayani