1

I want to add a scrollView to my relative layout without changing main relative layout.

I've tried to add a scrollView in different places of my code in xml but it does not work, also the position of the elements becomes mixed up.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/green"
tools:context="com.example.ahmedashrafhamza.GP.MainActivity">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/ll_send_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/editMessage"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="5"
        android:background="@drawable/edit_text_bubble"
        android:textColor="#ffff"
        android:gravity="top|left"
        android:inputType="textMultiLine|textCapSentences"
        android:maxLines="5"
        android:minLines="3"
        android:scrollHorizontally="false"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingTop="10dp"



        />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"

        android:gravity="center"
        android:text="Send"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

Can somebody assist?

Maharkus
  • 2,841
  • 21
  • 35
A.Hamza
  • 11
  • 7

2 Answers2

0

Make the scrollView the parent view, then have the RelativeLayout as a child view. It will work. Remember that the scrollView does not support sibling views.

EDIT The question has been answered here

Kingsley Ijike
  • 1,389
  • 1
  • 8
  • 13
0
  1. Right click on "layout"-> New -> Layout Resource File
  2. enter file name as sample
  3. type root element as ScrollView
  4. link this layout file to any activity.

And in layout file :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" >

        <TextView
        android:id="@+id/Heading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="TITLE"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/colorPrimaryDark" />


        <View
        android:id="@+id/Separator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        android:layout_below="@id/Heading"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="20dp" />


        <TextView
        android:id="@+id/ParaOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Separator"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="20dp"
        android:gravity="left"
        android:text="1)  fvfvfvfvfvfvfv fvfvfvfvfvfvfvfvvfvfv vfvfvfvfvfvfvfvfvfv "
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/colorPrimaryDark" />


        <TextView
        android:id="@+id/ParaTwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ParaOne"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="60dp"
        android:gravity="left"
        android:text="2) fvvfvfvfv fvfvfvfvfvfvfv fvfvfvfvfvfvfvfvvfvfv "
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/colorPrimaryDark" />


        <TextView
        android:id="@+id/ParaThree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ParaTwo"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="60dp"
        android:gravity="left"
        android:text="3) fvvfvfvfv fvfvfvfvfvfvfv fvfvfvfvfvfvfvfvvfvfv "
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/colorPrimaryDark" />

        <TextView
        android:id="@+id/ParaFour"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ParaThree"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="60dp"
        android:gravity="left"
        android:text="4) fvvfvfvfv fvfvfvfvfvfvfv fvfvfvfvfvfvfvfvvfvfv "
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/colorPrimaryDark" />

</RelativeLayout>

</ScrollView>
  1. Goto text mode and copy below relative layout as it is
  2. Goto Design mode and you should see scrolling relativelayout if you scroll the mouse over layout
  3. Or you can copy-paste whole code as it is in your existing layout file just for checking how scroll works
  4. But you have to unlink any java class or code associated with that layout just for testing

Try it and let me know.

sandhya sasane
  • 1,334
  • 12
  • 19