-1

I have this include

enter image description here

I have to "include" this "include" two times, in the same layout.

enter image description here

How can I get the TextView inside RL1? I thought I could do this:

RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.RL1);
TextView textView = (TextView)layout1.findViewById(R.id.textViewId);

but this way seems that I get the same Instance of the TextView, both from RL1 and RL2.

This is the full layout:

<LinearLayout>

    <RelativeLayout          
        android:id="@+id/RL1">
        <include layout="@layout/game_area"/>
    </RelativeLayout>   


    <RelativeLayout         
        android:id="@+id/RL2">
        <include layout="@layout/game_area"/>
    </RelativeLayout>

</LinearLayout>

Thank you in advance

MDP
  • 4,177
  • 21
  • 63
  • 119

2 Answers2

0

When you search by id you always find the first items, so the second widgets are hidden.

However, it can be solved

<include> -- id1
-- stuff
</include>
<include> -- id2
-- stuff
</include>

So we can find the subelements, by first looking up id2 / id1.

View include_1 = findViewById(R.id.id1); 
View include_2 = findViewById(R.id.id2); 

and finally

include_2.findViewById(R.id.elementx );

Ref: https://stackoverflow.com/a/10821976/5707364

Community
  • 1
  • 1
Mayura Devani
  • 440
  • 3
  • 17
  • Excuse me. There was another error. The way I did works. Thank you and sorry for wasting your time :) – MDP Feb 07 '17 at 10:03
  • As per your question, you can get TextView inside RL1 by just doing this: and findViewById(R.id.id2).findViewById(R.id.textViewId ); – Mayura Devani Feb 07 '17 at 10:19
0
<LinearLayout>

<RelativeLayout>
    <include android:id="@+id/RL1" layout="@layout/game_area"/>
</RelativeLayout>   


<RelativeLayout>
    <include android:id="@+id/RL2" layout="@layout/game_area"/>
</RelativeLayout>

you shuold add id to the include tag

WenChao
  • 3,586
  • 6
  • 32
  • 46
  • Excuse me. There was another error. The way I did works. Thank you and sorry for wasting your time :) – MDP Feb 07 '17 at 10:04