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>
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.
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.
To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current 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.
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 RelativeLayout
s, 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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With