5

I have a TextView (indicated in green below) and a LinearLayout (indicated in red below) in a RelativeLayout. I want to position the TextView on top of the LinearLayout, like this:

However, I tried this:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/linear_layout">
    <!--some other views-->
</LinearLayout>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" <!--this because the textview is the topmost view on the screen so I tried to use this-->
    android:layout_alignLeft="@id/linear_layout"
    android:text="my text"
    android:textSize="10pt"
    android:id="@+id/text1"/>

However, when I run the app, it is like this:

enter image description here

So I want to know what did I do wrong and how to fix it. Is there an xml attribute that I can use?

If you need more code to identify the problem, feel free to tell me!

Sweeper
  • 213,210
  • 22
  • 193
  • 313

4 Answers4

6

Put your LinearLayout after TextView like this:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" <!-- this because the textview is the topmost view on the screen so I tried to use this -->
    android:text="my text"
    android:textSize="10pt"
    android:id="@+id/text1" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text1"
    android:orientation="vertical"
    android:id="@+id/linear_layout">
    <!-- some other views -->
</LinearLayout>
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
SRB Bans
  • 3,096
  • 1
  • 10
  • 21
4

In Java, you can do it by text1.bringToFront();

zackygaurav
  • 4,369
  • 5
  • 26
  • 40
1

Add this line in your LinearLayout:

<LinearLayout
...
android:layout_below:"@+id/text1">

</LinearLayout>
Andro
  • 952
  • 9
  • 19
0

Replacing your layout with following one should work.

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" 
    android:layout_alignLeft="@id/linear_layout"
    android:text="my text"
    android:textSize="10pt"
    android:id="@+id/text1"/>
<LinearLayout
    android:id="@+id/linearlayout"
    android:layout_below="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/linear_layout">
    <!--some other views-->
</LinearLayout>