android setVisibility does not display if initially set to invisble
Asked Answered
D

13

30

I have a glsurface occupying the full screen. At the click of a button I want another layout to appear (settings type of thing). If I start with the overlay being visible, I can make it invisible and then visible again with no problem. But if I start with it invisible, I cannot make it ever visible again. Code follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <android.opengl.GLSurfaceView
    android:id="@+id/glPlaySurface"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</android.opengl.GLSurfaceView>

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/btnRotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:checked="true"
        android:text="R"
        android:textColor="#000" />

    <RadioButton
        android:id="@+id/btnPan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="P"
        android:textColor="#000" />
</RadioGroup>

<Button
    android:id="@+id/btnLights"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@+id/radioGroup1"
    android:text="Lights" />

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutLights"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:visibility="visible" <--- Does not work if set to invisible
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"

    android:background="#fff" >

    <Button
    android:id="@+id/btnLightsOK"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="15dp"
    android:text="OK" />

    <Button
    android:id="@+id/btnLights"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="15dp"
    android:text="OK" />

</RelativeLayout>

</RelativeLayout>


private OnClickListener mOnLightsClick = new OnClickListener() {
    public void onClick(View arg0) {
        if(mLayoutLights.getVisibility() == View.VISIBLE) {
            mLayoutLights.setVisibility(View.INVISIBLE);
        }
        else {
            mLayoutLights.setVisibility(View.VISIBLE);
        }
    }
};
Derril answered 4/3, 2012 at 16:40 Comment(4)
the problem is related to the glsurface. If i replace the glsurface with an ordinary surfaceview it works fine.Derril
more info: if I set the glsurface to GONE, the set the layout to VISIBLE, then set the glsurface back to VISIBLE - it works. But... if I set the glsurface to INVISIBLE instead of GONE - it does not work.Derril
ok - the surface_view_overlay in the api demos does exactly what i'm looking for - i'm gonna see where i went wrong and then post.Derril
run set visibility on UI Thread :)Presumptive
D
19

Got it. You have to set the visibility of all the items in the layout, not just the layout. So this code worked:

if (mLayoutLights.getVisibility() == View.VISIBLE) {
    ((Button) findViewById(R.id.btnLightsOK)).setVisibility(View.GONE);
((Button) findViewById(R.id.btnLightsCnc)).setVisibility(View.GONE);
mLayoutLights.setVisibility(View.GONE);
} else {
    mLayoutLights.setVisibility(View.VISIBLE);
((Button) findViewById(R.id.btnLightsOK)).setVisibility(View.VISIBLE);
((Button) findViewById(R.id.btnLightsCnc)).setVisibility(View.VISIBLE);
}
Derril answered 5/3, 2012 at 1:33 Comment(0)
G
34

Had similar error but it was due to my silly mistake of not using the UiThread.

Activity act = (Activity)context;
act.runOnUiThread(new Runnable(){
@Override
public void run() {
    mLayoutLights.setVisibility(View.VISIBLE);  
} });
Gazpacho answered 1/7, 2014 at 20:18 Comment(1)
Thank you man, it solved my problem ! I was setting the visibility in a callback, and had no idea why the view wasn't updated immediately, but now it works. It seems that I need to work on UI threads..Rolland
D
19

Got it. You have to set the visibility of all the items in the layout, not just the layout. So this code worked:

if (mLayoutLights.getVisibility() == View.VISIBLE) {
    ((Button) findViewById(R.id.btnLightsOK)).setVisibility(View.GONE);
((Button) findViewById(R.id.btnLightsCnc)).setVisibility(View.GONE);
mLayoutLights.setVisibility(View.GONE);
} else {
    mLayoutLights.setVisibility(View.VISIBLE);
((Button) findViewById(R.id.btnLightsOK)).setVisibility(View.VISIBLE);
((Button) findViewById(R.id.btnLightsCnc)).setVisibility(View.VISIBLE);
}
Derril answered 5/3, 2012 at 1:33 Comment(0)
A
11

In my case, with a plain SurfaceView, I just set the View to GONE in xml, not INVISIBLE. Then I can set VISIBILITY correctly after that.

Alimentation answered 18/6, 2015 at 19:51 Comment(1)
This is really a very nice answerTransition
I
5

I have faced the same problem and debug log was clearly showing that the visibility is correctly set.

I was using databinding. For me the solution below worked.

activity.runOnUiThread(() -> binding.getRoot().post(() -> {
    binding.additionalDataHolderOne.setVisibility(View.GONE);
    binding.firstStreamHolder.setVisibility(View.VISIBLE);
}));

Actually, binding.getRoot().post(() -> {}); did the job.

Interstellar answered 6/12, 2019 at 5:44 Comment(0)
V
4

I would suggest trying three independent things.

  1. Changing layout width and height to wrap content as it could be an issue with matching the parent.
  2. Calling bringToFront on the view
  3. Wrapping the surfaceView in a FrameLayout (this is related to #2, but it still might help)
Vestry answered 4/3, 2012 at 17:39 Comment(6)
Thanks - but none of those made a difference.Derril
Do you know if the render thread is running or even starting on the surface?Vestry
yes - the 3d responds to touch and renders properly (I've tried both render continuously and render when dirty - no difference)Derril
I mean specifically in the case when it starts invisible. It would seem like the renderer isn't running when you start it invisible. Is it still running?Vestry
yes it is - the 3d responds and renders correctly when the other layout is visible or invisible.Derril
after two god damn days your second suggestion (bringToFront) saved me. thanks.Kafka
B
1

if you set the CalendarView to visibility "VISIBLE" and in the inflateView/OnCreateView after you find it with "findById" just set to "GONE" and you can do what ever you want with it.

calendarView = (CalendarView) view.findViewById(R.id.all_scores_calendar_view); calendarView.setVisibility(View.GONE);

calendarView.setVisibility(View.VISIBLE);

Bambara answered 1/5, 2018 at 9:33 Comment(0)
L
0

I don't why is not working (for me also) but as a workaround you can do:

 val params = mLayoutLights.layoutParams
    if (shoouldShow()) {
        params.height = WRAP_CONTENT
    } else {
        params.height = 0
    }
    mLayoutLights.layoutParams = params
Literality answered 4/4, 2020 at 11:32 Comment(0)
P
0

I faced a problem like this before I was put 2 views in a LinearLayout. one of the 2 views is gone and the other one is visible.

and when I try to make the gone one visible and the visible one gone, it was not working.

I solved this issue by using RelativeLayout as a container layout instead of the LinearLayout, It's working fine then.

Pallas answered 30/7, 2020 at 10:47 Comment(0)
A
0

In my case, I have called .setVisiblity(View.GONE) in an element where visibility is GONE

 if (temp.getVisibility() == View.VISIBLE) {
            temp.setVisibility(View.GONE);
        }

doing something like that worked for me.

Autocrat answered 23/6, 2021 at 7:43 Comment(0)
B
0

In my case I just changed layout id and it worked.

Belier answered 26/8, 2022 at 11:31 Comment(0)
C
0

If you are using ViewBinding and you have a nested View via Include with a View whose Id is the same as in the main View, there may be a situation that the Visiblity property is not applied to one of these Views

Cleaver answered 8/11, 2022 at 0:36 Comment(0)
P
0

i don't know whats reas of not working but i put my layout into cardview and set cardview visiblity gone and it's work for me

<androidx.cardview.widget.CardView
    android:id="@+id/rec"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    android:elevation="10dp"
    android:foregroundGravity="center"
    app:cardCornerRadius="15dp"
    app:layout_constraintBottom_toTopOf="@+id/guideline3"
    app:layout_constraintEnd_toEndOf="@+id/cardTxtResult"
    app:layout_constraintStart_toStartOf="@+id/cardTxtResult"
    app:layout_constraintTop_toTopOf="@+id/guideline6">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/main_rec"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="@color/gray"
        android:foregroundGravity="center"
        android:padding="10dp"
        android:stackFromBottom="false"
        android:transcriptMode="alwaysScroll" />
</androidx.cardview.widget.CardView>

and java code connected to checkbox

 CHK.setOnCheckedChangeListener((compoundButton, b) -> {
        if (b) {
            rec.setVisibility(View.VISIBLE);
        } else {
            rec.setVisibility(View.GONE);
        }
    });
Preindicate answered 26/2, 2023 at 11:13 Comment(0)
S
-3
case R.id.title_call_button:
if(llButtonCallNow.getVisibility() != View.VISIBLE){
llButtonCallNow.setVisibility(View.VISIBLE);
}
    else{
    llButtonCallNow.setVisibility(View.GONE);
    Toast.makeText(getBaseContext(), ("Im here baby :)"),
    Toast.LENGTH_SHORT).show();

    }
break;
Salol answered 4/3, 2014 at 6:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.