Constraint Layout - Group visibility is not working inside dynamic module
Asked Answered
W

7

14

Has anyone experienced issues with ConstraintLayout group visibility? I'm using ConstraintLayout 1.1.3 and I'm setting the visibility of group in both XML layout and java code. But it does not change the visibility status. It is always visible.

This is the layout file

 <?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">

<ImageView
    android:id="@+id/iv_message_icon_activity_dictionary"
    android:layout_width="@dimen/message_icon_width"
    android:layout_height="@dimen/message_icon_height"
    android:layout_marginBottom="@dimen/message_text_view_margin_top"
    android:contentDescription="@string/app_name"
    android:src="@drawable/icon_error"
    app:layout_constraintBottom_toTopOf="@+id/tv_message_activity_dictionary"
    app:layout_constraintEnd_toEndOf="@id/tv_message_activity_dictionary"
    app:layout_constraintStart_toStartOf="@id/tv_message_activity_dictionary" />

<TextView
    android:id="@+id/tv_message_activity_dictionary"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginStart="20dp"
    android:gravity="center"
    android:textColor="@color/black"
    android:textSize="@dimen/medium_text_size"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="@string/msg_no_internet" />

<Button
    android:id="@+id/btn_try_again_activity_dictionary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/try_again_button_margin_top"
    android:background="@drawable/rounded_button"
    android:minHeight="30dp"
    android:minWidth="@dimen/try_again_button_width"
    android:padding="10dp"
    android:text="@string/btn_try_again"
    android:textAllCaps="false"
    android:textColor="@color/white"
    android:textSize="@dimen/text_size_5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tv_message_activity_dictionary" />

<android.support.constraint.Group
    android:id="@+id/group_component_activity_dictionary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    app:constraint_referenced_ids="btn_try_again_activity_dictionary,tv_message_activity_dictionary,iv_message_icon_activity_dictionary" />

</android.support.constraint.ConstraintLayout>

What could be the reason for this?

UPDATE:

I forgot to mention that I'm using this layout inside of a dynamic module. Also I tested this using in the base module and it is working as expected, but not in a dynamic module. So finally I figured out the reason for the issue. Also when I debugged the code and evaluated this expression (group.getVisibility == View.GONE), it gave me TRUE.(even though the views inside the group are still visible)

Any suggestion is appreciated.

Wabble answered 1/3, 2019 at 12:3 Comment(8)
crystal ball not working today, please show codeMauramauralia
@TimCastelijns updatedWabble
have you tried setting each component's visibility to gone?Carver
Have you looked at this question. #47865936 Look at the response by lukjarUnmake
@user3170251yes, if I set individual view's visibility from java code then it is working. but then there is no point of using groupsWabble
@Unmake yes, I have looked at it. but the issue is why the visibility is not working from xml propertiesWabble
@Wabble Have you tried using the 2.0.0 alpha? I have been using 2.0.0 for months now and it is working fine for me.Unmake
@Unmake I tried it as well, but not good. I couldn't mention that, i'm using this layout in a dynamic module. I updated the question.Wabble
W
9

Found a fix for this issue and I hope this will save time for someone else. It's strange but the reason for this issue is using the ConstraintLayout - Group, inside a dynamic module.

So the fix is in java code, once you got a reference to the group then set the reference ids using int array like below,

Group group = findViewById(R.id.group);
group.setReferencedIds(new int[]{R.id.btn_try_again_activity_dictionary, R.id.tv_message_activity_dictionary, R.id.iv_message_icon_activity_dictionary});

After this, it works as expected. Therefore no need to set visibility of individual views in the group.

Wabble answered 2/3, 2019 at 18:31 Comment(1)
So this is a work around. Actually it was working fine, but now its gone.Tooth
E
8

Don't forget that you can use androidx.constraintlayout.widget.Group only if the parent view is constraintlayout if the parent is LinearLayout then it will not work, then you can try this answer which says that you add your required views inside FrameLayout or LinearLayout then set the visiblity of the sub parent view.

Erastian answered 14/7, 2021 at 9:44 Comment(0)
M
5

I was having the same issue using ConstraintLayout v1.1.3. I was setting the visibility of the group in the XML but it wasn't working.

As ConstraintLayout v2.0.0 is already on beta, I migrated from v1.1.3 to v2.0.0-beta2 (check the latest version) and the issue is not happening anymore. I can now change the visibility of the group from XML or code without any problem.

Myrtlemyrvyn answered 10/7, 2019 at 10:2 Comment(2)
i was having the same issue, and using version 2.0.0-beta7 fixed itSesquialtera
Same here, the simple update to 2.0.0-beta8 fixed my issue. Probably a BUG in the v1.1.3Naldo
U
3

I have the same problem even though I'm on 2.0.0-beta8. group.visibility = View.GONE/VISIBLE doesn't work but group.isGone = true/false works

Undergird answered 24/7, 2020 at 11:23 Comment(0)
S
1

I have to add what was my problem - I mistakenly did not put the Group widget inside a ConstraintLayout and could not figure out how come it's working in one layout and not working in another. Thankfully, I didn't spend that much time on it but this mistake still can happen.

Semidiurnal answered 4/7, 2021 at 14:32 Comment(0)
S
0

This will be fixed with ConstraintLayout 2.0.2.

https://androidstudio.googleblog.com/2020/10/constraintlayout-202.html

Sabu answered 28/10, 2020 at 0:45 Comment(0)
G
0

You need to use isGone attribute for groups in constraint layout, and Groups will properly work with Parent layout as constraint layout

Gerius answered 3/4, 2023 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.