4

I want to create scrollview with the code,

I have created scrollview and linearlayout, but doesn't scroll to bottom,

What should I do for able to scrolling

you can find the screenshot following link http://screencast.com/t/bbDcDWoScPyM

Here is Activity xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/rlt"
                android:theme="@style/Theme.AppCompat.Light.NoActionBar"
                android:descendantFocusability="beforeDescendants"
                android:focusableInTouchMode="true"
                tools:context="com.test.test.test">
</RelativeLayout>

and here is the java code

RelativeLayout lL = (RelativeLayout) findViewById(R.id.rlt);

        ScrollView sv= new ScrollView(this);//(ScrollView) findViewById(R.id.svScroll);

        LinearLayout sahne = new LinearLayout(this);

        sahne.setOrientation(LinearLayout.VERTICAL);

        txt1.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt2.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt3.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt4.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt5.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt6.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt7.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));

        sahne.addView(txt1);
        sahne.addView(txt2);
        sahne.addView(txt3);
        sahne.addView(txt4);
        sahne.addView(txt5);
        sahne.addView(txt6);
        sahne.addView(txt7);
        sahne.addView(btn1);

        sv.addView(sahne);

        lL.addView(sv);
3
  • 1
    Have you tried setting the layout parameters on your ScrollView and LinearLayout? Commented Apr 16, 2015 at 14:20
  • Have you tried setting layout_width and layout_height for ScrollView sv and LinearLayout sahne? I believe these params are required. Commented Apr 16, 2015 at 14:23
  • yes but doesn't work Commented Apr 16, 2015 at 14:47

1 Answer 1

3

I change some order of your code and write the below demo. Let try it:

    RelativeLayout lL = (RelativeLayout) findViewById(R.id.rlt);

    ScrollView sv= new ScrollView(this);//(ScrollView) findViewById(R.id.svScroll);
    sv.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT,ScrollView.LayoutParams.MATCH_PARENT));
    lL.addView(sv);

    LinearLayout sahne = new LinearLayout(this);
    sahne.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
    sahne.setOrientation(LinearLayout.VERTICAL);
    sv.addView(sahne);
    TextView[] textViews = new TextView[20];
    for(int i = 0; i < 20; i++){
        textViews[i] = new TextView(this);
        textViews[i].setLines(2);
        textViews[i].setText("Bla bla bla bla");
        textViews[i].setGravity(Gravity.CENTER);
        textViews[i].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        sahne.addView(textViews[i], i, new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.