Android Button Doesn't Respond After Animation - java

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.

Related

Reset Button to default background

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);
}
}

Blur TextView on click, Android

I am trying to blur out a TextView, when a button is clicked. So far my code looks likes this:
#Override
public void onClick(View v) {
float radius = pdf.getTextSize() / 3;
BlurMaskFilter filter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
if (v==pause){
if (paused){
paused=false;
pdf.getPaint().setMaskFilter(null);
}
else{
paused=true;
pdf.getPaint().setMaskFilter(filter);
}
}
}
What am I doing wrong?
just set this
pdf.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
befor line: pdf.getPaint().setMaskFilter(filter);

Android add EditText dynamically

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.

Not doing Animations and Moving to the next screen

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!

android animation incorrectly animating

I have a top level LinearLayout that has many children composed of a button followed by an invisible LinearLayout. When I press the button, I would like the contents of the LinearLayout to animate and press everything down smoothly. Instead what is happening is a big white space is instantly being carved out and then the LinearLayout animates into the empty space instead of everything being gracefully pushed down.
My onCreate method creates the massive LinearList plus all the children of each child LinearList. If you run this code you will see 20 buttons labled "Button X" where X is 0-19. When you click on a button indented buttons animate below (like a sub menu) but the next button in the form of "Button X+1" is immediately painted all the way down and not animated being pushed down.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout topll = (LinearLayout)findViewById(R.id.topll);
for(int i=0;i<20;i++)
{
Button b = new Button(this);
b.setText("Button"+String.format("%s", i));
b.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
b.setOnClickListener(this);
topll.addView(b);
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
l.setVisibility(View.GONE);
l.setOrientation(LinearLayout.VERTICAL);
for(int k=0; k<10; k++)
{
Button b2 = new Button(this);
b2.setText("New Button"+String.format("%s", k));
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
p.setMargins(80, 0, 0, 0);
b2.setLayoutParams(p);
//b2.setOnClickListener(this);
l.addView(b2);
}
topll.addView(l);
}
}
And here is my method that gets called when a button is pushed...
#Override
public void onClick(View arg0)
{
LinearLayout topll = (LinearLayout)findViewById(R.id.topll);
for(int i=0;i<topll.getChildCount();i++)
{
if(topll.getChildAt(i) == arg0)
{
LinearLayout ll = (LinearLayout)topll.getChildAt(i+1);
if(ll.getVisibility() == View.GONE)
{
Animation animation = AnimationUtils.loadAnimation(this, R.anim.expanddown);
ll.setAnimation(animation);
animation.start();
ll.setVisibility(View.VISIBLE);
}
else
{
Animation animation = AnimationUtils.loadAnimation(this, R.anim.expandup);
ll.setAnimation(animation);
animation.start();
ll.setVisibility(View.GONE);
}
}
}
}
and finally my animation xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:duration="1000"
android:startOffset="0"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:toXScale="1.0"
android:pivotY="0%"/>
</set>
first of all use
ll.startAnimation(animation);
instead of :
ll.setAnimation(animation);
animation.start();
then use an animation listener and
animation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation anim)
{
};
public void onAnimationRepeat(Animation anim)
{
};
public void onAnimationEnd(Animation anim)
{
ll.setVisibility(View.VISIBLE);
// or what ever
};
});

Categories

Resources