androidandroid-studioandroid-layoutpreview

Android preview does not show custom view, but it's visible on device


I have the following custom view:

<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/declineButton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/confirmButton"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="decline"/>

    <Button
        android:id="@+id/confirmButton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/declineButton"
        app:layout_constraintBottom_toBottomOf="@id/declineButton"
        app:layout_constraintStart_toEndOf="@id/declineButton"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:text="confirm"/>

</androidx.constraintlayout.widget.ConstraintLayout>

In the preview for this layout, the buttons show as expected.

custom view

However, if I take that custom view and put it into my activity:

<?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"
    android:background="@color/backgroundColor">

    <TextView
        android:id="@+id/textAbove"
        style="@style/someStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/something"
        app:layout_constraintBottom_toTopOf="@id/buttons"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="text above"/>

    <MyCustomButtonsView
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/textBelow"
        app:layout_constraintTop_toBottomOf="@id/textAbove"/>

    <TextView
        android:id="@+id/textBelow"
        style="@style/someStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginBottom="16dp"
        android:text="text below"/>
</androidx.constraintlayout.widget.ConstraintLayout>

The buttons do not show in the preview, because the custom view somehow has the height 0:

enter image description here

So, if I add tools:layout_height="80dp" to MyCustomButtonsView

<MyCustomButtonsView
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_height="80dp
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/textBelow"
        app:layout_constraintTop_toBottomOf="@id/textAbove"/>

It shows that the buttons themselves are the problem, because the view gets larger, but the buttons still don't show:

enter image description here

The thing is that, if I run the app on my device, the buttons show as expected (without setting a specific hight as in the tools line).

What I want to know is why this occurs and how to avoid it.


Solution

  • Looks like this was just a glitch in Android Studio. I've rebuilt and restarted before I asked this question and it didn't help. One day later, I open Android Studio and, lo and behold, it shows the buttons properly. So I guess restarting the whole system might be a solution.