Vishal Khemka
Vishal Khemka

Reputation: 29

Accessing nested layouts in Android

I'm having 3 nested layouts and I'm facing difficulty in accessing the views in them. Main xml(A.xml) below, includes a single instance of B, which has mutiple includes of C.xml. There are 3 images in C.xml

Say

A.xml -> This is the main xml

  <include
        android:id="@+id/b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/B" />

B.xml -> This is level 2

 <include
        android:id="@+id/c1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/C" />

    <include
        android:id="@+id/c2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/C" />

    <include
        android:id="@+id/c3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/C" />

C.xml - this is level 3

<ImageView
        android:id="@+id/a1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img1" />

    <ImageView
        android:id="@+id/a2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img2" />

    <ImageView
        android:id="@+id/a3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img3" />

So here, main includes one instance on B, which in turn includes multiple instance of C. So if I want to access from B the id C3 and in C3 I want to make a2 visible, how should I go about it.

Upvotes: 2

Views: 2622

Answers (2)

Adil Hussain
Adil Hussain

Reputation: 32113

I have a nested layout structure similar to that described in the question and am not encountering problems getting views nested one or two levels from my main view. I set my Activity's content view to be a.xml and then find the views I need direct from my Activity (without having to find third level views from second level views), as follows:

@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.a);
  ImageView a1 = (ImageView)findViewById(R.id.a1);
}

Upvotes: 0

Animesh Sinha
Animesh Sinha

Reputation: 1045

You can Access all views by getting the reference of each views and sub-views as shown below as your example.

        View view = findViewById(R.id.b);
        view = view.findViewById(R.id.c1);
        ((TextView)view.findViewById(R.id.txt1)).setText("Got the First one");

        view = findViewById(R.id.b);
        view = view.findViewById(R.id.c1);
        ((TextView)view.findViewById(R.id.txt2)).setText("Got the Second one");

        view = findViewById(R.id.b);
        view = view.findViewById(R.id.c2);
        ((TextView)view.findViewById(R.id.txt1)).setText("Got the forth one");

similarly you can access other elements also.

Upvotes: 3

Related Questions