Scrolling linear layout in android

I want to add a scrollbar to my linear layout form as it is too long.

It has 19 text views and edits views.

Please can add scroll code to xml file.

this is my xml code:

<?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" >

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



<TextView
    android:id="@+id/Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

 <TextView
    android:id="@+id/FirstName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="FirstName"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editFirstName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>

 <TextView
    android:id="@+id/DateOfBirth"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Date Of Birth"
    android:textAppearance="?android:attr/textAppearanceLarge" />

 <ScrollView
     android:id="@+id/scrollView1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" >

     <LinearLayout
         android:id="@+id/linearLayout2"
         android:layout_width="match_parent"
         android:layout_height="match_parent" >
     </LinearLayout>
 </ScrollView>

 <EditText
     android:id="@+id/editDateOfBirth"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:inputType="date" />

 <TextView
    android:id="@+id/PlaceOfbirth"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Place Of birth"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editPlaceOfbirth"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>
 <TextView
    android:id="@+id/Address"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Address"
    android:textAppearance="?android:attr/textAppearanceLarge" />

 <EditText
     android:id="@+id/editAddress"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:inputType="textPostalAddress" />

 <TextView
    android:id="@+id/PhoneNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="PhoneNumber"
    android:textAppearance="?android:attr/textAppearanceLarge" />

 <EditText
     android:id="@+id/editPhoneNumber"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:inputType="phone" />

 <TextView
    android:id="@+id/e_mail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="e-mail"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/edite_mail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress" >

    <requestFocus />
</EditText>
 <TextView
    android:id="@+id/SocialInsurance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Social Insurance"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editSocialInsurance"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number" >

    <requestFocus />
</EditText>
 <TextView
    android:id="@+id/GeneralPractioner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="General Practioner"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editGeneralPractioner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/Size"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Size"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editSize"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>

 <TextView
    android:id="@+id/Weight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Weight"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editWeight"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>
 <TextView
    android:id="@+id/BMI"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BMI"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editBMI"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>
 <TextView
    android:id="@+id/BloodGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Blood Group"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editBloodGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>
<TextView
    android:id="@+id/DoctorNote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Doctor Note"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editDoctorNote"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine" >

    <requestFocus />
</EditText>
<TextView
    android:id="@+id/NurseNote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nurse Note"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editNurseNote"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:inputType="textMultiLine" >

    <requestFocus />
</EditText>
<TextView
    android:id="@+id/Results"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Results"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editResults"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:inputType="textMultiLine" >

    <requestFocus />
</EditText>
 <TextView
    android:id="@+id/DrugPrescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DrugPrescription"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editDrugPrescription"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>
<TextView
    android:id="@+id/Pathology"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pathology"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editPathology"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>
<TextView
    android:id="@+id/Recommendation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Recommendation"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editRecommendation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 
    android:inputType="textMultiLine" >
    <requestFocus />
</EditText>

<Button
    android:id="@+id/ButtonAddDatabase"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Register" />

  </LinearLayout>
 </ScrollView>

      

it works fine. But when I add the scroll it gives me an error (FATAL ERROR)

    03-16 00:06:18.140: E/AndroidRuntime(1215): FATAL EXCEPTION: main
    03-16 00:06:18.140: E/AndroidRuntime(1215): java.lang.RuntimeException: Unable to   start activity ComponentInfo{android.sqlite/android.sqlite.CreatePatientActivity}: java.lang.RuntimeException: Binary XML file line #3: You must supply a layout_width attribute.

      

+3


source to share


1 answer


All you have to do is place the LinearLayout in a ScrollView container:

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

    <LinearLayout android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        ...

    </LinearLayout>
</ScrollView>

      



Note that the wrap_content parameter is used here because the content of the LinearLayout determines the height.

+3


source







All Articles