0

I have a situation where a Button belonging to an Activity is always on top (visible) even after I add a fragment to a fragment container but once I change the element type to TextView it stays behind the new fragment like it should.

The element as TextView - stays behind the newly added fragment

<TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/openAdFilteringButton"
        android:text="Filters"
        android:onClick="openAdFiltering"
        android:layout_marginBottom="80dp"
        android:layout_gravity="center|bottom"/>

The element as Button - comes on top of the newly added fragment (stays visible)

<Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/openAdFilteringButton"
        android:text="Filters"
        android:onClick="openAdFiltering"
        android:layout_marginBottom="80dp"
        android:layout_gravity="center|bottom"/>

The element is a child of FrameLayout.

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

Note that this FrameLayout is the fragment container meaning I am adding the fragment to this FrameLayout:

FragmentTransaction.add(R.id.fragment_container, adFilteringFragment, "AdFilteringFragment").addToBackStack("AreaInterFrag").commit();

What do I need tell the button so that he will act like TextView and stay behind the fragment. Where does this difference in behavior (Button vs TextView) come from? Thanks!

KasparTr
  • 2,328
  • 5
  • 26
  • 55

1 Answers1

1

I suppose you are trying to hide the button by overlapping something to it. To achieve this kind of behavior and hide a view programmatically you'd better call

mView.setVisibility(View.INVISIBLE);

for what concerns the reason why buttons behave like that, try to read this and see if that can help you out: Button always displays on top in FrameLayout

Community
  • 1
  • 1
samugi
  • 395
  • 5
  • 17
  • 1
    Setting the button visibility is a not a good solution when you don't specifically need to do that. There are multiple cases to handle getting the button back (backstack change, fragment kill, etc). Your link answered the question and the solutions is to add this to the button: android:stateListAnimator="@null" – KasparTr Nov 13 '16 at 22:02
  • I'm glad that helped – samugi Nov 13 '16 at 22:05