Search code examples
javaandroidanimationandroid-framelayoutz-order

How to make View above Button (min sdk: 17)


I want to make a notification at the top of my application with the appearance / hiding animation. For this, I created a FrameLayout (flNotification) with a nested TextView (tvNotificationText)

Animation works acceptable, but I have trouble with z order - FrameLayout shows behind Button

Bug screenshot: screenshot

What I need to change is that flNotification is displayed on top of the button.

Min sdk: 17


Show notification method

public void showNotification(String message) {
    tvNotificationText.setText(message);

    flNotification.bringToFront(); // doesn't help
    clRoot.invalidate(); // doesn't help

    Animation animation = AnimationUtils.loadAnimation(this, 
R.anim.notification_animation);
    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            flNotification.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            flNotification.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    flNotification.startAnimation(animation);
}

Layout XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/clRoot"
tools:context=".activities.MainActivity">

[...]
<Button
    android:id="@+id/btnNameAdd"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:text="+"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="@+id/etAge"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/etName" />

[...]
[...]
[...]

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/flNotification"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:visibility="gone"
    android:background="@color/errorNotificationBackground"
    android:clipChildren="false">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvNotificationText"
        android:layout_gravity="center"
        tools:text="Lorem ipsum dolor sit amet"
        android:textColor="@color/errorNotificationForeground"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp">

    </TextView>

</FrameLayout>
</android.support.constraint.ConstraintLayout>

Animation file

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillAfter="true">

<translate
    android:fromYDelta="-100%p"
    android:toYDelta="0%p"
    android:duration="750"/>

<translate
    android:startOffset="2500"
    android:fromYDelta="0%p"
    android:toYDelta="-100%p"
    android:duration="750"/>
</set>

Solution

  • Solved the problem by wrapping the button in FrameLayout. API 21+ is not used

    <FrameLayout
        android:id="@+id/flBtnAdd"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/etAge"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/etName">
    
        <Button
            android:id="@+id/btnNameAdd"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:text="+"
            android:textSize="30sp" />
    </FrameLayout>