Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get animation in android?

Actually I have an Linear Layout which is matchParent(in width and height), and now I need to create a another layout(below that linearLayout) which must not be visible initially.

And when my activity will run then I want to animate that hided layout.

The hided layout must be come from below to up side. I don't know how to achieve this animation ?? I don't know how to create the layout which must not be visible initially and after a delay it must show from below of the screen and come up side ???

here is my code of xml file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/imageupLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imgview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/logo" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/hided_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#56a24b"
        android:orientation="vertical"
        android:visibility="visible" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="15dip"
            android:background="@drawable/btn_bg"
            android:gravity="center" >

            <Button
                android:id="@+id/btn_login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="2dip"
                android:background="@drawable/btn_login" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dip"
            android:background="@drawable/btn_bg"
            android:gravity="center" >

            <Button
                android:id="@+id/btn_register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="2dip"
                android:background="@drawable/btn_register" />
        </LinearLayout>

        <TextView
            android:id="@+id/tv_just_explore"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dip"
            android:layout_marginTop="20dip"
            android:gravity="center"
            android:text="JUST EXPLORE"
            android:textColor="#FFFFFF"
            android:textSize="15dip"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

like image 642
Amit Jayaswal Avatar asked Feb 11 '23 12:02

Amit Jayaswal


2 Answers

Yes I did it. Here is my code, may be it help others...

In my MainActivity.

public class MainActivity extends ActionBarActivity {

 private View hiddenPanel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    hiddenPanel = findViewById(R.id.hidden_panel);
        }

 public void slideUpDown(final View view) {
        if (!isPanelShown()) {
            // Show the panel
            Animation bottomUp = AnimationUtils.loadAnimation(this,
                    R.anim.bottom_up);

            hiddenPanel.startAnimation(bottomUp);
            hiddenPanel.setVisibility(View.VISIBLE);
        }
        else {
            // Hide the Panel
            Animation bottomDown = AnimationUtils.loadAnimation(this,
                    R.anim.bottom_down);

            hiddenPanel.startAnimation(bottomDown);
            hiddenPanel.setVisibility(View.GONE);
        }
    }

    private boolean isPanelShown() {
        return hiddenPanel.getVisibility() == View.VISIBLE;
    }

}

In my activity_main

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="@string/hello_world" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/hello_world" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:onClick="slideUpDown"
    android:text="Slide up / down" />

<RelativeLayout
    android:id="@+id/hidden_panel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:visibility="gone" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:layout_centerInParent="true"
        android:onClick="slideUpDown" />
</RelativeLayout>

bottom_down.xml

<?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="http://schemas.android.com/apk/res/android">
 <translate 
android:fromYDelta="0%p" 
android:toYDelta="100%p" 
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator"
android:duration="500" />
</set>

bottom_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate 
   android:fromYDelta="75%p"
   android:toYDelta="0%p"
   android:fillAfter="true"
   android:duration="500" />
 </set>
like image 74
Amit Jayaswal Avatar answered Feb 23 '23 14:02

Amit Jayaswal


You have not mentioned which layout in your code you want to hide and bring another layout. But I will give you a general code which you can apply. First for the layout you want to hide initially, set android:visibility = "gone" and in your activity while animating use below

layoutYouHideInitially.setVisibility(View.VISIBLE);
layoutYouHideInitially.animate().alpha(1).setDuration(500);
layoutYouWannaHide.setVisibility(View.GONE);
layoutYouWannaHide.animate().alpha(0).setDuration(500);

Between setting visibility GONE or INVISIBLE it's upto you based on Usage. And for animating you have other options too which you can explore in place of alpha like translationX, scaleX, etc. Please comment if you get stuck at any problem. Happy to help!

like image 31
himanshu1496 Avatar answered Feb 23 '23 14:02

himanshu1496