Kevin
Kevin

Reputation: 65

How to add more than one button to scrollview in android

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?

Upvotes: 3

Views: 17115

Answers (5)

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);

Upvotes: 1

Saeed Sharman
Saeed Sharman

Reputation: 785

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>

Upvotes: 0

Ahmad Kayyali
Ahmad Kayyali

Reputation: 8243

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.

Upvotes: 8

Nicklas Gnejs Eriksson
Nicklas Gnejs Eriksson

Reputation: 3415

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

Upvotes: 2

Ahmed Aeon Axan
Ahmed Aeon Axan

Reputation: 2139

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

Upvotes: 0

Related Questions