Question

In my application i am allowing user to add upto 5 EditText views by clicking "Add More Contacts". But when a EditText view appears the layout falls vertically. So i want to place a ScrollView for the EditText Views only not for the Button. I am trying but failing everytime. Can someone please help.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mLayout"
android:layout_width="match_parent"
android:layout_height="237dp"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="236dp" >

    <EditText
        android:id="@+id/edittext"
        android:layout_width="298dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="@string/et_display" >

        <requestFocus />
    </EditText>
</ScrollView>

<Button
    android:id="@+id/bpickperson"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="169dp"
    android:text="@string/b_pick" />

<Button
    android:id="@+id/balert"
    android:layout_width="288dp"
    android:layout_height="wrap_content"
    android:text="@string/b_alert" />

<Button
    android:id="@+id/baddmorecontacts"
    android:layout_width="288dp"
    android:layout_height="wrap_content"
    android:text="@string/b_addmorecontacts" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/b_facebook"
        android:layout_width="144dp"
        android:layout_height="fill_parent"
        android:text="@string/b_facebook" />

    <Button
        android:id="@+id/b_twitter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/b_twitter" />
</LinearLayout>

Was it helpful?

Solution

// try this way
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <EditText
            android:id="@+id/edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/et_display" >
        </EditText>
    </ScrollView>

    <Button
        android:id="@+id/bpickperson"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/b_pick" />

    <Button
        android:id="@+id/balert"
        android:layout_width="288dp"
        android:layout_height="wrap_content"
        android:text="@string/b_alert" />

    <Button
        android:id="@+id/baddmorecontacts"
        android:layout_width="288dp"
        android:layout_height="wrap_content"
        android:text="@string/b_addmorecontacts" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <Button
            android:id="@+id/b_facebook"
            android:layout_width="144dp"
            android:layout_height="wrap_content"
            android:text="@string/b_facebook" />

        <Button
            android:id="@+id/b_twitter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/b_twitter" />
    </LinearLayout>
</LinearLayout>

OTHER TIPS

A ScrollView can only contain ONE child view. Right now it contains an EditText, so it can't contain any more child views. You have to put your EditTexts into another layout, lets say a LinearLayout and put that LinearLayout into the ScrollView. You then add your EditTexts to the LinearLayout and not to the ScrollView.

try this one :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mLayout"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
<LinearLayout  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id ="@+id/editlayout"
    android:layout_marginTop="25dip" 
    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="50dp" >
        <EditText
            android:id="@+id/edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="5"
            android:hint="et_display" >

            <requestFocus />
        </EditText>
    </ScrollView>
</LinearLayout>
    <Button
        android:id="@+id/bpickperson"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="b_pick" />

    <Button
        android:id="@+id/balert"
        android:layout_width="288dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="b_alert" />

    <Button
        android:id="@+id/baddmorecontacts"
        android:layout_width="288dp"
        android:layout_height="wrap_content"
         android:layout_marginTop="10dp"
        android:text="b_addmorecontacts" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip" >

        <Button
            android:id="@+id/b_facebook"
            android:layout_width="144dp"
            android:layout_height="fill_parent"
            android:text="b_facebook" />

        <Button
            android:id="@+id/b_twitter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="b_twitter" />
    </LinearLayout>
    </LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top