1

I'm answering my own question because even though I figured it out, I wasn't able to get a clear solution. This may help someone else.

I know the exception I receive is a common one, but I couldn't solve my problem with the following pages:

The specified child already has a parent. You must call removeView() on the child's parent first
java.lang.IllegalStateException: The specified child already has a parent
Call removeView() on the child's parent first
http://www.phonesdevelopers.com/1716777/

What I want to do it add a bunch of TextViews to a ScrollView, and then put the ScrollView on the screen. The problem is that the ScrollView is only part of the screen, so I can't just use setContentView(scrollView) because that would replace the buttons and things I already have.

I have an XML file that contains the template for my layout, and I simply want to replace the ScrollView with the one I generated in my Activity.

Here is the layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mostParent" >

    <RelativeLayout
        android:id="@+id/loadingPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true" />
    </RelativeLayout>

    <Button
        android:id="@+id/btnLogout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/logout" />

    <ImageButton
        android:id="@+id/refresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_action_refresh" />

    <ToggleButton
        android:id="@+id/lockToggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text=""
        android:textOff="@string/toggle_off"
        android:textOn="@string/toggle_on" />

    <TextView
        android:id="@+id/loggedInAs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/refresh"
        android:layout_centerHorizontal="true"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ScrollView
        android:id="@+id/postsView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/btnLogout"
        android:layout_marginTop="30dp"
        android:scrollbarSize="3dip" />

</RelativeLayout>

The ScrollView at the bottom is the one I want to add the TextViews to and it has the id postsView.

So I'm going to create a LinearLayout, add the TextViews to that layout, and then add that layout to the ScrollView. Like this:

ScrollView postsView = (ScrollView)findViewById(R.id.postsView);

LinearLayout layout = new LinearLayout(MyActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);

for (final TextView post : posts) {
    // add post to layout
    layout.addView(post);
}

postsView.addView(layout);

But with this, I get the LogCat:

java.lang.IllegalStateException: ScrollView can host only one direct child

How can I remove the view from the ScrollView?

Community
  • 1
  • 1
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • so do you just want to add scrollview to xml, is that all?? – Dushyant Patel Jun 08 '14 at 20:03
  • @DushyantPatel Yes. The reason it was difficult was because I was adding the layout I created to *two* ScrollViews (for testing purposes), so I was getting an unexpected error. Then I found out the only real problem was that I was adding too many views and not removing them each time. Problem solved, I answered my own question. – Michael Yaworski Jun 08 '14 at 20:08

1 Answers1

0

You can use removeAllViews() on the ScrollView. This is happening because whenever the code is being run more than once, more than one view is being added to the ScrollView.

Your code would change this:

postsView.addView(layout);

To:

postsView.removeAllViews()
postsView.addView(layout);
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97