0

I want to add views which are created programmatically into a ScrollView and that ScrollView is declared in an XML file. How to achieve this?

package com.example.multipleviews;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class MainView extends Activity {

    LinearLayout main, child;
    ScrollView sc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_view);
        main = (LinearLayout) findViewById(R.id.main);
        // Toast.makeText(getApplicationContext(), "hello", 0).show();
        view();
    }

    private void view() {
        // TODO Auto-generated method stub
        main.removeAllViews();

        sc = (ScrollView) findViewById(R.id.sc);
        sc.removeAllViews();

        child = (LinearLayout) findViewById(R.id.lin);
        child.removeAllViews();

        for (int i = 0; i < 5; i++) {
            Button b = new Button(this);
            b.setText("Button "+i);
            b.setId(i);
            child=(LinearLayout)findViewById(R.id.lin);
            child.addView(b);

        }
        sc.addView(child);
        main.addView(sc);
        setContentView(main);

    }

}

here is my code.....but application does not runs.....

Cœur
  • 37,241
  • 25
  • 195
  • 267
Muhammad Husnain Tahir
  • 1,009
  • 1
  • 15
  • 28

4 Answers4

1

Step 1: Add any layout container in ScrollView(e.g linear layout )

<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   <LinearLayout android:id="@+id/resultContentHolder"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:gravity="center_horizontal|top" 
  android:layout_height="fill_parent" />
</ScrollView> 

Step 2:Add any custom view in layout container in activity clas for e.g

LinearLayout gapH = new LinearLayout(this);
            LayoutParams gapParamsH = new LayoutParams(LayoutParams.FILL_PARENT,10);
            gapH.setLayoutParams(gapParamsH);
            resultContentHolder.addView(gapH);   
Android
  • 8,995
  • 9
  • 67
  • 108
0

firs create a child layout in that you can create your dynamic view.You can refer this link for adding dynamic views.

DjP
  • 4,537
  • 2
  • 25
  • 34
0

Are you certain that you want to use a ScrollView for this? It sounds like you might be better served with a ListView.

In case a ListView is what you need, this tutorial on vogella.com is one of the best tutorials I know for how to use them.

scriptocalypse
  • 4,942
  • 2
  • 29
  • 41
0
// 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);

See this link

Community
  • 1
  • 1
Android
  • 8,995
  • 9
  • 67
  • 108