How to specify an identifier when use includes a layout file in the XML

In my layout XML file, I included a different layout XML file (each with a different android identifier).

<include layout="@layout/view_contact_name" android:id="+id/test1"/> <include layout="@layout/view_contact_name" android:id="+id/test2"/> 

But when I run it in the emulator and run the Hierarchy Viewer, each of the layout shows "NO_ID", and in my code I have findViewById(R.id.test1) and findViewById(R.id.test2) both return null.

Can anyone help me with my problem?

+109
android android-layout
Nov 18 '09 at 21:07
source share
10 answers

Specify an identifier in <include>

 <include layout="@layout/test" android:id="@+id/test1" /> 

Then use two findViewById to access the fields in the layout

 View test1View = findViewById(R.id.test1); TextView test1TextView = (TextView) test1View.findViewById(R.id.text); 

Using this approach, you can access any field in any of the available to you.

+274
Sep 22 '10 at 17:36
source share

I found out that if you use the <merge> in your include layout, then the identifier includes transfers of the merge tag, which is not a real view.

So, either remove the merge, or replace it with some kind of layout.

Tor Norbye wrote :

The <include> not a real view, so findByView will not find it. The @id attribute (and any other attributes that you set in the include tag) is then applied to the root tag of the included layout. So your activity.getView (R.id.included1) should actually be <TextView> .

+59
Jan 13 '14 at 8:57
source share

Romain Guy indicates that you can override the identifier of the included layout by placing the android:id attribute inside the <include> .

 <include android:id="@+id/cell1" layout="@layout/workspace_screen" /> 
+33
Nov 19 '09 at 0:01
source share

I think that the main answer misses the most important point and can lead people astray by thinking that the <include/> creates a view containing the contents of include.

The key point is that the inclusion of id is passed to the root view of the include layout file.

This means that:

 // activity_main.xml <include layout="@layout/somelayout" android:id="@+id/someid"/> // somelayout.xml <?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

Becomes as follows:

 // activity_main.xml <ImageView android:id="@+id/someid" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
+11
Mar 14 '17 at 12:41
source share

yes, it is, but be careful when the layout inserted in the include field is customizable and you want to access this root layout. This layout in this case @ layout / test test is actually returned in the first line.

 test test1View = (test)findViewById(R.id.test1); 
+4
Feb 08 2018-12-12T00:
source share

The problem is that we are trying to use an id that is not declared in the current layout file. Instead of declaring id again, you can simply pass it using @+id/ . If you reorganize the original identifier name through Android Studio, it also refactors the included layout.

 <include layout="@layout/toolbar"/> <TextView android:id="@+id/txt_description" android:layout_width="match_parent" android:layout_height="wrap_content" **android:layout_below="@+id/toolbar"** android:layout_marginTop="16dp" android:paddingLeft="8dp" android:paddingRight="8dp"/> 
+2
Aug 12 '16 at 18:17
source share
  • you must set id each include tag
  • included child set a new identifier. if you look at how to create a new identifier, look at this entry: https://stackoverflow.com/a/2123/
+1
Oct. 13 '15 at 12:13
source share

If using <RecyclerView> find the <include> identifier using the bloated view instance, otherwise it will return null .

 public class ViewHolder extends RecyclerView.ViewHolder { private mTextView; public ViewHolder(View view) { super(view); View include_1 = view.findViewById(R.id.include_1); mTextView = (TextView) include_1.findViewById(R.id.text_id); } } 
+1
Jul 17 '17 at 14:34
source share

Speaking of inclusion, you either have an identifier in the root representation inside the included layout file, or in the inclusion string, and not both. For example:

 <include layout="@layout/layout1" android:id="@+id/layout1"/> 

Layout 1 file

 <RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layout2"> </RelativeLayout> 

The above example is incorrect, because technically you have two id declared for one layout. So, you need to choose which element will have the identifier.

0
Nov 23 '16 at 17:29
source share

compare

 <include layout="@layout/view_contact_name" android:id="+id/test1"/> <include layout="@layout/view_contact_name" android:id="+id/test2"/> 

from

 <include layout="@layout/view_contact_name" android:id="@+id/test1"/> <include layout="@layout/view_contact_name" android:id="@+id/test2"/> 

are R.id.test1 and R.id.test2 generated?

-one
Dec 05 '15 at 19:40
source share



All Articles