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);
}
});
Related
How to create a fade in ImageView to change it to other image (i.e. from Image 1 to Image 2) when imageView is clicked and vica versa
I have created another click method but it is not working because i think as I have already put image-2 aplha to 0.
public void fade(View view)
{
ImageView mickey=findViewById(R.id.mickey);
ImageView mouse = findViewById(R.id.imageView2);
mickey.animate().alpha(0f).setDuration(2000);
mouse.animate().alpha(1f).setDuration(2000);
}
Here is a simple method to play around with: :)
boolean firstImage = false;
private void fadeFlip(View view) {
AlphaAnimation aa = new AlphaAnimation(1f,0f);
aa.setDuration(1*1000);
view.startAnimation(aa);
aa.start();
if(firstImage) {
view.setBackgroundResource(R.drawable.ic_launcher);
} else {
view.setBackgroundResource(R.drawable.image_2);
}
AlphaAnimation bb = new AlphaAnimation(0f,1f);
bb.setDuration(1*1000);
view.startAnimation(bb);
bb.start();
firstImage = !firstImage;
}
I'm trying to change the style attribute "colorControlNormal" of my app programmatically and during runtime, but I didn't have any results.
This property is the color that will tint the hamburger & back icons of the new Toolbar viewGroup. Beside, I'm using the v7 compatibility library.
I heard that we cannot change app theme during runtime, but I'm looking for an answer, even if it's not so clean way.
Edit:
I just figured that gmail is doing what i want, when you click on the search icon, the white hamburger icon turn into grey back.
Waiting for more.
I spent one day, played with different implementation. So my opinion, the best way todo that it copy paste DrawerArrowDrawable from AppCompat v7 library.
https://gist.github.com/IstiN/5d542355935fd7f0f357 - take a look on the code with some optimization
than you can use it in your main activity with code below
DrawerArrowDrawable drawable = new DrawerArrowDrawable(this, this);
ImageView menuButton = (ImageView) findViewById(R.id.arrow);
menuButton.setImageDrawable(drawable);
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((DrawerLayout)findViewById(R.id.drawer)).openDrawer(Gravity.START);
}
});
when you start new fragment, you need to create one more view on the same place and add second code to your fragment
private DrawerArrowDrawable mArrowDrawable;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mArrowDrawable = new DrawerArrowDrawable(getActivity(), getActivity());
ImageView topButton = (ImageView) view.findViewById(R.id.arrow);
topButton.setImageDrawable(mArrowDrawable);
topButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
closeSearch();
}
});
//run animation from hamburger to arrow
animate(0, 1, null);
....
private void animate(int startValue, int endvalue, Animator.AnimatorListener listener) {
ValueAnimator anim = ValueAnimator.ofFloat(startValue, endvalue);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
mArrowDrawable.setProgress(slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(300);
if (listener != null) {
anim.addListener(listener);
}
anim.start();
}
to make animation from arrow to hamburger handle back button and execute code
animate(1, 0, null);
you also need to wait in your fragment while animation will not finish, but it another questions.
If you have any questions ask in comments.
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);
I've got a simple View property animation attached to a button. The animation works perfectly on the first button press. However, on subsequent clicks no animation occurs. The log proves the button is being pressed. Is there something I'm missing? Here is the code:
Button handle = (Button) findViewById(R.id.HandleButton);
Button wheel1 = (Button) findViewById(R.id.Wheel1);
handle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Button Clicked");
ViewPropertyAnimator spinWheel1 = wheel1.animate().rotationX(360*8).setDuration(2000);
}
});
Button handle = (Button) findViewById(R.id.HandleButton);
handle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Button Clicked");
spinWheel1 = wheel1.animate().rotationXBy(360*8).setDuration(2000);
}
});
Try using rotationXBy instead of rorationX, you may also want to try using object animator as you can set start and end rotation degree.
try this:
ObjectAnimator animator = ObjectAnimator.ofFloat(spinWheel1, View.ROTATION, 0, 360*8);
animator.setDuration(2000);
animator.start();
your button already has rotated to 360*8
By adding the 0 your animation will start all over again
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.