I am trying to make my activity screen where there is a form to be filled scrollable but unfortunately, when I tried it crashed the app. I have the following xml for that activity:
<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:background="@color/background"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:textDirection="anyRtl"
tools:context=".AddDiscountActivity" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="19dp"
android:text="@string/discountLocationLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<EditText
android:id="@+id/shopLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignRight="@+id/shopCity"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView5"
android:layout_below="@+id/shopDiscount"
android:layout_marginTop="21dp"
android:text="@string/discountDurationLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<EditText
android:id="@+id/shopDiscountDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView6"
android:layout_alignLeft="@+id/shopDiscount"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/shopDiscount"
android:layout_alignParentRight="true"
android:text="@string/discountRateLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView6"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:background="@drawable/roundbutton"
android:text="@string/discountPublishButtonLabel"
android:textColor="@color/white"
android:onClick="SubmitData"
android:width="250dp" />
<EditText
android:id="@+id/shopCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_alignRight="@+id/shopName"
android:ems="10"
android:inputType="text" >
</EditText>
<EditText
android:id="@+id/shopDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/shopLocation"
android:layout_below="@+id/shopLocation"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/shopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_marginLeft="14dp"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/categoriesSpinner"
android:layout_alignRight="@+id/textView2"
android:text="@string/discountCategoryLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<Spinner
android:id="@+id/categoriesSpinner"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_above="@+id/shopName"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView2"
android:layout_marginTop="206dp"
android:text="@string/discountCityLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/shopCity"
android:layout_alignLeft="@+id/textView5"
android:layout_marginBottom="18dp"
android:text="@string/discountShopLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginTop="14dp"
android:gravity="right"
android:text="@string/addDiscountMsg"
android:textAlignment="viewEnd"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textDirection="anyRtl" />
</RelativeLayout>
And when I tried to add the following after defining RelativeLayout and before closing RelativeLayout, it broke the app immediately when I run it.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
...........
</ScrollView>
How can I make it scrollable?
Your app crashes because ScrollView can host only one direct child. Direct from official documentation:
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
Look this http://developer.android.com/reference/android/widget/ScrollView.html.
To solve your 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:background="@color/background"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:textDirection="anyRtl"
tools:context=".AddDiscountActivity" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="19dp"
android:text="@string/discountLocationLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<EditText
android:id="@+id/shopLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignRight="@+id/shopCity"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView5"
android:layout_below="@+id/shopDiscount"
android:layout_marginTop="21dp"
android:text="@string/discountDurationLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<EditText
android:id="@+id/shopDiscountDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView6"
android:layout_alignLeft="@+id/shopDiscount"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/shopDiscount"
android:layout_alignParentRight="true"
android:text="@string/discountRateLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView6"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:background="@drawable/roundbutton"
android:text="@string/discountPublishButtonLabel"
android:textColor="@color/white"
android:onClick="SubmitData"
android:width="250dp" />
<EditText
android:id="@+id/shopCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_alignRight="@+id/shopName"
android:ems="10"
android:inputType="text" />
<EditText
android:id="@+id/shopDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/shopLocation"
android:layout_below="@+id/shopLocation"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/shopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_marginLeft="14dp"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/categoriesSpinner"
android:layout_alignRight="@+id/textView2"
android:text="@string/discountCategoryLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<Spinner
android:id="@+id/categoriesSpinner"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_above="@+id/shopName"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView2"
android:layout_marginTop="206dp"
android:text="@string/discountCityLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/shopCity"
android:layout_alignLeft="@+id/textView5"
android:layout_marginBottom="18dp"
android:text="@string/discountShopLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginTop="14dp"
android:gravity="right"
android:text="@string/addDiscountMsg"
android:textAlignment="viewEnd"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textDirection="anyRtl" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Do it like this , it will work :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:textDirection="anyRtl"
tools:context=".AddDiscountActivity" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="19dp"
android:text="@string/discountLocationLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<EditText
android:id="@+id/shopLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignRight="@+id/shopCity"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView5"
android:layout_below="@+id/shopDiscount"
android:layout_marginTop="21dp"
android:text="@string/discountDurationLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<EditText
android:id="@+id/shopDiscountDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView6"
android:layout_alignLeft="@+id/shopDiscount"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/shopDiscount"
android:layout_alignParentRight="true"
android:text="@string/discountRateLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView6"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:background="@drawable/roundbutton"
android:text="@string/discountPublishButtonLabel"
android:textColor="@color/white"
android:onClick="SubmitData"
android:width="250dp" />
<EditText
android:id="@+id/shopCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_alignRight="@+id/shopName"
android:ems="10"
android:inputType="text" >
</EditText>
<EditText
android:id="@+id/shopDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/shopLocation"
android:layout_below="@+id/shopLocation"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/shopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_marginLeft="14dp"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/categoriesSpinner"
android:layout_alignRight="@+id/textView2"
android:text="@string/discountCategoryLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<Spinner
android:id="@+id/categoriesSpinner"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_above="@+id/shopName"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView2"
android:layout_marginTop="206dp"
android:text="@string/discountCityLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/shopCity"
android:layout_alignLeft="@+id/textView5"
android:layout_marginBottom="18dp"
android:text="@string/discountShopLabel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textStyle="bold" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginTop="14dp"
android:gravity="right"
android:text="@string/addDiscountMsg"
android:textAlignment="viewEnd"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textDirection="anyRtl" />
</RelativeLayout>
</ScrollView>
Hope help you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With