How to set button not clickable while animating on Android? - android

How to make button not clickable while translate.animate is in progress. I tried with button.setclickable(false) and button.setEnabled(false) both dint worked for me.
Here is my code:
b2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if(i%2==0)
{
//b2.setClickable( false );
// Toast toast1 = Toast.makeText(getBaseContext(), "Success", 1);
// toast1.show();
if(i==0)
{
//b2.setOnClickListener(null);
b2.setEnabled(false);
//b2.setClickable(false);
int modifierY;
modifierY=-70;
Animation translateAnimation=new TranslateAnimation(0, 0, 0, modifierY);
translateAnimation.setDuration(600);
translateAnimation.setFillEnabled(true);
MyAnimationListener listener=new MyAnimationListener(b2, modifierY,SimpleViewPagerforAndroidActivity.this);
translateAnimation.setAnimationListener(listener);
// b2.setOnClickListener(null);
Animation translateAnimation1=new TranslateAnimation(0, 0, 0,0 );
translateAnimation1.setDuration(1000);
translateAnimation1.setFillEnabled(true);
MyAnimationListener listener1=new MyAnimationListener(main, 0,SimpleViewPagerforAndroidActivity.this);
translateAnimation1.setAnimationListener(listener1);
main.setVisibility(RelativeLayout.VISIBLE);
/* long time =AnimationUtils.currentAnimationTimeMillis();
main.invalidate();
b2.invalidate();
translateAnimation.setStartTime(time);
translateAnimation1.setStartTime(time);*/
main.startAnimation(translateAnimation1);
b2.startAnimation(translateAnimation);
i++;
// b2.setOnClickListener(this);
}
else
{
//b2.setOnClickListener(null);
b2.setEnabled(false);
//b2.setClickable(false);
int modifierY;
modifierY=-70;
Animation translateAnimation=new TranslateAnimation(0, 0, 0, modifierY);
translateAnimation.setDuration(1000);
translateAnimation.setFillEnabled(true);
MyAnimationListener listener=new MyAnimationListener(b2, modifierY,SimpleViewPagerforAndroidActivity.this);
translateAnimation.setAnimationListener(listener);
Animation translateAnimation1=new TranslateAnimation(0, 0, 0,-150 );
translateAnimation1.setDuration(600);
translateAnimation1.setFillEnabled(true);
MyAnimationListener listener1=new MyAnimationListener(main, -150,SimpleViewPagerforAndroidActivity.this);
translateAnimation1.setAnimationListener(listener1);
main.setVisibility(RelativeLayout.VISIBLE);
/* long time =AnimationUtils.currentAnimationTimeMillis();
main.invalidate();
b2.invalidate();
translateAnimation.setStartTime(time);
translateAnimation1.setStartTime(time);*/
main.startAnimation(translateAnimation1);
b2.startAnimation(translateAnimation);
// b2.setOnClickListener(this);
i++;
}
}
else
{
//b2.setOnClickListener(null);
b2.setEnabled(false);
//b2.setClickable(false);
Animation translateAnimation=new TranslateAnimation(0, 0, 0,150 );
translateAnimation.setDuration(1000);
translateAnimation.setFillEnabled(true);
MyAnimationListener listener=new MyAnimationListener(main, 150,SimpleViewPagerforAndroidActivity.this);
translateAnimation.setAnimationListener(listener);
Animation translateAnimation1=new TranslateAnimation(0, 0, 0,70 );
translateAnimation1.setDuration(600);
translateAnimation1.setFillEnabled(true);
MyAnimationListener listener1=new MyAnimationListener(b2, 70,SimpleViewPagerforAndroidActivity.this);
translateAnimation1.setAnimationListener(listener1);
/* long time =AnimationUtils.currentAnimationTimeMillis();
main.invalidate();
b2.invalidate();
translateAnimation.setStartTime(time);
translateAnimation1.setStartTime(time);
main.startAnimation(translateAnimation1);
b2.startAnimation(translateAnimation);*/
b2.startAnimation(translateAnimation1);
main.startAnimation(translateAnimation);
i++;
//b2.setOnClickListener(this);
}
}
});

Check on button click whether animation is completed or not, using hasEnd property, check below code:
if (!(animation.hasEnded()) {
button.setClickable(false);
}
else{
button.setClickable(true);
}

I use button.setEnabled(false) to disable Touch event.

First make the button selectable.
Then those things will work.
Button.setSelected(true);

Related

How to increase speed for ObjectAnimator onClick method?

I m trying to increase the speed (duration) for the setmButton when mButton is clicked, but no results.I need to make the mButton to move from x=0 to x=100 in less seconds so i will increase the speed. The mButon animation starts from x=0 and move to x=100 in duration=10000, when i click on it i want the mButton to increase the speed to the final position.
Thank you
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void animationA() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
final ImageButton mButton = (ImageButton)findViewById(R.id.Button );
assert mButton != null;
mButton.setBackgroundResource(R.drawable.Button);
ObjectAnimator mButtonAnimation= ObjectAnimator.ofFloat(mButton , "translationX", 0, -2100);
mButtonAnimation.setDuration(11000);
mButtonAnimation.setStartDelay(50);
mButtonAnimation.setRepeatCount(ObjectAnimator.INFINITE);
mButtonAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
ObjectAnimator mButtonAnimation1 = ObjectAnimator.ofFloat(mButton, "alpha", 0, 1);
mButtonAnimation1 .setDuration(90);
mButtonAnimation1 .setInterpolator(new AccelerateDecelerateInterpolator());
final AnimatorSet setmButton = new AnimatorSet();
setmButton .playTogether(mButtonAnimatiea, mButtonAnimation1 );
//setmButton .setFillAfter(true);
setmButton.start();
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Handler handlermButton = new Handler();
handlermButton.postDelayed(new Runnable() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
#Override
public void run() {
setmButton.setDuration(500);
}
}, 100);
}
});
}}, 100); }
You could use ACCELERATE interpolator if you want an higher speed level near from the end or simply set the duration to a lower value so the animation will be faster.

Why OnAnimationEnd moves the initial position of the button but also moves the button itself? #.#

I did some animations with regards to my created button. However, after the animation, the button is at its new position, but the onclick function is at its old initial position. I did the onAnimationEnd and set the button to the new position but the Button and the invisible-like button with the onclick function moves together. Any help will be awesome
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//move
TranslateAnimation animation = new TranslateAnimation(0.0f, ((widthPixels / 2) - 100),
0.0f, 0.0f);
animation.setDuration(1000);
animation.setRepeatCount(0);
animation.setRepeatMode(0);
animation.setFillEnabled(true);
animation.setFillAfter(true);
//fade
// Play.setVisibility(View.VISIBLE);
Play.startAnimation(animationfadein);
Play.startAnimation(animation);
Play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DreamTales.this, Stories.class);
startActivity(i);
}
});
}
}, DELAY);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Button Playghost = (Button) findViewById(R.id.PlayBtn);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)Play.getLayoutParams();
lp.leftMargin = (widthPixels/100); // use topmargin for the y-property, left margin for the x-property of your view
Playghost.setLayoutParams(lp);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Android moving buttons and background in runtime

I'm trying to make a tab look-A-like buttons row with a background that is an ImageView.
Here is the ImageView:
now on this image there are three buttons when the fragment loads.
when i click button one:
there are two situations:
1) when screen looks like the image above, then i want to move the buttons to the right so the screen looks like this:
and when it looks like the image above, i want it to return to the first image state.
the last case is when a user press's B3 i want it to react the same as B1 but to the other side, like this:
I tried so far lots of methods to try this, all have not worked, it seems that when i animate the movement the buttons stay in the same place:
here is my code so far:
btn_spa.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
isMapPressed=false;
isInfoPressed=false;
int toMoveNow=0;
if(!isSpaPressed)
{
if(isMapPressed)
toMoveNow=(toMove/2)+40;
else
toMoveNow=-(toMove/2+40);
int[] arr = new int[]{0,0};
btn_spa.getLocationOnScreen(arr);
Animation animation = new TranslateAnimation(arr[0], toMoveNow, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
//rel_buttons_wrapper.startAnimation(animation);
btn_info.startAnimation(animation);
btn_map.startAnimation(animation);
btn_spa.startAnimation(animation);
Toast.makeText(getActivity(), "Button spa clicked", Toast.LENGTH_SHORT).show();
}
}
});
btn_map.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
isSpaPressed=false;
isInfoPressed=false;
int toMoveNow=0;
if(!isMapPressed && isSpaPressed)
{
toMoveNow=-(toMove/2+40);
int[] arr = new int[]{0,0};
btn_map.getLocationOnScreen(arr);
Animation animation = new TranslateAnimation(arr[0], toMoveNow, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
// rel_buttons_wrapper.startAnimation(animation);
btn_info.startAnimation(animation);
btn_map.startAnimation(animation);
btn_spa.startAnimation(animation);
Toast.makeText(getActivity(), "Button map clicked", Toast.LENGTH_SHORT).show();
}
else
{
toMoveNow=(toMove/2+40);
int[] arr = new int[]{0,0};
btn_map.getLocationOnScreen(arr);
Animation animation = new TranslateAnimation(arr[0], toMoveNow, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
// rel_buttons_wrapper.startAnimation(animation);
btn_info.startAnimation(animation);
btn_map.startAnimation(animation);
btn_spa.startAnimation(animation);
Toast.makeText(getActivity(), "Button map clicked", Toast.LENGTH_SHORT).show();
}
}
});
btn_info.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
int toMoveNow=0;
if(!isInfoPressed && isSpaPressed)
{
toMoveNow=(toMove/2)+40;
int[] arr = new int[]{0,0};
btn_info.getLocationOnScreen(arr);
Animation animation = new TranslateAnimation(arr[0], toMoveNow, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
rel_buttons_wrapper.startAnimation(animation);
btn_info.startAnimation(animation);
btn_map.startAnimation(animation);
btn_spa.startAnimation(animation);
Toast.makeText(getActivity(), "Button info clicked", Toast.LENGTH_SHORT).show();
}
else
{
toMoveNow=-(toMove/2);
int[] arr = new int[]{0,0};
btn_info.getLocationOnScreen(arr);
Animation animation = new TranslateAnimation(arr[0], toMoveNow, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
rel_buttons_wrapper.startAnimation(animation);
btn_info.startAnimation(animation);
btn_map.startAnimation(animation);
btn_spa.startAnimation(animation);
Toast.makeText(getActivity(), "Button info clicked", Toast.LENGTH_SHORT).show();
}
isSpaPressed=false;
isMapPressed=false;
}
});
Sorry for the long question, I wanted to be as informative as possible.
any help would be good guys,
Kind Regards.
you can use ObjectAnimator class, it moves views and their bounds so they actually move when you move them, here is a tutorial :
http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator

how to pause the image when i click stop button

i have 2 buttons(start&stop) and 1 image when i click start button image will move from top to bottom and bottom to top, this is fine but the problem when i click stop button image is gone but i wont that.my requirement is image has to pause on the screen until i click start button. i am waiting for u r response
please help me and once execute my code u can understand clearly
public class MainActivity extends Activity {
RelativeLayout rl_footer;
ImageView iv_header;
boolean isBottom = true;
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl_footer = (RelativeLayout) findViewById(R.id.rl_footer);
iv_header = (ImageView) findViewById(R.id.iv_up_arrow);
Button button = (Button) findViewById(R.id.button1);
Button buttonstop = (Button) findViewById(R.id.button2);
buttonstop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
rl_footer.clearAnimation();
rl_footer.removeAllViews();
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv_header.setImageResource(R.drawable.delete);
iv_header.setPadding(0, 10, 0, 0);
// rl_footer.setBackgroundResource(R.drawable.download);
// if (isBottom) {
SlideToAbove();
// isBottom = false;
// } else {
// iv_header.setImageResource(R.drawable.delete);
// iv_header.setPadding(0, 0, 0,50);
// rl_footer.setBackgroundResource(R.drawable.download);
// SlideToDown();
// isBottom = true;
// }
}
});
}
public void SlideToAbove() {
Animation slide = null;
slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, -5.0f);
slide.setDuration(400);
slide.setFillAfter(true);
slide.setFillEnabled(true);
rl_footer.startAnimation(slide);
slide.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// rl_footer.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
rl_footer.getWidth(), rl_footer.getHeight());
// lp.setMargins(0, 0, 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rl_footer.setLayoutParams(lp);
iv_header.setImageResource(R.drawable.delete);
iv_header.setPadding(0, 0, 0, 50);
SlideToDown();
}
});
}
public void SlideToDown() {
Animation slide = null;
slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 5.2f);
slide.setDuration(400);
slide.setFillAfter(true);
slide.setFillEnabled(true);
rl_footer.startAnimation(slide);
slide.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// rl_footer.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
rl_footer.getWidth(), rl_footer.getHeight());
lp.setMargins(0, rl_footer.getWidth(), 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl_footer.setLayoutParams(lp);
iv_header.setImageResource(R.drawable.delete);
iv_header.setPadding(0, 10, 0, 0);
SlideToAbove();
}
});
}
}
xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="#+id/rl_footer"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true" >
<ImageView
android:id="#+id/iv_up_arrow"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingBottom="10dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/rl_footer"
android:layout_alignParentTop="true"
android:text="start" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="84dp"
android:text="stop" />
</RelativeLayout>
clearAnimation()
Cancels any animations for this view.
removeAllViews()
Call this method to remove all child views from the ViewGroup.
Try removing this line:
rl_footer.removeAllViews();
As others have said, don't call removeAllViews.
If you call clearAnimation, the View will go back to its original position.
If you don't call clearAnimation, the View will continue to animate.
What you need to do is to calculate the amount of time into the current animation cycle, then create a new Animation with a 0 duration, fillEnabled, fillBefore and fillAfter set to true and for the amount of translation to be equal to the amount that the current animation would have been offsetting the View.
rl_footer.removeAllViews();
"Call this method to remove all child views from the ViewGroup".
Comment this and the image shouldn't disappear.
Hope this helps.

Playing DISAPPEARING and APPEARING layout animation

I'm trying to use the new property animations to play layout animations when I'm toggling between two components in one container.
I simply call this code:
public void onClick(View v) {
if (componentOneVisible) {
componentOne.setVisibility(View.GONE);
componentTwo.setVisibility(View.VISIBLE);
} else {
componentTwo.setVisibility(View.GONE);
componentOne.setVisibility(View.VISIBLE);
}
componentOneVisible = !componentOneVisible;
}
My animations are set as following:
final ObjectAnimator testIn = ObjectAnimator.ofPropertyValuesHolder(
componentContainer,
PropertyValuesHolder.ofFloat("scaleX", 0, 1f),
PropertyValuesHolder.ofFloat("scaleY", 0, 1f),
PropertyValuesHolder.ofFloat("alpha", 0f, 1f));
testIn.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim).getTarget();
view.setScaleX(1f);
view.setScaleY(1f);
view.setAlpha(1f);
}
});
testIn.setDuration(3000);
final ObjectAnimator testOut = ObjectAnimator.ofPropertyValuesHolder(
componentContainer,
PropertyValuesHolder.ofFloat("scaleX", 1f, 0),
PropertyValuesHolder.ofFloat("scaleY", 1f, 0),
PropertyValuesHolder.ofFloat("alpha", 1, 0f));
testOut.setDuration(3000);
testOut.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim).getTarget();
view.setScaleX(0f);
view.setScaleY(0f);
view.setAlpha(0f);
}
});
mTransitioner.setAnimator(LayoutTransition.APPEARING, testIn);
mTransitioner.setAnimator(LayoutTransition.DISAPPEARING, testOut);
mTransitioner.setDuration(5000);
mTransitioner.setDuration(LayoutTransition.APPEARING, 3000);
mTransitioner.setDuration(LayoutTransition.DISAPPEARING, 3000);
For some reason only the appearing animation is played when the components are switched. What I'm hoping to see is to first see disappearing animation and then appearing animation.
I also call this code to set the transitioner
mTransitioner = new LayoutTransition();
componentContainer.setLayoutTransition(mTransitioner);
Any ideas?

Categories

Resources