0

I am attempting to place two TextViews (one of which is <include/>'d) sequentially in a RelativeLayout, but they end up on top of each other.

I have the following res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lerenard.test.MainActivity">

    <include
        android:id="@+id/hello_layout"
        layout="@layout/hello_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/bonjour_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bonjour"
        android:textSize="@dimen/text_size"
        android:layout_below="@id/hello_layout"/>
</RelativeLayout>

res/layout/hello_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">

    <TextView
        android:id="@+id/hello_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="@dimen/text_size"/>
</merge>

What I would like to see is that bonjour_view is below hello_view. However, they are both squashed on top of each other. That is, it appears that the merge layout that bonjour_view references (hello_layout) has no height. This is supported by the fact that if I replace the merge with a LinearLayout, everything works as expected. However, I'd still like to get the performance benefits of using a merge if that's possible (and it seems like it should be given how simple this example is). I have tried referencing @id/hello_view, but I get Error:(20, 31) No resource found that matches the given name (at 'layout_below' with value '@id/hello_view'). when building the project.

Jon McClung
  • 1,619
  • 20
  • 28
  • 1
    `` tags don't have layout attributes because they come from the contained `View`s, since it's like those `View`s are directly in the parent `ViewGroup`. Anyhoo, if you use `@+id/hello_view` (notice the `+`), that should work. – Mike M. Dec 13 '16 at 06:03
  • This is the correct answer. Simply adding the `+` did the trick. Convert it to an answer and I'll accept it (: – Jon McClung Dec 13 '16 at 06:05
  • Cool. Actually, I'll just mark this as a duplicate of a very similar question. Thanks, though. Glad you got it working. Cheers! – Mike M. Dec 13 '16 at 06:18

1 Answers1

0

Change the code to

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lerenard.test.MainActivity">

<include
    android:id="@+id/hello_layout"
    layout="@layout/hello_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/bonjour_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/bonjour"
    android:textSize="@dimen/text_size"
    android:layout_below="@id/hello_layout"/>
</LinearLayout>
noobEinstien
  • 3,147
  • 4
  • 24
  • 42
  • As I stated in my question, I'm aware that the problem can be resolved by changing the `` tag to some other `Layout`. I want to know how to do it with a `` tag. Also, you need a space between `RelativeLayout` and `xmlns:android="http://schemas.android.com/apk/res/android"` – Jon McClung Dec 13 '16 at 05:52
  • I just changed the code . Try changing parent layout – noobEinstien Dec 13 '16 at 05:56
  • My example is only a stripped-down version of what I'm actually trying to accomplish, where I really do need a `RelativeLayout`. Is it simply impossible to accomplish with a `RelativeLayout`? – Jon McClung Dec 13 '16 at 05:59
  • Actually, the concept of merge tag is to merge with the parent code . It is same as your hello view (inside the include layout) is paste on the parent layout. so how you achieve without changing a single line of code? – noobEinstien Dec 13 '16 at 06:02