This is a demo app for animations that i am making. It's an application with simple image which should react on button click like rotate, zoom in, zoom out, fade in/out, etc, but instead it crashes, i can't possibly find what can be the problem, i have searched all over the internet for solutions, but with no luck, here is the code:
public class MainActivity extends AppCompatActivity implements
View.OnClickListener, Animation.AnimationListener {
#Override
public void onAnimationEnd(Animation animation) {
textStatus.setText("STOPPED");
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
textStatus.setText("RUNNING");
}
Animation animFadeIn;
Animation animFadeOut;
Animation animFadeInOut;
Animation animZoomIn;
Animation animZoomOut;
Animation animLeftRight;
Animation animRightLeft;
Animation animTopBottom;
Animation animBounce;
Animation animFlash;
Animation animRotateLeft;
Animation animRotateRight;
ImageView imageView;
TextView textStatus;
Button btnFadeIn;
Button btnFadeOut;
Button btnFadeInOut;
Button zoomIn;
Button zoomOut;
Button leftRight;
Button rightLeft;
Button topBottom;
Button bounce;
Button flash;
Button rotateLeft;
Button rotateRight;
SeekBar seekBarSpeed;
TextView textSeekerSpeed;
int seekSpeedProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadAnimations();
loadUI();
}
private void loadAnimations() {
animFadeIn = AnimationUtils.loadAnimation(this,R.anim.fade_in);
animFadeIn.setAnimationListener(this);
animFadeOut = AnimationUtils.loadAnimation(this,R.anim.fade_out);
animFadeInOut = AnimationUtils.loadAnimation(this, R.anim.fade_in_out);
animZoomIn = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
animZoomOut = AnimationUtils.loadAnimation(this,R.anim.zoom_out);
animLeftRight = AnimationUtils.loadAnimation(this,R.anim.left_right);
animRightLeft = AnimationUtils.loadAnimation(this, R.anim.right_left);
animTopBottom = AnimationUtils.loadAnimation(this, R.anim.top_bot);
animBounce = AnimationUtils.loadAnimation(this,R.anim.bounce);
animFlash = AnimationUtils.loadAnimation(this, R.anim.flash);
animRotateLeft = AnimationUtils.loadAnimation(this, R.anim.rotate_left);
animRotateRight = AnimationUtils.loadAnimation(this, R.anim.rotate_right);
}
private void loadUI() {
imageView = (ImageView) findViewById(R.id.imageView);
textSeekerSpeed = (TextView) findViewById(R.id.textStatus);
btnFadeIn = (Button) findViewById(R.id.btnFadeIn);
btnFadeOut = (Button) findViewById(R.id.btnFadeOut);
btnFadeInOut = (Button) findViewById(R.id.btnFadeInOut);
zoomIn = (Button) findViewById(R.id.btnZoomIn);
zoomOut = (Button) findViewById(R.id.btnZoomOut);
leftRight = (Button) findViewById(R.id.btnLeftRight);
rightLeft = (Button) findViewById(R.id.btnRightLeft);
topBottom = (Button) findViewById(R.id.btnTopBottom);
bounce = (Button) findViewById(R.id.btnBounce);
flash = (Button) findViewById(R.id.btnFlash);
rotateLeft = (Button) findViewById(R.id.btnRotateLeft);
rotateRight = (Button) findViewById(R.id.btnRotateRight);
zoomIn.setOnClickListener(this);
zoomOut.setOnClickListener(this);
leftRight.setOnClickListener(this);
rightLeft.setOnClickListener(this);
topBottom.setOnClickListener(this);
bounce.setOnClickListener(this);
flash.setOnClickListener(this);
rotateLeft.setOnClickListener(this);
rotateRight.setOnClickListener(this);
seekBarSpeed = (SeekBar) findViewById(R.id.seekBarSpeed);
textSeekerSpeed = (TextView) findViewById(R.id.textSeekerSpeed);
seekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int value, boolean fromUser) {
seekSpeedProgress = value;
textSeekerSpeed.setText("" +seekSpeedProgress + "of" +seekBarSpeed.getMax());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnFadeIn:
animFadeIn.setDuration(seekSpeedProgress);
animFadeIn.setAnimationListener(this);
imageView.startAnimation(animFadeIn);
break;
case R.id.btnFadeOut:
animFadeOut.setDuration(seekSpeedProgress);
animFadeOut.setAnimationListener(this);
imageView.startAnimation(animFadeOut);
break;
case R.id.btnFadeInOut:
animFadeInOut.setDuration(seekSpeedProgress);
animFadeInOut.setAnimationListener(this);
imageView.startAnimation(animFadeInOut);
break;
case R.id.btnZoomIn:
animZoomIn.setDuration(seekSpeedProgress);
animZoomIn.setAnimationListener(this);
imageView.startAnimation(animZoomIn);
break;
case R.id.btnZoomOut:
animZoomOut.setDuration(seekSpeedProgress);
animZoomOut.setAnimationListener(this);
imageView.startAnimation(animZoomOut);
break;
case R.id.btnLeftRight:
animLeftRight.setDuration(seekSpeedProgress);
animLeftRight.setAnimationListener(this);
imageView.startAnimation(animLeftRight);
break;
case R.id.btnRightLeft:
animRightLeft.setDuration(seekSpeedProgress);
animRightLeft.setAnimationListener(this);
imageView.startAnimation(animRightLeft);
break;
case R.id.btnBounce:
animBounce.setDuration(seekSpeedProgress/10);
animBounce.setAnimationListener(this);
imageView.startAnimation(animBounce);
break;
case R.id.btnFlash:
animFlash.setDuration(seekSpeedProgress/10);
animFlash.setAnimationListener(this);
imageView.startAnimation(animFlash);
break;
case R.id.btnRotateLeft:
animRotateLeft.setDuration(seekSpeedProgress);
animRotateLeft.setAnimationListener(this);
imageView.startAnimation(animRotateLeft);
break;
case R.id.btnRotateRight:
animRotateRight.setDuration(seekSpeedProgress);
animRotateRight.setAnimationListener(this);
imageView.startAnimation(animRotateRight);
break;
}
}
}
Here is the crashlog:
2020-12-09 22:48:57.075 8696-8696/com.example.animationdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.animationdemo, PID: 8696
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.animationdemo.MainActivity.onAnimationStart(MainActivity.java:31)
at android.view.animation.Animation.dispatchAnimationStart(Animation.java:999)
at android.view.animation.AnimationSet.getTransformation(AnimationSet.java:392)
at android.view.animation.Animation.getTransformation(Animation.java:1030)
at android.view.View.applyLegacyAnimation(View.java:21166)
at android.view.View.draw(View.java:21288)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at androidx.constraintlayout.widget.ConstraintLayout.dispatchDraw(ConstraintLayout.java:1975)
at android.view.View.updateDisplayListIfDirty(View.java:20474)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4428)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4401)
at android.view.View.updateDisplayListIfDirty(View.java:20441)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4428)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4401)
at android.view.View.updateDisplayListIfDirty(View.java:20441)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4428)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4401)
at android.view.View.updateDisplayListIfDirty(View.java:20441)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4428)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4401)
at android.view.View.updateDisplayListIfDirty(View.java:20441)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4428)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4401)
at android.view.View.updateDisplayListIfDirty(View.java:20441)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:584)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:590)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:668)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3840)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3648)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2954)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1857)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8089)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1057)
at android.view.Choreographer.doCallbacks(Choreographer.java:875)
at android.view.Choreographer.doFrame(Choreographer.java:776)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1042)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
The problem is that you are try to access the setText method of textStatus view when you have not initialised it.
From your code, you didn't initialize it any where. I believe
textSeekerSpeed = (TextView) findViewById(R.id.textStatus);
supposed to be where you initialize textStatus.
try replacing
textSeekerSpeed = (TextView) findViewById(R.id.textStatus);
with
textStatus = (TextView) findViewById(R.id.textStatus);
then set textSeekerSpeed with its corresponding TextView. it should solve your problem
Change
textSeekerSpeed = (TextView) findViewById(R.id.textStatus);
to
textStatus = (TextView) findViewById(R.id.textStatus);
You textStatus was never mapped so it was always null and caused a crash.
Related
I know that was already asked but it is outdated:
I have 2 buttons that represent 2 choices and if one is selected the background color gets changed to yellow. But if i want to change the choice i need to somehow reset the button:
I already try to set it back but some old design comes out. Can you provide me the id of the modern button style? And show me how to implement it?
int myChoice;
if (view == findViewById(R.id.choice1)){
myChoice = 1;
choice1.setBackgroundColor(getResources().getColor(R.color.highlightButton));
choice2.setBackgroundResource(android.R.drawable.btn_default);
}
else if (view == findViewById(R.id.choice2)){
myChoice = 2;
choice2.setBackgroundColor(getResources().getColor(R.color.highlightButton));
choice1.setBackgroundResource(android.R.drawable.btn_default);
}
}
Use Tags with getBackground(). This will assure you are always setting back to original.
Add following in beginning of function
if (v.getTag() == null)
v.setTag(v.getBackground());
Then instead of setBackgroundResource, use
v.setBackground(v.getTag());
Starting from here, you can store the default color of the button into a Drawable and grab the selection color (Yellow in your case) into anther Drawable, then toggle background colors of buttons with these Drawable variables
please check below demo
public class MainActivity extends AppCompatActivity {
private Drawable mDefaultButtonColor;
private Drawable mSelectedButtonColor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn1 = findViewById(R.id.btn1);
final Button btn2 = findViewById(R.id.btn2);
mDefaultButtonColor = (btn1.getBackground());
mSelectedButtonColor = ContextCompat.getDrawable(this, R.color.buttonSelected);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleButton(btn1, true);
toggleButton(btn2, false);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleButton(btn1, false);
toggleButton(btn2, true);
}
});
}
private void toggleButton(Button button, boolean isSelected) {
button.setBackground(isSelected ? mSelectedButtonColor : mDefaultButtonColor);
}
}
at the click of the button, I want to create EDITTEXT dynamically and display them vertically. I have this code but it just creates one EditText. Where am I wrong? Thanks for your help.
private LinearLayout containerLayout;
static int totalEditTexts = 0;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_category);
containerLayout = (LinearLayout)findViewById(R.id.relative1);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
totalEditTexts++;
EditText editText = new EditText(getBaseContext());
containerLayout.addView(editText);
editText.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) editText.getLayoutParams();
layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
layoutParams.setMargins(23, 34, 0, 0);
editText.setLayoutParams(layoutParams);
editText.setTag("EditText" + totalEditTexts);
}
});
}
Your example works perfectly if your add_category.xml were to look like this:
https://gist.github.com/akodiakson/9d37e3e48d0798d106c3
So I'm guessing your Activity layout might be a little bit off.
I have just started Android Studio and am also a little new to java, so please excuse the inefficient code.
Anyway, when I click on the button on the main activity on my app to take the user to the activity that I want to display the images, my app stops working and it throws an OutOfMemoryError.
Here is MainActivty.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button presidentQuizButton = (Button) findViewById(R.id.presidentQuizButton);
Button reviewPresidentsButton = (Button) findViewById(R.id.reviewPresidentsButton);
presidentQuizButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//when button is clicked, do something
}
});
reviewPresidentsButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//when button is clicked, do something
startActivity(new Intent(MainActivity.this, presidentReviewActivity.class));
}
});
}
Here is presidentReviewActivity.java:
ImageView imageView;
public int presidentNumber = 0;
private Drawable drawable;
private Drawable [] drawables = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_president_review);
Button menuButton = (Button) findViewById(R.id.menuButton);
Button nextButton = (Button) findViewById(R.id.nextButton);
Button backButton = (Button) findViewById(R.id.backButton);
drawables = new Drawable[]{
getResources().getDrawable(R.drawable.president1),
getResources().getDrawable(R.drawable.president2),
getResources().getDrawable(R.drawable.president3),
getResources().getDrawable(R.drawable.president4),
getResources().getDrawable(R.drawable.president5),
getResources().getDrawable(R.drawable.president6),
getResources().getDrawable(R.drawable.president7),
getResources().getDrawable(R.drawable.president8),
getResources().getDrawable(R.drawable.president9),
getResources().getDrawable(R.drawable.president10),
getResources().getDrawable(R.drawable.president11),
getResources().getDrawable(R.drawable.president12),
getResources().getDrawable(R.drawable.president13),
getResources().getDrawable(R.drawable.president14),
getResources().getDrawable(R.drawable.president15),
getResources().getDrawable(R.drawable.president16),
getResources().getDrawable(R.drawable.president17),
getResources().getDrawable(R.drawable.president18),
getResources().getDrawable(R.drawable.president19),
getResources().getDrawable(R.drawable.president20),
getResources().getDrawable(R.drawable.president21),
getResources().getDrawable(R.drawable.president22),
getResources().getDrawable(R.drawable.president23),
getResources().getDrawable(R.drawable.president24),
getResources().getDrawable(R.drawable.president25),
getResources().getDrawable(R.drawable.president26),
getResources().getDrawable(R.drawable.president27),
getResources().getDrawable(R.drawable.president28),
getResources().getDrawable(R.drawable.president29),
getResources().getDrawable(R.drawable.president30),
getResources().getDrawable(R.drawable.president31),
getResources().getDrawable(R.drawable.president32),
getResources().getDrawable(R.drawable.president33),
getResources().getDrawable(R.drawable.president34),
getResources().getDrawable(R.drawable.president35),
getResources().getDrawable(R.drawable.president36),
getResources().getDrawable(R.drawable.president37),
getResources().getDrawable(R.drawable.president38),
getResources().getDrawable(R.drawable.president39),
getResources().getDrawable(R.drawable.president40),
getResources().getDrawable(R.drawable.president41),
getResources().getDrawable(R.drawable.president42),
getResources().getDrawable(R.drawable.president43),
getResources().getDrawable(R.drawable.president44),
};
menuButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
presidentNumber++;
drawable = drawables[presidentNumber];
imageView.setImageDrawable(drawable);
}
});
And here is most of the error log:
http://imgur.com/xzFfPrq
Instead of store all the drawable content in an array. You can actually just store the resource id in a array.
replace private Drawable [] drawables = null; with private int[] drawableids
replace drawables = new Drawable[]{...} to drawableids = new int[]{...}
replace
drawable = drawables[presidentNumber];
imageView.setImageDrawable(drawable);
to
drawable = getResource().getDrawable(drawableids[presidentNumber]);
imageView.setImageDrawable(drawable);
Try to change as above steps and try again.
The nullpointerexception may be caused by you did not init the drawableids array or you are still using the drawables array instead of drawableids.
you are trying to load large size images directly into memory which causes out of memory exceptions,this issue is discussed in details and a nice solution is given in developers site
I'm going through the Sams Android Development book, and I put an animation code and a code to move it to the next screen. I tested it on my phone and the AVD and it's not working. Here's the code:
public class QuizSplashActivity extends QuizActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
TextView logo1 = (TextView) findViewById(R.id.TextViewTopTitle);
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade1);
TextView logo2 = (TextView) findViewById(R.id.BottomView1);
Animation fade3 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade3);
Animation spinin= AnimationUtils.loadAnimation(this, R.anim.custom_anim);
LayoutAnimationController controller = new LayoutAnimationController(spinin);
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); {
for (int i = 0; i < table.getChildCount(); i++) {
TableRow row = (TableRow) table.getChildAt(i);
row.setLayoutAnimation(controller);
}
}
Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
fade2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation){
startActivity(new Intent(QuizSplashActivity.this, QuizMenuActivity.class));
QuizSplashActivity.this.finish();
}
public void onAnimationStart(Animation a) { }
public void onAnimationRepeat(Animation a) { }
});
}
#Override
protected void onPause() {
super.onPause();
TextView logo1= (TextView) findViewById(R.id.TextViewTopTitle);
logo1.clearAnimation();
TextView logo2= (TextView) findViewById(R.id.BottomView1);
logo2.clearAnimation();
}
}
`
Please help, I want to move onto the next chapter.
Again, if I run this code, the animation doesn't run and the app doesn't move onto the next screen.
Thanks
First of all, onAnimationEnd of fade2 is where you start your next Activity but I don't see you using it anywhere.
I think you've got confused about which views you want to animate and which animations to use here:
TextView logo1 = (TextView) findViewById(R.id.TextViewTopTitle);
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade1); // <----- is this right?
TextView logo2 = (TextView) findViewById(R.id.BottomView1);
Animation fade3 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade3); // <----- and this one?
Also, onAnimationEnd is not reliable. You can take a look at setRepeatCount() and setRepeatMode() and play around with those but there are multiple bugs with them and worse, those bugs vary between Android version.
Instead, you can use a delayed Runnable to do your "post animation" work:
new Handler().postDelayed(new Runnable() {
public void run() {
view.clearAnimation(); // <--- whichever view you are animating
startActivity(new Intent(QuizSplashActivity.this, QuizMenuActivity.class));
QuizSplashActivity.this.finish();
}
}, fade2.getDuration());
This runnable will be delayed for however long fade2 is set to animate for. When it fires, it clears the current animation and starts the activity.
Good luck!
I have a basic animation of a button after it is pressed currently in my application. After the button finishes animating, I can no longer click on it. It doesn't even press with an orange highlight.
Any help?
Here's my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
animation = new AnimationSet(true);
animation.setFillAfter(true);
Animation translate = 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);
translate.setDuration(500);
animation.addAnimation(translate);
LayoutAnimationController controller = new LayoutAnimationController(animation, 0.25f);
generate = (Button)findViewById(R.id.Button01);
generate.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
keyFromTop();
}
});
}
public void keyFromTop(){
generate.setAnimation(animation);
}
Animations affect only the drawing of widgets, which means after the animation is done, your button is still at its previous location. You need to manually update the layout parameters of your button if you want to move it to the new location. Also, your AnimationSet and your AnimationController are useless.
If you want to be able to click the button after the animation is complete, you have to manually move the component.
Here's an example of a translate animation applied to a button:
public class Test2XAnimation extends Activity {
private RelativeLayout buttonContainer;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
buttonContainer = (RelativeLayout) button.getParent();
}
public void startTapped(View view) {
animateButton(200, 100);
}
public void buttonTapped(View view) {
Toast.makeText(this, "tap", Toast.LENGTH_SHORT).show();
}
private RelativeLayout.LayoutParams params;
private CharSequence title;
private void animateButton(final int translateX, final int translateY) {
TranslateAnimation translate = new TranslateAnimation(0, translateX, 0,
translateY);
translate.setDuration(1500);
translate.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
buttonContainer.removeView(button);
button = new Button(Test2XAnimation.this);
button.setText(title);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttonTapped(button);
}
});
params.leftMargin = translateX + params.leftMargin;
params.topMargin = translateY + params.topMargin;
params.rightMargin = 0 + params.rightMargin;
params.bottomMargin = 0 + params.bottomMargin;
button.setLayoutParams(params);
buttonContainer.addView(button);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
params = (RelativeLayout.LayoutParams) button.getLayoutParams();
title = button.getText();
}
});
button.startAnimation(translate);
}
}
The method startTapped() is triggered when a user clicks a button in the UI. The other button moves by (200,100). At the end, I remove the old one and create a new one, thenaI add it to the parent view. You can see that buttonTapped() is called after the animation.
A suggestion: you can use NineOldAndroids project if you want to support both the new and the old way of animating a component, then you can check the OS version and run this code only on Gingerbread and lower versions.
I really struggled with shuffling layout params to make the "real" button match up to its animated location. I finally succeeded by using this approach. I extended ImageButton and overrode getHitRect:
#Override
public void getHitRect(Rect outRect) {
Rect curr = new Rect();
super.getHitRect(curr);
outRect.bottom = curr.bottom + 75;
outRect.top = curr.top + 75;
outRect.left = curr.left;
outRect.right = curr.right;
}
Then, I could use this button in the troublesome view:
<com.sample.YPlus75ImageButton a:id="#+id/....
It's worth noting that all of this changes for 3.0.