Skip to content
Advertisement

Buttons Disappear With ScrollView on Android Studio

without ScrollView, my buttons look fine but after I use it, the buttons disappear, I tried a lot at solution but the thing appear is just <TextView>. How can I make them appear as preferred?

activity_main.xml

<RelativeLayout 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"
android:background="@drawable/background">



<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical">

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

<TextView
    android:id="@+id/HeaderText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="5dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="21dp"
    android:gravity="right"
    android:hapticFeedbackEnabled="false"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="25"
    android:text="HeaderText"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    android:textColor="@color/white"
    app:layout_constraintBottom_toTopOf="@+id/Button1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="RtlHardcoded" />

<Button
    android:id="@+id/Button1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="44dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:background="@android:drawable/btn_radio"
    android:text="Button1"
    app:layout_constraintBottom_toTopOf="@+id/Button2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/HeaderText"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/Button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="1dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:background="@android:drawable/btn_radio"
    android:text="Button2"
    app:layout_constraintBottom_toTopOf="@+id/Button3"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/Button1" />

<Button
    android:id="@+id/Button3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="1dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:background="@android:drawable/btn_radio"
    android:text="Button3"
    app:layout_constraintBottom_toTopOf="@+id/Button4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/Button2" />

<Button
    android:id="@+id/Button4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="1dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="364dp"
    android:background="@android:drawable/btn_radio"
    android:text="Button4"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/Button3" />

</LinearLayout>
</ScrollView>

</RelativeLayout>    

MainActivity.java is as default.

It works like this:

Advertisement

Answer

The problem is that your button widths are zero.

<Button
android:id="@+id/Button2"
android:layout_width="0dp" //<------------- HERE
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="@android:drawable/btn_radio"
android:text="Button2"
app:layout_constraintBottom_toTopOf="@+id/Button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Button1" />

Change the width and you will see them.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement