jl90
jl90

Reputation: 649

ListView pushes button out of screen

I'm trying to get the ListView to remain the same size even if it overflows. However, once the ListView overflows, it pushes the buttons below off the screen. How do I prevent it from pushing other elements that are below it?

As you can see from the above images, the second shows that the buttons get pushed off the screen once the ListView overflows.

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/splash_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout 
    android:background="@color/splash_bg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:paddingBottom="10dp"
        android:textColor="@color/splash_text"
        android:text="@string/sub" />

    <EditText 
        android:id="@+id/grp_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/field1"
        android:lines="1" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/members"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textSize="15sp"
            android:paddingTop="5dp"
            android:textColor="@color/splash_text"
            android:text="@string/members" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/splash_bg"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/name"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="@string/field2" />

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

        </LinearLayout>

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:background="@color/splash_bg" />

    </LinearLayout>

</LinearLayout>

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:gravity="bottom"
     android:paddingLeft="5dp"
     android:paddingRight="5dp"
     android:paddingBottom="5dp"
     android:paddingTop="10dp" >

    <Button 
        android:id="@+id/save_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:text="Save" />

     <Button 
        android:id="@+id/cancel_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel" />

</LinearLayout>

Upvotes: 1

Views: 2041

Answers (3)

Osama Nsr
Osama Nsr

Reputation: 87

I added this line : android:layout_weight="1" to the LinearLayout where EditText exist (my problem source) and the problem is gone !
look at the code where I commented <!-- HERE!!! -->


LOL I know i'm very late to answer this Q ,, sorry ,, but for anyone who is still struggling !

enter image description here


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

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="ID : "
            android:layout_margin="@dimen/EdgeMargins"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText"
            android:layout_margin="@dimen/EdgeMargins"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearLayout2"
        android:layout_weight="1"> <!-- HERE!!! -->

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Any Notes :"
            android:id="@+id/textView2"
            android:layout_margin="@dimen/EdgeMargins"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_margin="@dimen/EdgeMargins" />

    </LinearLayout>

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Done"
        android:id="@+id/button"
        android:layout_margin="@dimen/EdgeMargins" />

</LinearLayout>


i had my problem with the highlighted EditText in the pic ,,, it expands with the entered text and pushes the DONE Button down !
I know my solution may look so silly but I took a whole day to solve this !

Hope it helps ^_^ GL

Upvotes: -2

Raghunandan
Raghunandan

Reputation: 133560

You can use a relative layout for this. Modify the below according to your needs

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginRight="11dp"
        android:layout_marginTop="11dp"
        android:text="Create New Group" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="23dp"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="11dp"
        android:layout_marginTop="18dp"
        android:text="Members" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="31dp"
        android:ems="10" />

    <ListView
        android:id="@android:id/list"
        android:layout_above="@+id/button1"
        android:layout_below="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
         />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="13dp"
        android:text="ADD" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button3"
        android:layout_below="@android:id/list"
        android:text="Cancel" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/textView1"
        android:text="Save" />

</RelativeLayout>

enter image description here

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157457

Changet

  <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:background="@color/splash_bg" />

to

  <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:background="@color/splash_bg" />

Upvotes: 8

Related Questions