androidandroid-fragmentsandroid-recyclerviewoverlapping

RecyclerView overlaps TextView and Button or disappear inside the fragment


I stuck for a whole day with simple question...
Inside my fragment I want to place vertically from the top - TextView(title), below it - RecyclerView, and below recyclerview - button This is my fragment xml

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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=".presentation.view.CurrentLocationFragment">

<TextView
    android:id="@+id/country_output"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/recycler_view_title"
    android:textAlignment="center"
    android:textSize="30sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    app:layout_constraintBottom_toTopOf="@id/btn_country_choice"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/country_output"
    />

<Button
    android:id="@+id/btn_country_choice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/yes"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

in this case recyclerview overlaps textview and button partially overlaps recyclerview. this is a screenshot ovelapping

if I define in recyclerview android:layout_height="0dp" then I have another problem - recyclerview disappered

disappering

preview in android studio displays me all like I need, but on device all is another android studio preview

any suggestions?


Solution

  • Your RecyclerView has constraints issues. Its height is not being constraint, it is wrapping its content. You should use 0dp, which means MATCH_CONSTRAINT:

    Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"

    Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".

    https://developer.android.com/reference/android/support/constraint/ConstraintLayout

    Also, instead of @id use @+id. This is because you Button ID is only declared before your RecyclerView in your layout. From the docs:

    The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace.

    So, applying everything:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="12dp"
        app:layout_constraintBottom_toTopOf="@+id/btn_country_choice"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/country_output"/>