Search code examples
androidandroid-layoutandroid-listviewandroid-xmlandroid-constraintlayout

Why using constraint layout, a listView pushes button out of screen? how to solve it?


I have an editText, a button and a listView in a constraint layout.

The problem is that the listView pushes button and editText out of screen when I fill it in.

Can someone help me to solve it, please? Thanks a lot

Here the XML code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lvJugadores"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </ListView>

    <EditText
        android:id="@+id/etJugador"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/etJugadorHint"
        android:inputType="textPersonName"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/lvJugadores" />

    <Button
        android:id="@+id/btnConfirmarNuevoJugador"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnConfirmarNuevoJugador"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/etJugador" />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Try this.

      <?xml version="1.0" encoding="utf-8"?>
    
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        tools:context=".MainActivity">
    
        <ListView
            android:id="@+id/lvJugadores"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
    
            app:layout_constraintBottom_toTopOf="@id/etJugador">
    
        </ListView>
    
        <EditText
            android:id="@+id/etJugador"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="etJugadorHint"
            android:inputType="textPersonName"
            android:textAlignment="center"
            app:layout_constraintBottom_toTopOf="@id/btnConfirmarNuevoJugador"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
    
            app:layout_constraintTop_toBottomOf="@+id/lvJugadores" />
    
        <Button
            android:id="@+id/btnConfirmarNuevoJugador"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="btnConfirmarNuevoJugador"
             app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/etJugador"
    
              //this attribute make button and textview always within screen
              app:layout_constraintBottom_toBottomOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>