I am making a simple game that displays balloons (Button/ImageView with background pic of a balloon). I've animated the balloons using the anim.xml files and the balloons move pretty well around the screen.
The problem is that when you touch them, they don't do anything. The location I placed them in XML retains the power to click on them. On touching the moving pics, nothing happens.
How do I make the balloons pop on touch (i.e. change background to a broken balloon image and produce a sound)?
Refer this link to know the property animation working. also use object animator to animate view. so only the click functionality of the view will remain in the view wherever we move.
Don't use animation XML files for this, use ViewPropertyAnimator instead. Just call `.animate()' on your balloon view and set a direction and duration of the animation. For example this code will move your balloon by 200 pixels to the right over 1 second:
balloonView.animate().translationXBy(200).setDuration(1000);
Check this link out.
This is an example of how to create views which are moving and can react on touches.
This is part of Coursera Android Development course.
If you are interested - take part in that course for free.
Best
Yes you create a SeperateClass For touch and Click listener and pass the Necessarydata.
e.g.
class BaloonClickListener implements onCLickListener
{
Button button;
public BaloonClickListener(Button button)
{
this.button=button;
}
#override
public void onCLick(View view)
{
button.setBackgroundResource(bustedImage);
}
}
Set Listener:
balloonView.setOnClickListener(new BaloonClickListener(balloonView));
I think this may solve your problem.
Related
I have four ImageButtons in my app.
Three smaller buttons can be either shown or "hidden" below the bigger button. I hide buttons using rotate and translate animations.
The problem is:
OnClickListener's onClick method always gets triggered no matter a smaller button shown or not.
I mean, when smaller buttons are "hidden", touching the place on screen, where one of the smaller button resides when shown, triggers onClick method.
It looks like Android OS does not take actual placement of the button into account when deciding whether it should trigger onClick method or not.
How can I overcome the issue?
I want onClick method to be called ONLY when there is a button below my finger.
EDIT:
All suggested workarounds rely on hiding the button. This doesn't help at all. The onClick method gets called for INVISIBLE and GONE buttons too. I checked this in debugger.
Looks like you are going for an Arc menu. Why not use the awesome implementations of Siyamed and DaCapricorn, instead of re-inventing the wheel?
Did you set your click interface your activity and implemented onClickListener? If so you need to separated your buttons by setting if conditions like if(arg0 == button1) for all the buttons.
The issue was caused by the fact that tween animations (the original animation framework) animate the pixels, not the touch zones of a widget.
I have replaced tween animations with property animations and now everything works like I expect it to work.
Setting
btn.setVisibility(View.INVISIBLE);
doesn't call onClick method where buttons are not visible. I have tested it. So you might be registering onClick listener in unnecessary views.
Try to set the listener using the property onClick of the button in the layout.xml (for example
android:onClick="onClickButtonCamera"
and then declare a public function with the same name like:
public void onClickButtonCamera(View view) {
//some stuff...
}
I allways use it and the function is never called when the buttons are hidden.
I hope it helps you.
i think there are three ways to solve this issue.
1) if you are setting onClick Listener then set it null when you hide buttons and set listener when you show them.
2)Add a checking in onclick for each button about their visibility simply return; the function when button's visibility is not VISIBLE(use the function button.getVisibility() != View.VISIBLE)
3)Last one not sure, usually setting gone as visibility will protect it from clicking.
I'd suggest setting the button visibility to GONE after completing the animation.
UPDATE:
Another suggestion is to have a button that is not animated (for which the problem should not happen) and an image (graphically identical) that is animated after the button is hidden. The image should not receive any clicks, under the big button or not, and the real button should not receive clicks when hidden.
I am making a game in which I have 5 buttons, looking like clouds, falling from the "sky".
That means that when my activity starts, 'clouds' cannot be seen, since the marginTop is set to -100dp.
From that position they start falling down untill they get lost on the bottom side of the screen.
The thing is, I need those buttons to be clickable, during the process of animation.
So far, I found some documentation about how I can make the buttons clickable AFTER the animation ends. But I don't need that. I need to be able to click on the buttons through the animation time itself.
NOTE: I need something that works with versions before 3.0.
Anybody has any link to documentation or some example or anything ?
After doing some research, I found out that there are two types of animations:
View Animation and Property Animation.
The view animation can only animate View objects. It also lack a variety of animations, since it can do only stuff as scale, rotate, move... It cannot change background color, for example.
Also, the disadvantage of the View Animation is that it only change the position of where the View object is DRAWN. Physically, it still stays in the same position.
That's why the button is un-clickable, after the View Animation is finished upon it.
Property Animation, in the other hand, can animate both View and non-View objects and it doesn't have constraints as the View Animation.
When objects are moved, for example, with the property animation, they are not just drawn on some other position on the screen, but they are actually MOVED there.
Now, Property Animation is a lot more complex to write than the View Animation, so if you don't really need all the advantages of the Property Animation, it is suggested to use View Animation.
Source:
Property vs View Animation
Tutorial and SupportLybrary up to API 1:
nineoldandroids
You can change the buttons to imageViews and then do
imageView.setOnClickListener(myListener)
then set myListener to do whatever you previously wanted to happen on the buttons onClick. Your activity will have to implement OnClickListener
Added bonus: you can make the images look like clouds :)
I want to implement the concept of "Slide to open" concept in my application. Basically, I am using an ImageView. If the user slide it to a specific direction (to the right for example), the next activity is started. I do not know how to implement this in Android, I have searched in lots of place but found nothing.
Any clues ?
What I came into :
No I do not think that the use of ViewPager or HorizontalScrollView is appropriate in this case.
I don't know if the concept of Drag And Drop helps (how to imply a drag in a specific direction ? How to let the ImageView return to its initial location if the drag ends midway ? ...)
Use frame layout and put a ImageView in it. Then add touchEvents to Imageview. update imagViews Layout parameters at ON_MOVE event.This is the basic idea of slide and drag stuff in android.Good luck.
you may use View.Animation for moving back the slider to start position again ON_UP event
you can move view only in x axis by only changing the views :
LayoutParams.leftMargin=event.getRawX();
(always set the gravity to NO_GRAVITY)
If you want to implement smth like "slide to unlock" you can implement drag and drop and forbid moving by Y axis for example. Also you can check image position and analys if this shift is enough to open\unlock, and if not - simple move this image back.
I started with a simple animation on button,
I am successful to an extent,
The problem i am facing is, I am able to animate the button but the position of listener,
Even after button is animated the clickable area is not animated , It is resting at the original postion
Could some one help me with an useful code snippet or suggestion
Regards
Android animations can not change the layout of buttons or view in general. Only their image will appear to move, but they will stay in the same place. After the animation is finished, get the Layout parameters through View.getLayoutParams() and change them.
I have a text view where in i have to keep ages between 1-99. I also have two buttons ^ and v(i mean up and down arrows) on clicking them the values of the age should smoothly scroll to next or previous value.
I have been trying different ways but couldnt achieve smooth scrolling. Can anyone please give me any idea of how to achieve the task.
I think the easiest way is to simply put the TextView within a ScrollView, and let the arrows interact with the ScrollView, by using things like the fling method and playing around with the velocity parameter to suit your needs.
Use the animation framework.
When pressing down, start the 'down'-animation.
When pressing up, start the 'up'-animation.
Read more about animation here: http://developerlife.com/tutorials/?p=343
View animation is not much matured and hence i am noy sure if that can be used for moving the views.
Please find the description below:
Another disadvantage of the view
animation system is that it only
modified where the View was drawn, and
not the actual View itself. For
instance, if you animated a button to
move across the screen, the button
draws correctly, but the actual
location where you can click the
button does not change, so you have to
implement your own logic to handle
this.
Source
To scroll smoothly you can try using the scroller component.
Reference
What you would need to do is pass the duration of the scroll in the constructor and then use the property
setFinalY(int newY)
to increment the counter position by 1 unit (equal to the height of the item).
Please let me know if that helps!