Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button always displays on top in FrameLayout

I have FrameLayout like this:

<FrameLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">      <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:onClick="changeColor"         android:text="new button"/>      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="some text"/>  </FrameLayout> 

The problem is that the button is displayed on top while FrameLayout class overview tells us this: "Child views are drawn in a stack, with the most recently added child on top".

like image 305
Nokuap Avatar asked Aug 31 '15 08:08

Nokuap


People also ask

Why does FrameLayout hold one view?

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

What is the difference between relative layout and FrameLayout?

RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets. TableLayout : is a view that groups its child views into rows and columns. FrameLayout : is a placeholder on screen that is used to display a single ...

What is a relative layout?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

What is the difference between fragment and FrameLayout?

Show activity on this post. A framelayout, Relative View and a few others represents a view in android and is extended from viewgroup. A Fragment is a an an Object that is used to represent a portion of a user interface and is usually hosted in an activity. A fragment has a viewgroup which you can assign an XML layout.


2 Answers

This answer

Buttons in Lollipop and higher have a default elevation to them which causes them to always draw on top. You can change this by overriding the default StateListAnimator.

Try putting this into your button XML:

android:stateListAnimator="@null"

The FrameLayout should now cover the button.

like image 156
Rahul Tiwari Avatar answered Oct 11 '22 04:10

Rahul Tiwari


In the Android 5.0 (API 21) and above, you must add android:elevation into the view.

<FrameLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">      <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:onClick="changeColor"         android:text="new button"/>      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="some text"         android:elevation="3dp"/> 
like image 34
Khoa Vo Avatar answered Oct 11 '22 03:10

Khoa Vo