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="xyz@gmail.com"
    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);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 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 – AxelH Feb 02 '17 at 09:34
  • No use of merge layout here. you can use `LinearLayout` – arjun Feb 02 '17 at 09:34
  • In this [answer](http://stackoverflow.com/a/21087173/4391450) you can see this doesn't work with `merge` – AxelH Feb 02 '17 at 09: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 – SherryChhabra51 Feb 02 '17 at 09:38
  • There is no `View` with ID `view_candidateInfo` after inflation. The `` tags cause those `View`s to be added directly to the parent of the ``. If the `xyz` layout had an actual root `View`, then the `` ID would override the root `View`'s ID, but that's not the case here. `xyz` has no root `View`. – Mike M. Feb 02 '17 at 09:39
  • why did people downvote a valid question? – Zulqurnain Jutt Apr 30 '18 at 05:55

2 Answers2

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

Extremis II
  • 5,185
  • 2
  • 19
  • 29
AxelH
  • 14,325
  • 2
  • 25
  • 55
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="xyz@gmail.com"
    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);
Nayan
  • 367
  • 4
  • 18