7

I have a button, A, inside special_button.xml, that I reuse on all my activities. Each activity has a root RelativeLayout.

The problem: one of my activities has a button, B, on the same position as A. I decided to just move B above A, but I can't reference A from the activity's xml.

Here are the xmls

special_button.xml

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

    <Button
        android:id="@+id/A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="20dp"/>

</merge>

layout_main.xml

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

    <include layout="@layout/special_button" />

    <Button
        android:id="@+id/B"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_above="<id to reference button A>"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="20dp" />

</RelativeLayout>
aysonje
  • 2,595
  • 1
  • 14
  • 16

2 Answers2

4

When you use the include tag, you should also specify a new id within that XML, which you can then reference as the id within the RelativeLayout. See the documentation and this sample code:

You can also override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the tag. For example:

<include android:id="@+id/news_title"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     layout="@layout/title"/>

EDIT: As @eskimoapps.com points out, there appears to be a known issue doing this with RelativeLayouts, as documented here, but when I run the following code, it is working as OP requests:

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

    <include layout="@layout/special_button"
        android:id="@+id/btn_a_ref"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
         />

    <Button
        android:id="@+id/B"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_above="@+id/A"     <!-- this still works, despite having warning in Eclipse -->
        android:layout_marginBottom="15dp"
        android:layout_marginRight="20dp" />

</RelativeLayout>

Here is the picture from HierarchyViewer showing the correct layout: id/A below id/B

enter image description here

My only thought is that Eclipse doesn't like it since it's statically trying to find the Button within the same layout file. Since <include> and <merge> work dynamically with the LayoutManager, that's probably while this still works as expected.

Please try and see if this works for you as well.

pensono
  • 336
  • 6
  • 17
anddev84
  • 1,473
  • 12
  • 30
  • I have tried this. The problem with this approach is that the id is given to the root of the included layout. In my case, it is the `merge` tag, which is actually the `RelativeLayout` of the activity. I still have no access to button A. – aysonje Jan 15 '14 at 02:25
  • Your statement is incorrect, if you give an id to the `` that is not the same as the id for the `RelativeLayout`. Please check my edit and see if that helps. – anddev84 Jan 15 '14 at 03:37
  • Wow. Thanks! I did not know that you could reference `A` directly. Just to let you know, I did not add any data to the `` tag, simply: `` and it worked. – aysonje Jan 15 '14 at 03:51
  • Thanks a lot! I did not know that the "id"(and other attributes) of include tag will override that of the root view on included layout. Your first paragraph opened my eyes :) – Vinay Vissh Mar 10 '17 at 11:42
0

You can add an id attribute to your <include> like this:

<include android:id="@+id/someId"
         layout="@layout/special_button"/>
pensono
  • 336
  • 6
  • 17
Emmanuel
  • 13,083
  • 4
  • 39
  • 53
  • I have tried this. The problem with this approach is that the id is given to the root of the included layout. In my case, it is the `merge` tag, which is actually the `RelativeLayout` of the activity. I still have no access to button A. – aysonje Jan 15 '14 at 02:26
  • You will not be able to directly reference button A from the XML with this method, but you can now refer to the new ID that you define within the ``. That will work in the `layout_above` value you mention. – anddev84 Jan 15 '14 at 02:29
  • This actually will not work if the merge tag is being used (id, visibility, and layout_* tags are ignored), and will also not work if layout_width and layout_height are not specified, see http://stackoverflow.com/questions/2316465/how-to-get-relativelayout-working-with-merge-and-include – eski Jan 15 '14 at 02:32
  • @anddev84: By using an id attached to the include, I will be referencing the `RelativeLayout` which covers the whole screen. I still do not have information on how I can place button B above button A. – aysonje Jan 15 '14 at 02:40
  • @eskimoapps.com: Thank you for the link. Although Macarse's approach is more neat, I don't think it will solve my problem. I will try using alienjazzcat's approach. – aysonje Jan 15 '14 at 02:42