Home > front end >  Why is the space still there for a hidden EditText field in my Android layout?
Why is the space still there for a hidden EditText field in my Android layout?

Time:10-14

I'm creating a dynamic form input and sometimes the fields are hidden. But the layout still shows the empty space when I set it to invisible in onCreateView method. I want all the fields to move up if one of them is hidden.

binding.vendorinvoice.setVisibility(View.INVISIBLE)

Here's my layout XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/ic_launcher_background"
    tools:context=".activity.NewReceipt.NewReceiptReceiptHeader">

    <!-- TODO: Update blank fragment layout -->

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:elevation="3dp"
        android:background="@color/white" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <EditText
                android:id="@ id/projectnumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="Project Number" />

            <EditText
                android:id="@ id/projectsequence"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="Project sequence number" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:elevation="3dp"
        android:background="@color/white">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <EditText
                android:id="@ id/vendor"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="Vendor" />

            <EditText
                android:id="@ id/vendorinvoice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="Vendor Invoice number" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:elevation="3dp"
        android:background="@color/white">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <EditText
                android:id="@ id/carrier"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="Carrier" />

            <EditText
                android:id="@ id/carriertrackingnumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="Carrier tracking number" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

and here's the result. what is the blank space there under Vendor??

enter image description here

CodePudding user response:

Use setVisibility(View.GONE) instead of setVisibility(View.INVISIBLE) to avoid the blank space that is left.

Making the view INVISIBLE makes the view utilize the space that it was assigned to occupy while GONE will make the view behave as if it was not even put in the layout - it won't occupy any space.

  • Related