user3433865
user3433865

Reputation: 1

XML Android align TextView

This is my first try to create an android application.
I am trying to create a layout right now and am stuck allready.
Here is what I am trying to accomplish:

         Jaarverbruik
         2013 - 2014
Afrekendatum         Peildatum

So, the word Afrekendatum should be aligned to the left, the word Peildatum should be aligned to the right of the screen.

This is my current code:

<?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"
android:paddingLeft="16dp"
android:paddingRight="16dp" >

<TextView
    android:id="@+id/periode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:textSize="20sp"
    android:text="@string/periode" />

<TextView
    android:id="@+id/afrekendatum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/periode"
    android:layout_alignParentLeft="true"
    android:text="@string/afrekendatum" />

<TextView
    android:id="@+id/peildatum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/periode"
    android:layout_toRightOf="@id/afrekendatum"
    android:layout_alignParentRight="true"
    android:text="@string/peildatum" />

</RelativeLayout>

Sadly, the words are displayed directly behind each other.
What is the best way to fix this?

Upvotes: 0

Views: 72

Answers (3)

Silambarasan Poonguti
Silambarasan Poonguti

Reputation: 9442

Try this...

You have to set TextView's gravity.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2b579a"
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=".MainActivity1" >

<TextView
    android:id="@+id/periode"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/periode"
    android:textSize="20sp" 
    android:textColor="#FFFFFF"
    android:layout_margin="10dp"/>

<TextView
    android:id="@+id/afrekendatum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:gravity="start"
    android:textSize="20sp"
    android:textColor="#FFFFFF"
    android:layout_below="@+id/periode"
    android:text="@string/afrekendatum" />

<TextView
    android:id="@+id/peildatum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/periode"
    android:gravity="end"
    android:textSize="20sp"
    android:textColor="#FFFFFF"
    android:layout_toRightOf="@id/afrekendatum"
    android:text="@string/peildatum" />

I also attached screenshot here Genius!

TextView Alignment in Relative layout

Upvotes: 0

Kunu
Kunu

Reputation: 5134

You don't need android:layout_toRightOf="@id/afrekendatum" because you have declared android:layout_alignParentRight="true". Remove it and it will work fine.

Upvotes: 1

MSr
MSr

Reputation: 288

You should use something like this.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

Upvotes: 0

Related Questions