2

I'm trying to create an android app with RelativeLayout inside a scroll view which has a TextView with scrollbar but every time I try to scroll the TextView, RelativeLayout gets scrolled.

I've also tried following solutions but nothing works:

  1. https://www.viralandroid.com/2015/10/how-to-make-scrollable-textview-in-android.html

  2. how to make a relative layout scrollable when it has many children views?

  3. RelativeLayout Scrollable

*My layout looks almost similar to 3rd link but with more controls.

Following is the code for my layout

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical">
    <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"
        tools:context=".MainActivity"
        android:scrollbars = "vertical">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="365dp"
            android:layout_height="20dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:text="@string/text1" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="365dp"
            android:layout_height="40dp"
            android:layout_below="@+id/textView1"
            android:layout_alignParentStart="true"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="5dp"
            android:inputType="text"
            android:text=""/>

        <!-- Other Controls, about 15 of them -->

        <TextView
            android:id="@+id/testLogs"
            android:layout_width="360dp"
            android:layout_height="150dp"
            android:layout_below="@+id/something"
            android:layout_alignParentStart="true"
            android:layout_marginStart="14dp"
            android:layout_marginTop="15dp"
            android:layout_marginEnd="14dp"
            android:background="@drawable/edit_text_background"
            android:textColor="@color/editTestBorderColor"
            android:enabled="true"
            android:gravity="bottom"
            android:hint="Process logs"
            android:scrollbars="vertical"
            android:textIsSelectable="true"
            android:focusable="true"
            android:longClickable="true"
            android:textSize="10dp" />

        <Button
            android:id="@+id/btnExecuteTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editTextLogs"
            android:layout_alignParentStart="true"
            android:layout_marginStart="67dp"
            android:layout_marginTop="10dp"
            android:text="@string/btnExecute" />

    </RelativeLayout>
</ScrollView>

Problem : Unable to scroll Textview (android:id="@+id/testLogs")

Somehow if I have less controls(as long as layout's scrollable is not activated) then TextView's scroll is working.

Following are the device specs. that I'm using for testing :

  1. Name : Huawei P10 lite
  2. OS : Android 8.0

I have no idea why TextView scroll si not working? Help Please!

JustAProgrammer
  • 436
  • 2
  • 6
  • 18

2 Answers2

3

try this

in XML file

android:maxLines = "AN_INTEGER"

android:scrollbars = "vertical"

and in java

  tv_desc.setMovementMethod(new ScrollingMovementMethod());


  scrollview.setOnTouchListener(new OnTouchListener() { 

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        tv_desc.getParent().requestDisallowInterceptTouchEvent(false);

        return false;
    }
});

 tv_desc.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        tv_desc.getParent().requestDisallowInterceptTouchEvent(true);

        return false;
    }
});
Sohel S9
  • 193
  • 12
0

Please do not mind for coding structure... Thank you.

in your layout:

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:text="Enter SSID" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="365dp"
            android:layout_height="40dp"
            android:layout_below="@+id/textView1"
            android:layout_alignParentStart="true"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="5dp"
            android:inputType="text"
            android:text="" />

        <!-- Other Controls, about 15 of them -->
        <androidx.core.widget.NestedScrollView
            android:id="@+id/scroll1"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/editText1"
            android:layout_marginTop="50dp"
            android:fillViewport="false"
            android:isScrollContainer="false">

            <TextView
                android:id="@+id/testLogs"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_below="@+id/editText1"
                android:layout_alignParentStart="true"
                android:background="@color/colorPrimary"
                android:enabled="true"
                android:focusable="true"
                android:hint="Process logs"
                android:longClickable="true"
                android:scrollbars="vertical"
                android:text="@string/sample_text"
                android:textColor="@color/WindowBackground"
                android:textIsSelectable="true"
                android:textSize="14sp" />
        </androidx.core.widget.NestedScrollView>

        <Button
            android:id="@+id/btnExecuteTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/scroll1"
            android:layout_alignParentStart="true"
            android:layout_marginStart="134dp"
            android:layout_marginTop="10dp"
            android:text="Execute Test" />


        <!--This is Additional code bellow -->
        <!-- if you want to view multiple TextView with scrollable -->


        <androidx.core.widget.NestedScrollView
            android:id="@+id/scroll2"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/btnExecuteTest"
            android:layout_marginTop="50dp"
            android:fillViewport="false"
            android:isScrollContainer="false">


            <TextView
                android:id="@+id/textView99"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_below="@+id/btnExecuteTest"
                android:text="@string/sample_text" />
        </androidx.core.widget.NestedScrollView>

        <androidx.core.widget.NestedScrollView
            android:id="@+id/scroll3"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/scroll2"
            android:layout_marginTop="50dp"
            android:fillViewport="false"
            android:isScrollContainer="false">

            <TextView
                android:id="@+id/textView100"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_below="@+id/textView99"
                android:text="@string/sample_text" />
        </androidx.core.widget.NestedScrollView>

    </RelativeLayout>
</ScrollView>

in you activity:

OnCreate():

    TextView textView = findViewById( R.id.testLogs );
    TextView textView1 = findViewById( R.id.textView99 );
    TextView textView2 = findViewById( R.id.textView100 );
    textView.setMovementMethod( new ScrollingMovementMethod( ) );
    textView1.setMovementMethod( new ScrollingMovementMethod( ) );
    textView2.setMovementMethod( new ScrollingMovementMethod( ) );

Preview:

Picture 1:

enter image description here

Picture 2:

enter image description here

Vignesh
  • 276
  • 1
  • 3
  • 15