2

I have this layout for a fragment that has to contain a scrollview and a button:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp" >

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <include
            android:id="@+id/row1"
            android:layout_width="fill_parent"
            layout="@layout/catalog_row" />

        <View
            android:layout_width="match_parent"
            android:layout_height="20dip"
            android:alpha="0"
            android:background="#FFFFFF" />

        <include
            android:id="@+id/row2"
            android:layout_width="fill_parent"
            layout="@layout/catalog_row" />

        <View
            android:layout_width="match_parent"
            android:layout_height="20dip"
            android:alpha="0"
            android:background="#FFFFFF" />

        <include
            android:id="@+id/row3"
            android:layout_width="fill_parent"
            layout="@layout/catalog_row" />

        <View
            android:layout_width="match_parent"
            android:layout_height="20dip"
            android:alpha="0"
            android:background="#FFFFFF" />

        <include
            android:id="@+id/row4"
            android:layout_width="fill_parent"
            layout="@layout/catalog_row" />

        <View
            android:layout_width="match_parent"
            android:layout_height="20dip"
            android:alpha="0"
            android:background="#FFFFFF" />
    </LinearLayout>
</ScrollView>

<Button
    android:id="@+id/more_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="more"
    android:visibility="invisible" />

</RelativeLayout>

Now under some conditions (when button becomes visible, and user click on it) I would add inside scrollView other "catalog_row" layouts. How can I do it programmatically?

1
  • You have to Inflate your layout and add that layout in your Scrollview on Demand . Commented Apr 16, 2014 at 9:11

1 Answer 1

1

You have to give an ID to the LinearLayout inside the ScrollView, so in the code you will:

LinearLayout l = (LinearLayout) findViewById(R.id.yourID);

and then simply:

l.addView(yourView);

Remember that ScrollView can have only one child, so if you need another Layout you have to set up a parent Layout to contain the old and the new one.

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.