Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Different Ids for the same include Layout

Tags:

android

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?

like image 342
Ahmad Avatar asked Jun 29 '12 16:06

Ahmad


People also ask

Is it possible to include one layout definition in another?

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.

Can one activity have multiple layouts?

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.

What is the purpose of the FindViewById () method?

FindViewById(Int32)Finds a view that was identified by the android:id XML attribute that was processed in #onCreate .

Which layout allows us to display items on the screen relative to each other?

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).


2 Answers

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);
like image 159
nEx.Software Avatar answered Oct 05 '22 01:10

nEx.Software


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.

like image 45
Angelo Avatar answered Oct 05 '22 01:10

Angelo