I have a FrameLayout in which I put the Button and FrameLayout in order to overlap with the image (future animation, therefore FrameLayout ) button. The preview displays everything correctly:

enter image description here

However, at startup, the button overlaps the "FrameLayout":

enter image description here

Here is the markup:

  <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.25"> <Button android:id="@+id/button_auth" android:layout_width="match_parent" android:layout_height="match_parent" /> <FrameLayout android:layout_width="36dp" android:layout_height="36dp" android:layout_gravity="center" android:background="@drawable/ic_refresh_36dp_1" /> </FrameLayout> 

Why Button overlaps FrameLayout (although with a similar ... construction, in the same markup, this was not. And how to place FrameLayout on top of Button ?

    2 answers 2

    The whole point here is in a button attribute like android:elevation .

    A button in API> 21 has the android:stateListAnimator , which by default refers to the button_state_list_anim_material.xml file with the following content :

     <?xml version="1.0" encoding="utf-8"?> ... <!-- base state --> <item android:state_enabled="true"> <set> <objectAnimator android:propertyName="translationZ" android:duration="@integer/button_pressed_animation_duration" android:valueTo="0" android:startDelay="@integer/button_pressed_animation_delay" android:valueType="floatType"/> <objectAnimator android:propertyName="elevation" android:duration="0" android:valueTo="@dimen/button_elevation_material" android:valueType="floatType" /> </set> </item> ... </selector> 

    We see that in the normal state_enabled state, the elevation attribute has the value @dimen/button_elevation_material , which is 2dp .

    Conclusion: if we want a view overlap a button, you need (view!) To use android:elevation > = 2dp .

    • @ bukashka101 Well, firstly, I also recently had such a problem, I resolved it with a crutch, then I saw your question and decided to figure it out ... I searched for a query like "android button above appear view" and since the problem is inherent only for API> 21, then put the Google filter to display the results only за год ... the very first link resulted in enSO where in the first answer there was a link to the detailed info - stackoverflow.com/a/27112143/4728929 - ermak0ff

    Wrap the button and frame additionally with separate layatami, I already caught such a bug at myself, when I didn’t see the view that had to be painted over the Button on Android 5.0 and higher. Not yet got to the cause, but it was crutally decided like this.

      <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.25"> <RelativeLayout .....> <Button android:id="@+id/button_auth" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> <RelativeLayout .....> <FrameLayout android:layout_width="36dp" android:layout_height="36dp" android:layout_gravity="center" android:background="@drawable/ic_refresh_36dp_1" /> </RelativeLayout> </FrameLayout> 

    Maybe it just came to me ... AppCompat redefines the standard widgets when I create them and puts my own (AppCompatButton, AppCompatTextView, etc. - I noticed during the debug), since they delete and add, the Button widget will have an index more than FrameLayout, and, accordingly, higher in the hierarchy and as a result after all these Google scams - the Button widget lies on top of everything else in the general container. Correct me in the comments if this is nonsense.

    • In your assumption there was a grain of truth, but you did not get to the real problem. The answer above explains what exactly happened :). - user189127