1

I am trying to merge one layout (xyz.xml) into other layout(main.xml) using include tag. I want to hide/show the layout xyz.xml on click of a button in main.xml. So for that I provide the id to included view. But when I am accessing that in java file it is throwing null pointer exception. Following is my code of xyz.xml file:-

 <merge xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/tv_candidateName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/icon_candidate_name" android:drawablePadding="8dp" android:drawableStart="@drawable/icon_candidate_name" android:padding="8dp" android:text="Raju jain" android:textColor="@android:color/black" android:textSize="15sp" /> <TextView android:id="@+id/tv_candidateEmail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:drawableLeft="@drawable/icon_mail" android:drawablePadding="8dp" android:drawableStart="@drawable/icon_mail" android:paddingBottom="8dp" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="[email protected]" android:textColor="#a9a9a9" android:textSize="13sp" /> </merge> 

I am using the above layout in main.xml file like below:-

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#f7f7f7" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/tv_candidateInfoLbl" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/blue" android:drawableEnd="@drawable/icon_check" android:drawableRight="@drawable/icon_check" android:padding="8dp" android:text="Candidate Information" android:textColor="@android:color/white" android:textSize="15sp" /> <include android:id="@+id/view_candidateInfo" android:layout_width="match_parent" android:layout_height="wrap_content" layout="@layout/include_interview_candidate_info" /> </LinearLayout> </ScrollView> 

In MainActivity.java file , when I am trying to find Id of include tag , I am getting null pointer exception.

 View view_candidateInfo= (View)findViewById(R.id.view_candidateInfo); view_candidateInfo.setVisibility(View.GONE); 
6
  • A common mistake is that there is a specific layout for the device your are using (based on some resourrce like screen size or orientation). But I am not sure the include still exist at that point, you might need to put this into a component to hide it Commented Feb 2, 2017 at 9:34
  • No use of merge layout here. you can use LinearLayout Commented Feb 2, 2017 at 9:34
  • In this answer you can see this doesn't work with merge Commented Feb 2, 2017 at 9:38
  • @arjun_sna I know that same can be achieved using the LinearLayout and it will work fine. But I want to know what is the problem with above code snippet Commented Feb 2, 2017 at 9:38
  • There is no View with ID view_candidateInfo after inflation. The <merge> tags cause those Views to be added directly to the parent of the <include>. If the xyz layout had an actual root View, then the <include> ID would override the root View's ID, but that's not the case here. xyz has no root View. Commented Feb 2, 2017 at 9:39

2 Answers 2

2

There is an interesting explanation in the android doc explaining that the merge is to prevent redundant LinearLayout so the contain replace the include tag.

From Use the <merge> Tag

Now, when you include this layout in another layout (using the tag), the system ignores the element and places the two buttons directly in the layout, in place of the tag.

So your tag include don't exist in the view, it is replaced by the component (without the merge tag) of the included layout.

You can either include this into a View that will have an ID or remove the merge

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

Comments

0

No need to use Merge tag. Simply use a Linear Layout for xyz.xml

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_candidateName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/icon_candidate_name" android:drawablePadding="8dp" android:drawableStart="@drawable/icon_candidate_name" android:padding="8dp" android:text="Raju jain" android:textColor="@android:color/black" android:textSize="15sp" /> <TextView android:id="@+id/tv_candidateEmail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:drawableLeft="@drawable/icon_mail" android:drawablePadding="8dp" android:drawableStart="@drawable/icon_mail" android:paddingBottom="8dp" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="[email protected]" android:textColor="#a9a9a9" android:textSize="13sp" /> </LinaerLayout> 

Then include this layout in your main.xml as;

<include android:id="@+id/layout_xyz" layout="@layout/xyz" android:visibility="visible"/> 

You can programatically set this layout visible/invisible.

LinearLayout xyzLayout = (LinearLayout) findViewByid(R.id.layout_xyz); xyzLayout.setVisibility(View.VISIBLE); 

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.