1

I currently have a program that uses a linear layout that contains a Textview that is populated with x amount of values (dependent on arrayList size determined in separate activity) but Scrollview is not working as I anticipated.

Since Scrollview can only have one direct child, I have the Linear Layout with the TextView nested within; however when I wrap both of these within Scrollview, the Linear Layout is still not scrollable

//This is what I am using to populate the Linear Layout that I'm trying to make scrollable
TextView DisplayString = (TextView)findViewById(R.id.stringNumsCon1);
        LinearLayout LinContainer = (LinearLayout)findViewById(R.id.LinLay);

        Intent intent = getIntent();
        ArrayList<String> timerVals = (ArrayList<String>)getIntent().getSerializableExtra("timerVals");

        DisplayString.setTextSize(15);
        DisplayString.setTextColor(Color.BLACK);
        for(int i=0; i<timerVals.size(); i++){
            DisplayString.append(timerVals.get(i));
            DisplayString.append("\n");
        }

//This is how I am trying to make it scrollable within the xml
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/LinLay"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="391dp">
            <TextView
                android:id="@+id/stringNumsCon1"
                android:layout_width="match_parent"
                android:layout_height="391dp"
                android:orientation="vertical">
            </TextView>

        </LinearLayout>
    </ScrollView>

I expected this to make the Linear Layout portion scrollable, but I can see data is cut off and inaccessible when actually implementing this code

ousalia
  • 43
  • 6

1 Answers1

1

You won't need the ScrollView, neither the LinearLayout. Remove them from your layout. All you will need is set in your TextView in xml:

android:scrollbars = "vertical"

so your final xml layout would be:

<TextView
   android:id="@+id/stringNumsCon1"
   android:layout_width="match_parent"
   android:layout_height="391dp"
   android:orientation="vertical"
   android:scrollbars = "vertical" />

then set the movement method in onCreate of your activity/fragment as shown below:

Java

DisplayString.setMovementMethod(new ScrollingMovementMethod());

Kotlin

DisplayString.movementMethod = ScrollingMovementMethod()

P.S.1: You can find the original answer here
P.S.2: Consider to use camel case naming convention for your variable names. E.g. use displayString instead of DisplayString.