1

I am trying to animate a text banner from outside of a linear layout to on screen of the linear layout at the click of a button but the notification text banner still remains on screen when the application starts.

My Activity with notification banner text

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="@color/white"
    android:clipChildren="false"
    android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary" >
    <ImageButton
        android:id="@+id/go_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:layout_alignLeft="@id/title"
        android:layout_alignParentLeft="true"
        android:background="@mipmap/back" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View Product"
        android:textStyle="bold"
        android:textSize="25sp"
        android:layout_centerInParent="true"
        android:paddingBottom="5dp"
        android:textAlignment="center"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textColor="@color/white"/>

    <include android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/title"
        android:layout_alignParentRight="true"
        layout="@layout/notification_layout"/>
</RelativeLayout>

<include android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/loader"/>

<include android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignRight="@id/title"
    android:layout_alignParentRight="true"
    layout="@layout/alert_banner"/>
<ImageView
    android:id="@+id/product_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    app:srcCompat="@drawable/slider_item_2" />

<TextView
    android:id="@+id/product_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Nike Air Mag - Limited Edition Sneakers"
    android:textStyle="bold"
    android:textSize="25sp"
    android:textAlignment="center"
    android:layout_margin="4dp"
    android:textColor="@color/print"/>

<Button
    android:id="@+id/add_to_cart"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableRight="@mipmap/add"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:layout_marginTop="30dp"
    android:textColor="@color/white"
    android:textStyle="bold"
    android:textSize="20sp"
    android:layout_marginBottom="10dp"
    android:background="@drawable/button_rounded_corners"
    android:paddingRight="10dp"
    android:text="Add to Cart" />

In my java class I start the animation with the following below

  //This is done when initializing the view
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
            product_image.getLayoutParams();
    params.setMargins(-1 * alert_info.getWidth(), 0, 0, 0);
    alert_info.setLayoutParams(params);

//when the button is clicked
ObjectAnimator animX = ObjectAnimator.ofFloat(alert_info,
                    View.TRANSLATION_X, -1* alert_info.getWidth(), 0);
            animX.setDuration(500);
            // alert_info.setVisibility(View.VISIBLE);
            animX.start();

but it does not work and the view still remains on screen when the application starts

Arun J
  • 687
  • 4
  • 14
  • 27
DaviesTobi alex
  • 610
  • 1
  • 9
  • 34

1 Answers1

3

Here is the code for the sliding animation for view.

inFromRightAnimation

private Animation inFromRightAnimation() {
    Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    inFromRight.setDuration(500);
    inFromRight.setInterpolator(new AccelerateInterpolator()); 
    return inFromRight; 
}

outToLeftAnimation

private Animation outToLeftAnimation() {
    Animation outtoLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoLeft.setDuration(500);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
} 

inFromLeftAnimation

private Animation inFromLeftAnimation() {
    Animation inFromLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    inFromLeft.setDuration(500);
    inFromLeft.setInterpolator(new AccelerateInterpolator());
    return inFromLeft;
}

outToRightAnimation

private Animation outToRightAnimation() {
    Animation outtoRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoRight.setDuration(500);
    outtoRight.setInterpolator(new AccelerateInterpolator());
    return outtoRight;
}

and now start Animation on view

View.startAnimation(inFromRightAnimation());
Gaurav Joseph
  • 927
  • 14
  • 25
Rohit Sharma
  • 1,384
  • 12
  • 19
  • Does it work if the view was originally hidden because that is an issue I face when starting the animation, usually it would not load the animation if the view was initially hidden because the view would need to be set to visible and the the animation would start. – DaviesTobi alex Mar 23 '18 at 16:48
  • Yes you need to set it to visible before starting – Rohit Sharma Mar 25 '18 at 01:42