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);
includestill exist at that point, you might need to put this into a component to hide itLinearLayoutmergeViewwith IDview_candidateInfoafter inflation. The<merge>tags cause thoseViews to be added directly to the parent of the<include>. If thexyzlayout had an actual rootView, then the<include>ID would override the rootView's ID, but that's not the case here.xyzhas no rootView.