Adding multiple views of the same type

11,865

How are you trying to add the multiple copies to the 'root' LinearLayout?

If you're simply trying to call addView(newView) twice, then you're trying to add the same View object reference twice over. This is wrong because you're trying to add the same View object reference twice. I'm not entirely sure what the defined behaviour is when you do this, but I assume that addView() performs no action the second time because it checks that it already holds a reference to newView (would be grateful if anyone could confirm whether that's right or wrong).

So you need to inflate two separate instances of your child View I think, using say:

View newView0 = View.inflate(this, R.layout.alarm, null);
View newView1 = View.inflate(this, R.layout.alarm, null);

And then add them individually.

I think you'd then get around the problem of duplicate IDs by calling findViewById() on the actual child Views, as opposed to the parent:

newView0.findViewById( someID )

Update: Just tested the code in Eclipse for you. I added two child Views created from your XML file to a LinearLayout, and then changed a property (background colour to blue) of one of the Views within the second child View:

    LinearLayout root = new LinearLayout(this);
    LinearLayout newView0 = (LinearLayout)View.inflate(this, R.layout.main, null);
    LinearLayout newView1 = (LinearLayout)View.inflate(this, R.layout.main, null);
    root.addView(newView0);
    root.addView(newView1);
    setContentView(root);
    newView1.findViewById(R.id.view_monday).setBackgroundColor(0xff0000ff);
Share:
11,865
Atrus
Author by

Atrus

I'm a programmer/web developer/student. I attend the University of Toledo, and work full time for two different projects. In my spare time I work on my Pet Projects, including the RepRap project and our game design software suite, Fluxware.

Updated on June 04, 2022

Comments

  • Atrus
    Atrus about 2 years

    So, I have this nice little view that I've made, which basically shows two buttons with some status labels. Nothing too complicated.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical">
    
    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
        <ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@drawable/on_off">
        </ToggleButton>
    
        <TextView android:text="TextView" android:id="@+id/textView1"
            android:layout_height="wrap_content" android:layout_width="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_marginLeft="20px" android:layout_marginRight="20px"
            android:layout_marginTop="3dp" android:layout_marginBottom="3dp">
        </TextView>
        <ImageButton android:src="@drawable/preferences"
            android:layout_height="wrap_content" android:layout_width="wrap_content"
            android:id="@+id/imageButton2" android:background="@android:color/transparent">
        </ImageButton>
    </LinearLayout>
    
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView android:id="@+id/view_monday" android:textSize="10dp" android:layout_marginRight="3dp"
            android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
            android:text="@string/monday_short"></TextView>
        <TextView android:id="@+id/view_tuesday" android:textSize="10dp" android:layout_marginRight="3dp"
            android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
            android:text="@string/tuesday_short"></TextView>
        <TextView android:id="@+id/view_wednesday" android:textSize="10dp" android:layout_marginRight="3dp"
            android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
            android:text="@string/wednesday_short"></TextView>
        <TextView android:id="@+id/view_thursday" android:textSize="10dp" android:layout_marginRight="3dp"
            android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
            android:text="@string/thursday_short"></TextView>
        <TextView android:id="@+id/view_friday" android:textSize="10dp" android:layout_marginRight="3dp"
            android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
            android:text="@string/friday_short"></TextView>
        <TextView android:id="@+id/view_saturday" android:textSize="10dp" android:layout_marginRight="3dp"
            android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
            android:text="@string/saturday_short"></TextView>
        <TextView android:id="@+id/view_sunday" android:textSize="10dp" android:layout_marginRight="3dp"
            android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
            android:text="@string/sunday_short"></TextView>
    </LinearLayout>
    </LinearLayout>
    

    And I want to add it to my main activity with the following code:

    LinearLayout root = (LinearLayout)findViewById(R.id.alarms);
    View newView = View.inflate(this, R.layout.alarm, null);
    alarms.add(newView);
    

    However, it seems as if I can't add more than one of them, and I'm not sure why, or how to get around this problem to be able to add multiple copies. Furthermore, I don't know how to access individual parts, as they would all have the same id.

    Thanks, Tim

  • Andrew Anderson
    Andrew Anderson almost 13 years
    you are right. Checked it now. I must have confused it with something else. My mistake. You are correct. Deleting my answer now.
  • Andrew Anderson
    Andrew Anderson almost 13 years
    It is Scroll view that takes only one child. My bad
  • Atrus
    Atrus almost 13 years
    I didn't think of calling findViewById() on the actual child. Seems obvious in retrospect. Accepted.