1

I just want to know if its possible or not? I am using include tag in a layout xml file of an activity

layout.xml

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:padding="5dp">

    <include android:id="@+id/error_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/error_xml"
      android:visibility="gone"/> --- at first keeping it invisible

<EditText 
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:paddingLeft="10dp"
       />
</LinearLayout>

erro_xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/error_linear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
     android:background="@drawable/error_bg"
     android:weightSum="1"
     android:layout_marginTop="10dp"
     android:visibility="gone" >

   <TextView android:id="@+id/error_txt"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:textSize="16sp"

     android:layout_weight="0.9"
     android:padding="10dp"/> 

   <ImageView android:id="@+id/error_cross"
    android:layout_width="0dp"
    android:layout_height="wrap_content"

    android:src="@drawable/squarecross"
     android:layout_weight="0.1"
   android:layout_marginTop="6dp"/>
</LinearLayout>

For some reasons I wish to display the included layout on some conditions that are processed dynamically in activity code

I know we can refer EditText and etc views by writing the below code:

EditText edit = (EditText)findViewById(R.id.edit);

Can I also refer the include tag layout like the above code? Is it possible? or do I have to inflate it? But how? I am too confused.

EDIT

As Answered BY MathanG -- when I used reference of linearlayout in fragment i.e.

LinearLayout error_layout = (LinearLayout)rootView.findViewById(R.id.error_layout);

the above statement worked when used in fragment.. But when I am trying to refer the same through activity it is giving me null pointer exception

sanedroid
  • 1,036
  • 4
  • 16
  • 27

2 Answers2

3

You can directly access the views inside the included layout. Include Tag will includes the entire layout inside the used layout dynamically. So you will not have any problem in directly accessing it. To hide and show the included layout you can use

error_layout.setVisiblity(View.GONE) 
error_layout.setVisiblity(View.VISIBLE) 

where error_layout is the type of root layout of included layout(error_xml).

MathanG
  • 1,195
  • 10
  • 22
  • No error_layout should be root layout of error_xml layout. You didnt post error_xml so i dont know what layout you used inside error_xml. – MathanG Nov 20 '14 at 07:13
  • Hey well this worked while I used include in fragments but this isnt working for activity when I am trying to refer include through reference of linear Layout.. I will edit the question with my include layout contents.. – sanedroid Nov 20 '14 at 07:41
  • use findviewById() for activity instead of rootView.findViewById() – MathanG Nov 20 '14 at 08:19
  • You are trying to access the view inside the fragment from the activity. then your question itself wrong. You should ask how to access the view inside the fragment from activity. – MathanG Nov 20 '14 at 09:08
  • Refer SMR Answer here http://stackoverflow.com/questions/24188050/how-to-access-fragments-child-views-inside-fragments-parent-activity. – MathanG Nov 20 '14 at 09:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65270/discussion-between-pooh1527-and-mathang). – sanedroid Nov 20 '14 at 09:11
1

Yes ofcourse you can access the views inside the layout as suggested by @MathanG, as even though the layout visibility is set to "gone" it does not mean that the layout is removed, it actually exist only the layout does not takes any space refer Android Developer setVisibility="gone"

  • I know I can access them.. BUt I want to know How? – sanedroid Nov 20 '14 at 07:42
  • For that I asked you to refer MathanG answer, I was helping you to understand the reason behind it,bcoz of which the view is accessible. – Tech_Intelliswift Nov 20 '14 at 08:16
  • Well thanks.. But MathanG's answer is also not working either.. May be I am doing something silly here.. But I tried referencing both ids using linearLayout(i.e error_layout and error_linear) in activity but this gives me nullpointerexception.. also MathanG has mentioned that I need to refer the id of the root layout which in this case is "error_linear" which is also giving me nullpointer while using in fragment but error_layout works with fragments.. – sanedroid Nov 20 '14 at 08:21
  • @pooh1527 if you want to access TextView or Imageview use directly their respective ids, and if you want to access the whole layout use the referral id you had given for the include tag. – Tech_Intelliswift Nov 20 '14 at 08:43
  • I did that but it isnt working.. I created a LinearLayout reference for id error_layout but it is giving nullpointer in activity but working in fragment.. Do I have to cast it to something other than LinearLayout? – sanedroid Nov 20 '14 at 09:10
  • Tried it also on activity its working fine on my side.Give me your mail-id – Tech_Intelliswift Nov 20 '14 at 09:49