Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference the id of a view inside an "include" layout in xml

Tags:

android

xml

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>
like image 962
aysonje Avatar asked Jan 15 '14 02:01

aysonje


People also ask

How do you add a view in XML?

Android View Class Constructors To create a new View instance from Kotlin code, it needs the Activity context. To create a new View instance from XML. To create a new view instance from XML with a style from theme attribute. To create a new view instance from XML with a style from theme attribute and/or style resource.

What an XML layout file contains?

Specifically, Android considers XML-based layouts to be resources, and as such, layout files are stored in the reslayout directory inside your Android project. Each XML file contains a tree of elements specifying a layout of widgets and containers that make up one View.

What is the XML element for adding a layout defined in another layout file?

To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout.

What is XML layout?

XML-Based Layouts in Android In Android, an XML-based layout is a file that defines the different widgets to be used in the UI and the relations between those widgets and their containers. Android treats the layout files as resources. Hence the layouts are kept in the folder reslayout.


1 Answers

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.

like image 57
anddev84 Avatar answered Oct 28 '22 08:10

anddev84