-1

I really didn't want to post it here because this problem sounds really stupid and is difficult to describe, but after hiting my head 2 hours for a stupid layout problem i decided to try

I've one activity with several layout components...
During on create all components are set to be invisible just one keeps visible.

When user presses the button, all components turn visible
when presses button again, all components SHOULD turn invisible again

ALL COMPONENTS VISIBILITY IS ADJUST IN ONLY ONE METHOD

so the activity looks like:

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_giveaway, R.id.mainView);
/*lots of stuff*/
//last thing
   makeVisible(View.INVISIBLE);
}

  private void makeVisible(int visi) {
        findViewById(R.id.cardView).setVisibility(visi);
             ((ViewGroup) findViewById(R.id.influencerLayout)).setVisibility(visi);
        this.recyclerView.setVisibility(visi);
    }

the problem: is on second click all components get invisible but one keeps on screen

the component which stays on is a cardview

Mainlayout:

<RelativeLayout 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"
        tools:context="com.tomatedigital.giveawaymaster.activity.NewGiveawayActivity">
//lots of stuff//
      <include layout="@layout/giveaway" />

layout/giveaway is:

  <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="@dimen/giveawayCardHeight"


        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="4dp"
        card_view:cardUseCompatPadding="true">
        //lots of other stuf
</cardview>

It's the first thing i set visible on controller method but the only which doesn't go back invisible

REPEATING: there is no other calls to setVisibility other than these, all visibitilities are controled just under that method

I didn't post the whole activity code here because is way long

==========UPDATE==========

Just to clarify:
1- the cardview is one separated layout file re-used several places
2- there is only one cardview in the mainlayout
3
if i remove makeVisible(View.INVISIBLE)from onCreate(), all stuff stays visible,
if i call makeVisible(View.INVISIBLE) and never call makeVisible(View.VISIBLE) all stuff stays invisible
but if I invisble->visible->invisible everything goes invisible but cardview keeps visible

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105
  • try making it a child view rather than parent in the layout file – Ashutosh Sagar Aug 01 '18 at 04:08
  • I think the problem that you are facing is about elevations. Try to set the cardview visibility to gone and watch what happend. (Are you using a RelativeLayout as a parent, a Linear or what?) – Hamlet Leon Aug 01 '18 at 04:08
  • @AshutoshSagar it is a chield view, i pasted code just as example – Rafael Lima Aug 01 '18 at 04:10
  • @HamletLeon gone doesn't work either – Rafael Lima Aug 01 '18 at 04:11
  • That `CardView` appears to be in its own layout, and it looks like you've implemented your own custom `setContentView()` method. Given those, I suspect you might be building at least part of the UI yourself dynamically. Are you sure you don't have more than one of those `CardView`s in the `Activity`'s overall hierarchy? `findViewById()` will just return the first one it finds, so maybe it's returning one, possibly unseen, that isn't what you expect. You might try to examine that hierarchy with the [Layout Inspector](https://developer.android.com/studio/debug/layout-inspector). – Mike M. Aug 01 '18 at 04:11
  • My cardview has it's own layout, it is included in the mainlayout as `` – Rafael Lima Aug 01 '18 at 04:13
  • It still seems like you might be ending up with multiples, somehow, possibly due to your `setContentView()` implementation, or the layouts used. See what the Layout Inspector shows. – Mike M. Aug 01 '18 at 04:16
  • 1
    You are trying to put that include invisible? (do it directly to that include giving it an id when you set visibility of all @layout/giveaway) – Hamlet Leon Aug 01 '18 at 04:17
  • @MikeM. my custom `setContentView` comes from a supertype which most of my activities extend, it does simple calculate the free space in layout and inject a dinamic `Adview` into it, this method HAS NOT awareness of cardview – Rafael Lima Aug 01 '18 at 04:22
  • @HamletLeon it worked, if you want you can write as an answer and i mark as right.... Just to explain, the root of layout/giveaway.xml was a `LinearLayout` and it used to work fine. after changing it to cardview all this thing happened, seams there is a not standard behavior for this – Rafael Lima Aug 01 '18 at 04:27
  • There is a standard behavior, in that an `id` on an `` will override the `id` of the root `View` in the layout. If you had an `id` on the `` after making the `CardView` the root, then you've likely still got another `View` with ID `cardView` in the hierarchy somewhere; otherwise `findViewById(R.id.cardView).setVisibility(visi);` would've thrown a `NullPointerException`. You should still consider examining your hierarchy with Layout Inspector. Just a suggestion. – Mike M. Aug 01 '18 at 04:35
  • I will have a further look but still disagree about the standard behavior. Understand, i always had 2 layouts everything in the layouts was kept the same, but i changed the root of giveaway.xml from `linearlayout` to `cardview` simple because with linearlayout the `elevation` was not working... after that i got this weird behavior – Rafael Lima Aug 01 '18 at 05:03

1 Answers1

1

When you want to set the whole layout to Invisibility state, you need to do it in your include @layout/giveaway.xml. Becouse it is a view too.

Like we talk in comments...

Hamlet Leon
  • 427
  • 3
  • 9