Question

I have following code problem is when i run this all text boxes are not shown on screen so i figured that I need to add all text boxes in a scrollview but I dont know how.Also I can do this using listview but i have to do it by adding scrollview programatically in java code help anybody

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

    String[] textArray={"one","two","asdasasdf", "asdf" ,"dsdaa","fsvs","sd"};
    int length=textArray.length;

    LinearLayout layout = new LinearLayout(this);
      setContentView(layout);

    layout.setOrientation(LinearLayout.VERTICAL);        
    for(int i=0;i<length;i++)
    {
        TextView tv=new TextView(getApplicationContext());
        tv.setText(textArray[i]);
        tv.setTextSize(40);
        tv.setTextColor(Color.BLACK);
        tv.setPadding(20, 50, 20, 50);
        tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
        layout.addView(tv);
        //tv.setMovementMethod(new ScrollingMovementMethod());


    }
         }
     }
Was it helpful?

Solution

// try this way,hope this will help you...

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] textArray={"one","two","asdasasdf", "asdf" ,"dsdaa","fsvs","sd"};
        int length=textArray.length;

        ScrollView scrollView = new ScrollView(this);
        LinearLayout layout = new LinearLayout(this);

        layout.setOrientation(LinearLayout.VERTICAL);
        for(int i=0;i<length;i++)
        {
            TextView tv=new TextView(getApplicationContext());
            tv.setText(textArray[i]);
            tv.setTextSize(40);
            tv.setTextColor(Color.WHITE);
            tv.setPadding(20, 50, 20, 50);
            tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
            layout.addView(tv);
            //tv.setMovementMethod(new ScrollingMovementMethod());


        }
        scrollView.addView(layout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        setContentView(scrollView);

}

OTHER TIPS

Define one LinearLayout under ScrollView in xml file. Give id field to LinearLayout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/<ID> >
    </LinearLayout>
</ScrollView>

Map LinearLayout in your class:

LinearLayout layout = (LinearLayout) findViewById(R.id.<ID>);

Add TextBox dynamically here:

TextView textBox = new TextView(context);
layout.addView(textBox);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top