The plan is for the refresh icon to starts animating when the user clicks on the refresh icon, however, there seems to be an error when I implement the animation function; "The method loadAnimation(Context, int) in the type AnimationUtils is not applicable for the arguments (new View.OnClickListener(){}, int)".
Below is the code snippet. How do I load the Animation from the Animation Source, without the above error while still retaining the onClickListener framework.
final ImageView refreshBtn= (ImageView) findViewById(R.id.spin_refresh);
Log.i("RootActivity:setupHeader","******ImageView refreshBtn******");
//Listening to Button Click by User
refreshBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//To set new INTENT on calling Methods in UPDATE & DOWNLOADSERVICE
.....
//Perform Refresh Animation
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView iv = (ImageView)inflater.inflate(R.layout.header, null);
//ISSUE IS AT .loadAnimation not able to be implemented.
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate_refresh);
rotation.setRepeatCount(Animation.INFINITE);
iv.startAnimation(rotation);
item.setActionView(iv);
//ALERT DIALOG TO INFORM USER THAT REFRESH FUNCTION HAS BEEN CALLED
.......
}
});
Change
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate_refresh);
to
Animation rotation = AnimationUtils.loadAnimation(YourActivity.this, R.anim.rotate_refresh);
The reason is that at that point in your code, this refers to View.OnClickListener, not to your activity.
this refers to OnclickListner there
change it to your context
Animation rotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_refresh);
Related
I am trying to implement push notification using GCM. On my activity where I get the broadcast, I want to blink a menuitem so that user know that there is something waiting to be read.
I tried the following code:-
Animation mAnimation = new AlphaAnimation(1, 0);
mAnimation.setDuration(200);
mAnimation.setInterpolator(new LinearInterpolator());
mAnimation.setRepeatCount(Animation.INFINITE);
mAnimation.setRepeatMode(Animation.REVERSE);
menuItem.startAnimation(mAnimation);
However the last line gives me error, it says "The method startAnimation is undefined for menuItem", which is true as I implemented it for button so what I did, I typecasted it to View and altered the last line with
((View) menuAlert).startAnimation(mAnimation);
However its gives me Class cast exception. How can I make this menuitem flashing.
LayoutInflater inflater = (LayoutInflater) getApplication()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView iv = (ImageView) inflater.inflate(R.layout.imagelayout,
null);
Animation rotation = AnimationUtils.loadAnimation(getApplication(),
R.anim.alert_rotate);
rotation.setRepeatCount(Animation.INFINITE);
rotation.setDuration(200);
iv.startAnimation(rotation);
menuAlert.setVisible(true);
menuAlert.setActionView(iv);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
menuAlert.getActionView().clearAnimation();
menuAlert.setActionView(null);
lbltitle.setTypeface(null, Typeface.BOLD);
lbltitle.setText(Html.fromHtml("<u>" +broadcastTitle +"</u>"));
lblMessage.loadData(broadcastMessage, "text/html", "UTF-8");
m.setVisible(false);
k.setVisible(false);
menuAlert.setVisible(false);
}
});
I made a resource layout, where I put a Imageview then I made a animation resource for rotation and then I wrote above code and it did work.
Check this out.
MenuItem item = menu.findItem(R.id.action_settings);
ImageView imgView = new ImageView(this);
imgView.setBackground(getResources().getDrawable(R.drawable.ic_launcher));
item.setActionView(imgView);
imgView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(HorizontalScrollView.this, "onCLicked imgView", Toast.LENGTH_SHORT).show();
Animation mAnimation = new AlphaAnimation(1, 0);
mAnimation.setDuration(200);
mAnimation.setInterpolator(new LinearInterpolator());
mAnimation.setRepeatCount(Animation.INFINITE);
mAnimation.setRepeatMode(Animation.REVERSE);
MenuItem item = menu.findItem(R.id.action_settings);
item.getActionView().startAnimation(mAnimation);
}
});
This is a code for blinking textview on a button click..
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
recordShow.setVisibility(View.VISIBLE);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
recordShow.startAnimation(anim);
}
i have to stop blinking on another button click...what to do..??
Another approach could be:
1. Declare the Animation and TextView objects globally (outside any methods) in your Activity.
private Animation mAnim;
private TextView mRecordShow;
2. Setup a class that sets your animation properties and starts it. Let this class expect a TextView widget as its parameter.
protected void setBlinkingText(TextView textView) {
mAnim = new AlphaAnimation(0.0f, 1.0f);
mAnim.setDuration(1000); // Time of the blink
mAnim.setStartOffset(20);
mAnim.setRepeatMode(Animation.REVERSE);
mAnim.setRepeatCount(Animation.INFINITE);
textView.startAnimation(mAnim);
}
3. Setup another class that stops your animation on a given text view. Let this class expect a TextView widget as its parameter as well.
protected void removeBlinkingText(TextView textView) {
textView.clearAnimation();
}
4. Now you can use your classes wherever desired, passing it the appropriate text views.
e.g.
(a) In your onClick() method where you want to start the animation, replace all your animation code with:
setBlinkingText(mRecordShow);
(b) wherever you want to stop the animation on that text view, just call:
removeBlinkingText(mRecordShow);
The following assumes you want to stop the blink by clicking the same button. If you want to stop the click using a different button, you can split the if-else in the onClick() below into separate click handlers.
First, move anim outside onClick() and make it a field of the containing class. You need anim to be stored somewhere so you can cancel it later.
Animation anim = new AlphaAnimation(0.0f, 1.0f)
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
Second, create a boolean field in the containing class to keep track of whether the TextView is currently blinking:
boolean mBlinking = false;
Then:
#Override
public void onClick(View v)
{
recordShow.setVisibility(View.VISIBLE);
if(!mBlinking){
recordShow.startAnimation(anim);
mBlinking = true;
} else{
recordShow.clearAnimation(anim); // cancel blink animation
recordShow.setAlpha(1.0f); // restore original alpha
mBlinking = false;
}
}
I am using sequence for animation in my project. I have set onClickListener to that image where I put animation sequences. now when first time I run the code, and click on image, it start run animation correctly but then it never run again on click. I have to again run the code. So how I can run animation sequences more than one time? I have put my codebelow to start animation sequence.
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
animation_door = (AnimationDrawable) image.getBackground();
animation_door.start();
} });
Well, before clicking your button again you have to stop the animation also, so you can try it like this,
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
animation_door = (AnimationDrawable) image.getBackground();
animation_door.stop();
animation_door.start();
}
});
Is your animation set as a oneShot? It's most likely in the end state and you just need to hint it back to the start state.
AnimationDrawable door = (AnimationDrawable) image.getBackground();
door.setOneShot(false);
door.start();
Keep in mind this will most likely lead to a looping animation, which you probably don't want. You could consider using an Animation Set instead since you'll have a bit more control over the animation itself.
I am dealing with a view flipper. I have 2 views in my view flipper and in the second view on completing a frame animation an animated popup menu translating from bottom. when I press the back button I could able to flip to first view from second but again when I switch to the second view from first view that popup menu is not disappearing. I used reset() and setfillafter() methods but no result
How to solve this? any Idea?
Here is my code.
final Animation popup = new TranslateAnimation(0, 0, 200, 0);
popup.setDuration(20000);
popup.setFillAfter(true);
hearttap.setOnClickListener(new View.OnClickListener() {
public void onClick(final View view) {
final RelativeLayout popuplayout = (RelativeLayout) findViewById(R.id.popuplayout);
final ImageView ekgimgview4 = (ImageView) findViewById(R.id.ekgimgview4);
ekgimgview4.setVisibility(ImageView.VISIBLE);
ekgimgview4.setBackgroundResource(R.anim.ekgtimer);
AnimationDrawable ekgframeAnimation4 = (AnimationDrawable) ekgimgview4
.getBackground();
if (ekgframeAnimation4.isRunning()) {
findViewById(R.id.ekgimgview4).postDelayed(new Runnable() {
public void run() {
// openOptionsMenu();
popuplayout.startAnimation(popup);
popup.setFillAfter(true);
popup.setStartTime(30000);
ekgimgview4.setVisibility(view.GONE);
}
}, 30000);
final Button ekgbutton = (Button) findViewById(R.id.ekgbutton);
ekgbutton.setOnClickListener(new View.OnClickListener() {
public void onClick( View view) {
RelativeLayout popuplayout = (RelativeLayout) findViewById(R.id.popuplayout);
popuplayout.setVisibility(View.INVISIBLE);
}
});
You need to use the dismiss() function for popups. So use the following code whenever you transition between Views:
popup.dismiss();
I would edit it into your code myself, but the way StackOverflow handles displaying code tags made your code half into HTML code tags and half not.
i am trying to do some simple animation, i have list with items, those items include also checkboxes
on a checkbox button click i want to show with animation some button from the buttom, something like this:
private int mPosition;
private CheckBox chkBox;
OnItemClickListener(CheckBox mChkBox, View v)
{
chkBox = mChkBox;
chkBox.setClickable(false);
chkBox.setChecked(false);
chkBox.setClickable(true);
}
#Override
public void onClick(View v)
{
if (chkBox.isChecked())
{
animation = AnimationUtils.loadAnimation(context,
R.layout.animation_slide_in);
animation.setDuration(500);
animation.setInterpolator(new AccelerateInterpolator());
btDeleteItms.startAnimation(animation);
btDeleteItms.setVisibility(btDeleteItms.VISIBLE);
}
now the wierd thing, that after i click the checkboxbutton, the animation does work fine,but the state of the triggering checkbox button, is being uncheck unexpectdly?
how come? does animation resetting states somehow?
Thaks,
ray.
If I understood you correctly you need to use animation.setFillAfter(true); after .startAnimation(animation) method.