129

In my layout xml file, I have included other layout xml file (each with a different android id).

<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 start Hierarchy Viewer, each of the layout still shows 'NO_ID', and in my code, I have findViewById(R.id.test1) and findViewById(R.id.test2) both returns null.

Can anyone please help me with my problem ?

1
  • 8
    Your IDs are missing the @ symbol. Commented Feb 8, 2017 at 18:33

12 Answers 12

313

Specify the ID in the <include>

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

Then use two findViewById to access fields in the layout

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

Using that approach, you can access any field in any include you have.

Sign up to request clarification or add additional context in comments.

6 Comments

This method return me null value for test1View object.
I don't really see the difference, can you explain?
I found out that if the layout that us included is using merge, this will not help. But in case where merge is not used, this works.
@Zlatko Yep, and this can't work with merge even in theory, because the result of including a merge is not a single view, but a bunch of them. So it's OK.
This will only work in case merge is not used. In case of merge it is not possible as specified here: code.google.com/p/android/issues/detail?id=36918#c3
|
66

I found out, that if you are using <merge> tag in your include layout, then the ID of include transfers to the merge tag which is not real view.

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

Tor Norbye wrote:

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

1 Comment

Good one. I remove merge tag and it start working but my question here is what is the usage of merge tag if any include layout working without it?
44

Romain Guy indicates that you can override the ID of an included layout by putting an android:id attribute inside the <include> tag.

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

3 Comments

This is correct. The way to reference the root element in the included layout file is by the id given in the 'include' tag (unless it is not given).
ID in <include> = root's ID in included layout
It appears to me if I set the id first (in include tag, it overrides the id from the included layout, right?), I will have the include tag to be initially '@+id/cell1' and then the layout='@layout/workspace_screen' will override the android:id again with the id of the included layout. @Ron Romero's answers makes more sense to me.
22

I think the top answer misses the most important point and might mislead people into thinking the <include/> tag creates a View that holds the include contents.

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

Meaning that this:

// 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 this:

// activity_main.xml <ImageView android:id="@+id/someid" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

Comments

4

yes is like this, but careful when the layout inserted in include field is a custom one and you want to access that root layout. That layout in this case @layout/test test, is actually returned in first line.

test test1View = (test)findViewById(R.id.test1); 

Comments

2
  1. you must set id each include tag
  2. included child element set a new id. if you look how to generate new id, look at this entry: https://stackoverflow.com/a/15442898/1136117

Comments

2

Problem is we try to use id which is not declared in current layout file. Instead of declaring again, id can be simply referred using @+id/. If you refactor original id name through Android Studio it does refactor in included layout as well.

<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"/> 

Comments

2

In a case of using <RecyclerView> find the id of <include> by using an instance of inflated view or else 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); } } 

Comments

1

If you have set id to either root tag of included layout then you can use that id or you can set id to included layout.

But you can not set id to both it may throw exception.

<include layout="@layout/view_contact_name" android:id="+id/test1"/> <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> .... </LinearLayout> 

Or

<include layout="@layout/view_contact_name"/> <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/llBottomMainView" android:layout_width="match_parent" android:layout_height="wrap_content"> .... </LinearLayout> 

Comments

0

When talking about include you either have an id on the root view inside the included layout file or on the include line itself and not on 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 wrong because technically you have two id's declared for the same layout. So what you have to do is pick which element will have the id.

Comments

0

To specify the id when you are including a xml file is like setting it to any xml element

Example:

*list_layout.xml* `<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvNames" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>` *activity_main.xml* `<RelativeLayout tools:context=".MainActivity"> <include layout="@layout/list_layout" android:id="@+id/myList" /> </RelativeLayout>` 

Now if you want to get that to use in .kt file, just use normally findViewById

Exemplo

*MainActivity.kt* `val myList: RecycleView = findViewById(R.id.myList)` 

Comments

-2

Wow, I can't believe this question doesn't have the right answer yet. It's simple tags suck. You can only change things that start with android:layout_ which android:id doesn't match. So the answer is you can't. Sorry. What you can do instead is create a class that will be a ViewGroup which will inflate the included views inside, then add that as a tag in your layout, but that's about it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.