Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top