Search code examples
androidbuttonscrollview

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?


Solution

  • 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.