Question

I have implemented Dynamic TextViews. I am able to view my dynamically generated textview. however, I need to implement a scrollview:

1.Using Code only.

Kindly help.
How do i achieve these 2 functionality? The code below works just fine(it gets all the textview and displays in the screen dynamically but without the scrolling functionality)

TextView[] textViewArray = new TextView[iterator];

            for( int i = 0; i < iterator; i++) {
                   textViewArray[i] = new TextView(narutoLinksOnly.this);
                   textViewArray[i].setText(narutoLinkHeadingName[i]);
                   textViewArray[i].setId(i); 
                   textViewArray[i].setTextColor(0xff000000);
                   textViewArray[i].setTextSize(20);
                   textViewArray[i].setOnClickListener(this);

                   textViewArray[i].setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));//suggested
                   //textViewArray[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

                   ((LinearLayout) linearLayout).addView(textViewArray[i]);

                }

InSide Oncreate:

linearLayout =  findViewById(R.id.dynamicTextview1);

XML Code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dynamicTextview1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/Ivory"
    android:orientation="vertical" >



</LinearLayout>
Was it helpful?

Solution

I tried it again in my editor with some different values and names, but concept is same as you.

My Activity class i.e MainActivity.java:

  public class MainActivity extends Activity implements OnClickListener {
  ScrollView scrollView;
  LinearLayout linearLayout;
  String[] narutoLinkHeadingName = { "abcv", "bvvvv", "cvvvv", "dvvvv",
        "avvvv", "bvvvv", "cvvvv", "d", "a", "b", "c", "d", "a", "b", "c",
        "d", "avvvv", "b", "c", "d", "a", "vvvb", "c", "vvvvd", "a",
        "vvvb", "cvvvv", "vvvvd" };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    linearLayout = (LinearLayout) findViewById(R.id.dynamicTextview1);

    scrollView = new ScrollView(MainActivity.this);
    scrollView.setBackgroundColor(Color.BLUE);

    TextView[] textViewArray = new TextView[narutoLinkHeadingName.length];

    for (int i = 0; i < narutoLinkHeadingName.length; i++) {
        textViewArray[i] = new TextView(MainActivity.this);
        textViewArray[i].setText(narutoLinkHeadingName[i]);
        textViewArray[i].setId(i);
        textViewArray[i].setTextColor(0xff000000);
        textViewArray[i].setTextSize(20);
        textViewArray[i].setOnClickListener(this);
        textViewArray[i].setLayoutParams(new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// suggested

        ((LinearLayout) linearLayout).addView(textViewArray[i]);
    }
    if(((ViewGroup)linearLayout.getParent()) != null){
        ((ViewGroup)linearLayout.getParent()).removeView(linearLayout);

        scrollView.addView(linearLayout);
        addContentView(scrollView, new LayoutParams(LayoutParams.MATCH_PARENT, 
                LayoutParams.MATCH_PARENT)); 
    }else{
        scrollView.addView(linearLayout);
        addContentView(scrollView, new LayoutParams(LayoutParams.MATCH_PARENT, 
                LayoutParams.MATCH_PARENT)); 
    }

}

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub

  }

  }

My layout i.e activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dynamicTextview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff"
android:orientation="vertical" >
</LinearLayout>

Now its perfectly scrolling vertically and for horizontal scrolling you can use the HorizontalScrollView in developer site.

NOTE: We have to take care of removeView() method otherwise it may give IllegalStateException like The specified child already has a parent. You must call removeView() on the child's parent first

OTHER TIPS

just put your linearlayout inside scrollview in xml.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true" >
<LinearLayout 
    android:id="@+id/dynamicTextview1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/Ivory"
    android:orientation="vertical" >



</LinearLayout>
</ScrollView>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top