Android 5.0 Android:Elevation Works for View, But Not Button

Android 5.0 android:elevation Works for View, but not Button?

The default Button style under Material has a StateListAnimator that controls the android:elevation and android:translationZ properties. You can remove the existing animator or set your own using the android:stateListAnimator property.

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

<Button
...
android:stateListAnimator="@anim/my_animator" />

The default animator is defined in button_state_list_anim_material.xml. Here is a sample showing the enabled and pressed states:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="@dimen/button_pressed_z_material"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType"/>
</set>
</item>
<!-- 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>

Android elevation not showing shadow on Button


The default Button style under Material has a StateListAnimator
that controls the android:elevation and android:translationZ
properties.

copied from here

just add this property to your Button. you can set your own using the android:stateListAnimator property.

android:stateListAnimator="@null"

full code :

        <Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:elevation="2dp"
android:translationZ="2dp"
android:stateListAnimator="@null"
android:background="@android:color/holo_green_light"
android:text="BUTTON">

UpDate :

for Understanding I set it 10dp..

xml code :

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">

<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:elevation="10dp"
android:translationZ="10dp"
android:stateListAnimator="@null"
android:background="@android:color/holo_green_light"
android:text="BUTTON"/>

</RelativeLayout>

Sample Image

Android elevation not showing a shadow

I've been playing around with shadows on Lollipop for a bit and this is what I've found:

  1. It appears that a parent ViewGroup's bounds cutoff the shadow of its children for some reason; and
  2. shadows set with android:elevation are cutoff by the View's bounds, not the bounds extended through the margin;
  3. the right way to get a child view to show shadow is to set padding on the parent and set android:clipToPadding="false" on that parent.

Here's my suggestion to you based on what I know:

  1. Set your top-level RelativeLayout to have padding equal to the margins you've set on the relative layout that you want to show shadow;
  2. set android:clipToPadding="false" on the same RelativeLayout;
  3. Remove the margin from the RelativeLayout that also has elevation set;
  4. [EDIT] you may also need to set a non-transparent background color on the child layout that needs elevation.

At the end of the day, your top-level relative layout should look like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/block"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/lightgray"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"
>

The interior relative layout should look like this:

<RelativeLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="[some non-transparent color]"
android:elevation="30dp"
>

Elevation above 2dp causes buttons to disappear

The following reflects this Stack Overflow question and the accepted answer.

Let's consider what you start off with. I don't have access to all your dimensions, etc. to make the following look exactly like your example but the basis is there. Below is the base layout. You can see that the button lies beneath the view even though the view has an elevation of 8dp and the button has an elevation of 10dp.

Sample Image

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">

<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="175dp"
android:layout_alignBottom="@+id/button"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_margin="30dp"
android:background="@android:color/white"
android:elevation="8dp">

</View>

<Button
android:id="@+id/button"
android:layout_width="190dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:background="@mipmap/ic_launcher"
android:elevation="10dp"
android:text="LOGIN"
android:textColor="@color/white"
android:textSize="20sp" />

</RelativeLayout>

</LinearLayout>

So, why is this? It is because android:elevation="10dp" in the XML for the button isn't really defining the elevation. Instead, the button's elevation is controlled by a "StateListAnimator" that controls the android:elevation and android:translationZ properties." (See accepted answer to above-referenced question.)

If you don't really care for what the StateListAnimator is doing for your layout, you can simply eliminate it by adding android:stateListAnimator="@null" to the XML for the button. Here is what the layout looks like after adding this code. (Please note that the designer may not show the changes immediately; Android Studio 3.0 Beta 6 didn't). So, you may need to refresh the layout to see the changes.

Sample Image

As you can see, the button is now above the view as we expect.

If you do care what StateListAnimator does for you, you will need to define your own and enter its name instead of @null in the XML statement changed above. The answer I reference above has the StateListAnimator that Android presumably uses, so you can take that as a basis and change the elevation to what you prefer. I haven't tried this, but it should work.

Android 5.0 - ProgressBar cannot be displayed over a Button

You can add the android:translationZ attribute to the ProgressBar:

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="50dp"
android:layout_height="50dp"
android:translationZ="2dp"
android:layout_centerInParent="true"/>

Android button elevation shadow not working

Use below the line of code to show an elevation in button

android:outlineProvider="bounds" – K. Sopheak

That works, thanks!

android:elevation doesn't work in L preview

CardView sets its own elevation during initialization, which will override whatever you set from XML. You should file this as a bug at https://code.google.com/p/android-developer-preview/wiki/FilingIssues?tm=3

@Override
public void initialize(CardViewDelegate cardView, Context context, int backgroundColor,
float radius) {
cardView.setBackgroundDrawable(new RoundRectDrawable(backgroundColor, radius));
View view = (View) cardView;
view.setClipToOutline(true);
view.setElevation(context.getResources().getDimension(R.dimen.cardview_elevation));
}

Elevation not working

For android version 5.0 & above try the Elevation for other views..

android:elevation="10dp"

For Buttons,

android:stateListAnimator="@anim/button_state_list_animator"

button_state_list_animator.xml





<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:state_enabled="true">

<set>

<objectAnimator android:propertyName="translationZ"

android:duration="@integer/button_pressed_animation_duration"

android:valueTo="@dimen/button_pressed_z_material"

android:valueType="floatType"/>

<objectAnimator android:propertyName="elevation"

android:duration="0"

android:valueTo="@dimen/button_elevation_material"

android:valueType="floatType"/>

</set>

</item>

<!-- 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>

<item>

<set>

<objectAnimator android:propertyName="translationZ"

android:duration="0"

android:valueTo="0"

android:valueType="floatType"/>

<objectAnimator android:propertyName="elevation"

android:duration="0"

android:valueTo="0"

android:valueType="floatType"/>

</set>

</item>

</selector>

android:Elevation not respected across FragmentTransactions

Shadows respect the elevation of siblings, but not cousins. The action bar and the SlidingTabLayout are not siblings, so the action bar casts a shadow on the SlidingTabLayout.

You'll need to customize the action bar to remove the elevation if you don't want the shadow.



Related Topics



Leave a reply



Submit