Ifeanyi Onyejekwe Ifeanyi Onyejekwe - 1 month ago 8
Android Question

XML for Android Studio

Fairly new to android, I am working on a page, and I want my texts and image to be placed perfectly on the centre of the page. something like this:How I want my result to be.

Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_about"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="mealsthatheals.emfitech.com.About">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/textColorPrimary"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/Appname" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/version"
android:textColor="@color/textColorPrimary"
android:textSize="10sp"
android:id="@+id/version"
android:layout_marginTop="18dp"
android:layout_below="@+id/Appname"
android:layout_centerHorizontal="true" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:id="@+id/imageView"
android:layout_below="@+id/version"
android:layout_alignStart="@+id/Appname"
android:layout_marginTop="39dp" />

<TextView
android:id="@+id/webLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="web"
android:gravity="center"
android:padding="20px"
android:text="@string/www.emfitech.com"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColorLink="@color/colorPrimary"
android:layout_below="@+id/imageView"
android:layout_alignEnd="@+id/imageView"
android:layout_marginTop="28dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/copyright"
android:textColor="@color/textColorPrimary"
android:textSize="14sp"
android:id="@+id/copywright"
android:layout_marginTop="54dp"
android:layout_centerHorizontal="true"
android:layout_alignTop="@+id/webLink"
android:layout_alignParentEnd="true"
android:layout_marginEnd="73dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/all_rights"
android:textColor="@color/textColorPrimary"
android:textSize="10sp"
android:id="@+id/reserve"
app:layout_anchor="@+id/imageView"
android:layout_marginTop="22dp"
android:layout_below="@+id/copywright"
android:layout_alignEnd="@+id/version" />

</RelativeLayout>


So I want the outcome to be all centred in the middle. enter image description here

Answer

I have done it like this by using android:layout_centerHorizontal="true".

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_about"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
    android:id="@+id/Appname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/app_name"
    android:textColor="@color/textColorPrimary"
    android:textSize="18sp" />

<TextView
    android:id="@+id/version"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Appname"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:text="@string/version"
    android:textColor="@color/textColorPrimary"
    android:textSize="10sp" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/version"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="39dp"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/webLink"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    android:autoLink="web"
    android:gravity="center"
    android:padding="20px"
    android:text="@string/www.emfitech.com"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColorLink="@color/colorPrimary" />

<TextView
    android:id="@+id/copywright"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/webLink"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="54dp"
    android:text="@string/copyright"
    android:textColor="@color/textColorPrimary"
    android:textSize="14sp" />

<TextView
    android:id="@+id/reserve"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/copywright"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="22dp"
    android:text="@string/all_rights"
    android:textColor="@color/textColorPrimary"
    android:textSize="10sp" />

Comments