casillas
casillas

Reputation: 16793

Putting TextViews in relative layout

I am trying to add three text views as follows, but I could not able to achieve it. I want A to be on the left side, and the other two texviews to be right side.

enter image description here

Here is what I get:

enter image description here

Here is my current implementation

<RelativeLayout
    android:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:background="#363636">
    <TextView
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="A"
        android:textColor="@android:color/white"
        android:paddingTop="3dp"
        android:paddingBottom="3dp"
        android:textSize="16sp"
        android:fontFamily="sans-serif-medium" />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:id="@+id/innerLayout">
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_centerVertical="true"
            android:text="B "
            android:textSize="16sp"
            android:textColor="@android:color/white"
            android:paddingRight="8dp" />
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_centerVertical="true"
            android:text="C "
            android:textSize="16sp"
            android:textColor="@android:color/white"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp" />
    </RelativeLayout>
</RelativeLayout>

Upvotes: 1

Views: 86

Answers (2)

Krish
Krish

Reputation: 3885

Change the xml like this ,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#363636"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

    <TextView android:background="#458765"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:fontFamily="sans-serif-medium"
        android:paddingBottom="3dp"
        android:paddingTop="3dp"
        android:text="A"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <RelativeLayout
        android:id="@+id/innerLayout"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentRight="true">

        <TextView android:background="#458765"
            android:id="@+id/b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:text="B"
            android:textColor="@android:color/white"
            android:textSize="16sp" />

        <TextView android:layout_marginLeft="5dp" android:background="#458765"
            android:layout_toRightOf="@id/b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:text="C"
            android:textColor="@android:color/white"
            android:textSize="16sp" />
    </RelativeLayout>
</RelativeLayout>

Upvotes: 1

bryan c
bryan c

Reputation: 1366

use this:

<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"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:id="@+id/A"
        android:text="aaaaaaa"
        android:padding="8dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:id="@+id/C"
        android:text="ccccccc"
        android:padding="8dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/C"
        android:id="@+id/B"
        android:text="bbbbbbb"
        android:padding="8dp"/>

</RelativeLayout>

Upvotes: 1

Related Questions