10

I'm trying to position one item in a RelativeLayout to the right of another. In the example below I want the "," to be placed to the right of the description field. However the problem is it puts the comma on the first line next to the name field. Can anyone help?

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10sp">
    <TextView 
        android:id="@+id/name"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/name" />
    <TextView
        android:id="@+id/descriptioncomma"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=", "
        android:layout_toRightOf="@id/description" />
Floern
  • 33,559
  • 24
  • 104
  • 119
GuybrushThreepwood
  • 5,598
  • 9
  • 55
  • 113

3 Answers3

13

Try this code, it should work: Button used layout_toRightOf attribute with id of Textview widget.

<?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:orientation="horizontal"
    android:padding="10sp">
    <TextView 
        android:id="@+id/name" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/description" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/name" />
    <TextView
        android:id="@+id/descriptioncomma" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=","
        android:layout_below="@id/name"
        android:layout_toRightOf="@id/description" />
</RelativeLayout>

In Both TextView you add text and than check it.

OK

R kanojia
  • 105
  • 4
  • 17
Mitesh Jain
  • 673
  • 1
  • 7
  • 20
2

use this one

  <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10sp">
    <TextView 
    android:id="@+id/name"
    android:text="na"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" />
    <TextView
    android:id="@+id/description"
    android:text="desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/name" />
    <TextView
    android:id="@+id/descriptioncomma"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=", "
    android:layout_toRightOf="@id/description"
    android:layout_below="@id/name" />
            </RelativeLayout>
Jyosna
  • 4,436
  • 13
  • 60
  • 93
-1

Here it will help full : Button used layout_toRightOf attribute with id of Textview widget.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="TextView"
            android:textSize="18sp"
            android:padding="10dp"/>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_toRightOf="@id/textView"
            android:text="Button" />
    </RelativeLayout>

Here you can find Complete examples of linear layout positioning

R kanojia
  • 105
  • 4
  • 17
  • Welcome to Stack Overflow! You can make your answer better by explaining your code. Also, when you post a link it's important to quote the relevant parts of the linked page, in case the page is taken down later. – jkdev Jul 11 '18 at 08:27