I have a layout which I have to Include several times. It consists of a TextView
and an ImageView
:
<ImageView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="@drawable/back2"
android:id="@+id/id_1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_2"
android:textSize="15dp"
android:typeface="sans"
android:textColor="#ffffff" />
Now I want to set the text programmatically, but the problem that I'm facing is, that the TextView
now always has the same Id, because I'm including the same Layout several times. Is there a way to programmatically include a Layout and always change the Id for each included layout?
To efficiently reuse complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.
You can use as many layouts as possible for a single activity but obviously not simultaneously. You can use something like: if (Case_A) setContentView(R. layout.
FindViewById(Int32)Finds a view that was identified by the android:id XML attribute that was processed in #onCreate .
RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).
What I'd do is, when you need to access the views on a particular instance of the included layout:
ViewGroup instance = (ViewGroup) findViewById(R.id.included1); // Replace ViewGroup with whatever your particlar type is
ImageView iView = (ImageView) instance.findViewById(R.id.id_1);
TextView tView = (TextView) instance.findViewById(R.id.id_2);
You can create your TextView dynamically and then use TextView.setId(int id)
to set the id of that View so that you can call it later with the new id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With