How to specify id when uses include in layout xml file

AndroidAndroid Layout

Android Problem Overview

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( and findViewById( both returns null.

Can anyone please help me with my problem ?

Android Solutions

Solution 1 - Android

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(;
TextView test1TextView = (TextView) test1View.findViewById(;

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

Solution 2 - Android

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( should in fact be the <TextView> itself.

Solution 3 - Android

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

Solution 4 - Android

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

Becomes this:

// activity_main.xml

Solution 5 - Android

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(;

Solution 6 - Android

  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:

Solution 7 - Android

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


Solution 8 - Android

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) {
			View include_1 = view.findViewById(;
            mTextView = (TextView) include_1.findViewById(;

Solution 9 - Android

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



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

<?xml version="1.0" encoding="utf-8"?>


Solution 10 - Android

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



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.

Solution 11 - Android

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.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhap497View Question on Stackoverflow
Solution 1 - AndroidRon RomeroView Answer on Stackoverflow
Solution 2 - Androidloki19View Answer on Stackoverflow
Solution 3 - AndroidDaniel YankowskyView Answer on Stackoverflow
Solution 4 - AndroidAndre RomanoView Answer on Stackoverflow
Solution 5 - AndroidjokernkView Answer on Stackoverflow
Solution 6 - AndroidThe GoatView Answer on Stackoverflow
Solution 7 - AndroidPrakashView Answer on Stackoverflow
Solution 8 - AndroidShanki BansalView Answer on Stackoverflow
Solution 9 - AndroidUpendra ShahView Answer on Stackoverflow
Solution 10 - Androidmossman252View Answer on Stackoverflow
Solution 11 - AndroidTheHebrewHammerView Answer on Stackoverflow