How to implement slow popup showing in Android. Or simple Animation sample.

Published by Igor Khrupin on

Anybody know that cool Android animation need to be smooth.
For example. Your application need to show some popup with mediaplayer controls (play/pause buttons, etc….). How to implement this?

Below you can download source code of Android project

I will show you how I implemented this in one of my projects. Below you can see youtube video:

The activity’s source code is here:

package com.hrupin;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class SampleActivity extends Activity implements OnClickListener, AnimationListener {
	/** Called when the activity is first created. */

	private Animation popupShow;
	private Animation popupHide;

	private LinearLayout linearLayoutPopup;
	private Button buttonShowHidePopup;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		popupShow = AnimationUtils.loadAnimation(this, R.anim.popup_show);
		popupShow.setAnimationListener(this);
		popupHide = AnimationUtils.loadAnimation(this, R.anim.popup_hide);
		popupHide.setAnimationListener(this);

		linearLayoutPopup = (LinearLayout) findViewById(R.id.linearLayoutPopUp);
		linearLayoutPopup.setVisibility(View.GONE);

		buttonShowHidePopup = (Button) findViewById(R.id.buttonShowHidePopUp);
		buttonShowHidePopup.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (linearLayoutPopup.getVisibility() == View.GONE) {
			linearLayoutPopup.startAnimation(popupShow);
		} else {
			linearLayoutPopup.startAnimation(popupHide);
		}
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		if (animation.equals(popupShow)) {
			buttonShowHidePopup.setText(getString(R.string.btn_hide_txt));
		} else if (animation.equals(popupHide)) {
			buttonShowHidePopup.setText(getString(R.string.btn_show_txt));
		}
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationStart(Animation animation) {
		if (animation.equals(popupShow)) {
			linearLayoutPopup.setVisibility(View.VISIBLE);
		} else if (animation.equals(popupHide)) {
			linearLayoutPopup.setVisibility(View.GONE);
		}
	}
}

The code is pretty simple 🙂

You can download the source code from github

Download it from github

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.