VioletLogic
VioletLogic

Reputation: 65

setVisibility(View.GONE) shrinking display size

Android version 11.0.4

I am trying to use a ListView to display a series of elements with different types of information to display in the same list using a custom ListAdapter.

To make the same ListAdapter able to display different types of information I'm creating a custom XML template with all possible elements that I want to display and hiding the ones I don't want to show programmatically.

However, when I hide the extra elements I don't want to show the size of the view displaying them shrinks.

When the extra elements are shown

When the extra elements are gone

The code that hides the elements:

TextView catagory = (TextView) convertView.findViewById(R.id.title);
TextView time1 = (TextView) convertView.findViewById(R.id.time1);
TextView time2 = (TextView) convertView.findViewById(R.id.time2);
TextView location = (TextView) convertView.findViewById(R.id.location);
TextView description = (TextView) convertView.findViewById(R.id.description);

View separator1 = (View) convertView.findViewById(R.id.separator_vertical1);
View separator2 = (View) convertView.findViewById(R.id.separator_horizontal1);
View separator3 = (View) convertView.findViewById(R.id.separator_vertical2);
View separator4 = (View) convertView.findViewById(R.id.separator_horizontal2);

catagory.setText(item.name);
Class type = item.getType();
if (EventItem.class.equals(type)) {
    time1.setText(String.valueOf(((EventItem)item).timeStart));
    time2.setText(String.valueOf(((EventItem)item).timeEnd));
    location.setText(((EventItem)item).location);
    description.setText(((EventItem)item).description);
} else if (TaskItem.class.equals(type)) {
    time1.setText(String.valueOf(((TaskItem)item).timeDue));
    description.setText(((TaskItem)item).description);
    //hide unused elements
    separator2.setVisibility(View.GONE);
    location.setVisibility(View.GONE);
    separator3.setVisibility(View.GONE);
    time2.setVisibility(View.GONE);
} else if (ReminderItem.class.equals(type)) {
    time1.setText(String.valueOf(((ReminderItem)item).time));
    //hide unused elements
    separator2.setVisibility(View.GONE);
    location.setVisibility(View.GONE);
    separator3.setVisibility(View.GONE);
    time2.setVisibility(View.GONE);
    separator4.setVisibility(View.GONE);
    description.setVisibility(View.GONE);
} else if (SubjectItem.class.equals(type)) {
    description.setText(((SubjectItem)item).description);
} else if (NoteItem.class.equals(type)) {
    description.setText(((TaskItem)item).description);
    //hide unused elements
    separator1.setVisibility(View.GONE);
    time1.setVisibility(View.GONE);
    separator2.setVisibility(View.GONE);
    location.setVisibility(View.GONE);
    separator3.setVisibility(View.GONE);
    time2.setVisibility(View.GONE);
} else if (ContactItem.class.equals(type)) {
    //hide unused elements
    separator1.setVisibility(View.GONE);
    time1.setVisibility(View.GONE);
    separator2.setVisibility(View.GONE);
    location.setVisibility(View.GONE);
    separator3.setVisibility(View.GONE);
    time2.setVisibility(View.GONE);
}

The XML template:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/rectangle">
    <TextView
            android:id="@+id/title"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:textAlignment="textStart"
            android:textSize="16sp"
            android:textColor="#ffffffff"/>
    <View
            android:id="@+id/separator_vertical1"
            android:layout_width="1dp"
            android:layout_height="23dp"
            android:layout_toStartOf="@id/time1"
            android:background="@android:color/white"/>
    <TextView
            android:id="@+id/time1"
            android:layout_width="94dp"
            android:layout_height="wrap_content"
            android:layout_toStartOf="@id/title"
            android:layout_alignParentEnd="true"
            android:textAlignment="center"
            android:textSize="16sp"
            android:textColor="#ffffffff"/>
    <View
            android:id="@+id/separator_horizontal1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@id/title"
            android:background="@android:color/white"/>
    <TextView
            android:id="@+id/location"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@id/separator_horizontal1"
            android:textAlignment="textStart"
            android:textSize="16sp"
            android:textColor="#ffffffff"/>
    <View
            android:id="@+id/separator_vertical2"
            android:layout_width="1dp"
            android:layout_height="23dp"
            android:layout_toStartOf="@id/location"
            android:layout_below="@id/separator_horizontal1"
            android:background="@android:color/white"/>
    <TextView
            android:id="@+id/time2"
            android:layout_width="94dp"
            android:layout_height="wrap_content"
            android:layout_toStartOf="@id/location"
            android:layout_below="@id/separator_horizontal1"
            android:layout_alignParentEnd="true"
            android:textAlignment="center"
            android:textSize="16sp"
            android:textColor="#ffffffff"/>
    <View
            android:id="@+id/separator_horizontal2"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@id/location"
            android:background="@android:color/white"/>
    <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/separator_horizontal2"
            android:textAlignment="textStart"
            android:textSize="20sp"
            android:textColor="#ffffffff"/>

The ListView the elements are displayed in:

<ListView
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"

    android:id="@+id/text_overview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:textAlignment="center"
    android:textSize="20sp"
    android:textColor="#ffffffff"
    android:divider="@android:color/transparent"
    android:dividerHeight="8dp"
    android:headerDividersEnabled="true"
    android:footerDividersEnabled="true"
    android:scrollbars="none"

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

Upvotes: 2

Views: 489

Answers (3)

VioletLogic
VioletLogic

Reputation: 65

I eventually got it working, not sure how but setting each element as visible before going into the if statements stoped the shrinking of the view window. It also solved the issue of some list items missing elements that shouldn't have been hidden. I can only assume some resources were being reused somewhere and arriving pre-hidden.

Upvotes: 0

maitree thakar
maitree thakar

Reputation: 19

Use View.INVISIBLE which takes up space and invisible the view. while you are using View.GONE which doesn't takes up space for the view and invisible the view.

For more description, you can go with:https://developer.android.com/reference/android/transition/Visibility

Upvotes: 0

Masoom Badi
Masoom Badi

Reputation: 996

The answer to your question is to use View.INVISIBLE

However, when I hide the extra elements I don't want to show the size of the view displaying them shrinks.

View.GONE: This view is invisible, and it doesn't take any space for layout purposes.

View.INVISIBLE: This view is invisible, but it still takes up space for layout purposes.

Here's the link of Documentation.

Upvotes: 1

Related Questions