Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong render order for button when material theme is applied

Button widget draws on top of any other widget no matter what the layout structure is. It repeats in both RelativeLayout and FrameLayout when Material Dark/Light theme is applied. Check out the screenshots below for better illustration of this strange behaviour.

Checked on Nexus 4 and Nexus 5. However I doubt it is related to devices.

How it looks in Android Studio

How it looks on my Nexus 4

like image 384
Sergey Dmitriev Avatar asked Jan 23 '15 08:01

Sergey Dmitriev


People also ask

What is material button in Android?

Material Button is a customizable button component with updated visual styles. This button component has several built-in styles to support different levels of emphasis, as typically any UI will contain a few different buttons to indicate different actions.

What is button discuss the working of Button with example?

In Android, Button represents a push button. A Push buttons can be clicked, or pressed by the user to perform an action. There are different types of buttons used in android such as CompoundButton, ToggleButton, RadioButton. Button is a subclass of TextView class and compound button is the subclass of Button class.


2 Answers

From Lollipop (API 21) onwards, Android applies elevation / Z animation to buttons by default. To avoid this, add the following to your Button XML:

<Button
    ...
    android:stateListAnimator="@null"
/>

This will make the button respect its Z-index.

Source

like image 103
Maksim Ivanov Avatar answered Oct 29 '22 04:10

Maksim Ivanov


Android 5.0 Lollipop along with Material Design introduced new property to specify the elevation (Z-index) of widgets. It is described here.

To draw the view over the button you can add android:elevation="1dp" to the View

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Don't look so deep"
    />
<View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#C00"
    android:elevation="1dp"
    />

Following is part of earlier answer to misunderstood question, kept for future reference

With RelativeLayout you have to specify the position of elements relative to other elements.

So say you want to have the View below the button, you'll have to add id's to the elements and specify that the view is below the button:

<Button
    android:id="+id/myactivity_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Don't look so deep"
    />
<View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#C00"
    android:layout_below="@id/myactivity_button"
    />

Check out the Android Developer Guide for RelativeLayout and the available LayoutParameters for RelativeLayouts


FrameLayout is usually not good for organize multiple components. The FrameLayout is designed to block out an area on the screen to display a single item. The position of the FrameLayout childs can be controlled by using the android:layout_gravity attribute.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:text="Don't look so deep"
    />
<View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#C00"
    android:layout_gravity="bottom"
    />

Check out the Android docs for FrameLayout and the available parameters for the layout_gravity

like image 44
arnfada Avatar answered Oct 29 '22 04:10

arnfada