3

I want to achieve this functionality: dynamically add multiple button to the scrollview, if scrollview more than a certain height, it is will automatically diplay scroll bar.

can you give me some advice?

Kevin
  • 65
  • 1
  • 1
  • 4

5 Answers5

8

check the following code snippet:

// Find the ScrollView 
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);

// Create a LinearLayout element
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

// Add Buttons
Button button = new Button(this);
button.setText("Some text");
linearLayout.addView(button);

// Add the LinearLayout element to the ScrollView
scrollView.addView(linearLayout);

Quoted from How do I add elements dynamically to a view created with XML.

Community
  • 1
  • 1
Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
  • Thanks your advice. But i want to add more buttons by horizonital, And it will Automatically wrap if more than one line. Can you help me again? – Kevin Feb 18 '13 at 05:40
  • then why do you need a `ScrollView` if you want your item to wrap? – Ahmad Kayyali Feb 18 '13 at 05:47
  • thanks a lot. This is requirment. I have resolved this problme reference open source code from http://code.google.com/p/android-masonry/. I am a chinese fresh android developer, thank you for your help again. – Kevin Feb 19 '13 at 01:19
  • i am trying to add button dynamically in the process given above. but i am not able to view the button.can any one give any hint why its happening.. – user1859771 Jan 06 '14 at 07:59
  • LinearLayout.VERTICAL is what you want? try LinearLayout.HORIZONTAL – Ahmad Kayyali Jan 06 '14 at 09:37
2

Put for example a linearlayout in the scrollview, then add the buttons to the linearlayout. Problem solved.

Nicklas Gnejs Eriksson
  • 3,395
  • 2
  • 21
  • 19
1

It's an example for Xamarin C#.

<ContentPage.Content>
    <ScrollView x:Name="ScrollLogonID"                     
        BackgroundColor="Transparent"
        HorizontalOptions="FillAndExpand" Opacity="0.85">
        <StackLayout Opacity="0.85">
            <StackLayout 
                x:Name="LogoID" 
                BackgroundColor="Transparent"
                VerticalOptions="Start" 
                Opacity="0.65"
                HorizontalOptions="FillAndExpand">
                    <!--- place the button here -->
            </StackLayout> 
        </StackLayout> 
    </ScrollView>
</ContentPage.Content>
<!--
/*
C# Code snippet
*/-->
    Xamarin.Forms.Button btnEnter = new Xamarin.Forms.Button {
        Text = "Entrar",
        HorizontalOptions = LayoutOptions.FillAndExpand,                
        FontSize = 18,
        HeightRequest = 26,
        TextColor = Color.LightGray,
        BackgroundColor = Color.DarkRed
    };

    StackLayout stkButton = new StackLayout{ Children = { btnEnter }};
    this.FindByName<StackLayout>("LogonID").Children.Add(stkButton);

0

Since scrollview can host only one direct child. You can add a linearLayout to the ScrollView and programmatically add buttons to that LinearLayout

Ahmed Aeon Axan
  • 2,139
  • 1
  • 17
  • 30
0

Here is my example in layout file :

  <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/scrollView" >

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

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="120dp"
                    android:id="@+id/button4" />

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="120dp"
                    android:id="@+id/button" />

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="120dp"
                    android:id="@+id/button9" />

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="120dp"
                    android:id="@+id/button10" />
            </LinearLayout>
   </ScrollView>
Saeed Sharman
  • 765
  • 5
  • 24