So I have spent a long time looking to see if there is a solution for this. Basically, the below code allows me to press on a button, when I press (action_down) the 'pressed version' of the button is displaying good, then when I remove my finger the graphic returns to normal (action_up).
This part is working good BUT I need the ACTION_DOWN to stay active a little longer after the user presses the button and not return to ACTION_UP so fast. I can see lots of different people asking this question but no answers relating to editing the action_up or action_down part. I thought there must be some simple way to achieve this goal..
The current code:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
int img = Integer.parseInt(v.getTag(R.id.tag_press_down_img).toString());
if (v instanceof ImageView) {
((ImageView) v).setImageResource(img);
}
} else if (MotionEvent.ACTION_UP == event.getAction()) {
int img = Integer.parseInt(v.getTag(R.id.tag_press_up_img).toString());
if (v instanceof ImageView) {
((ImageView) v).setImageResource(img);
}
}
return false;
}
(This code is working fine but the user experience is bad because the button reverts back to ACTION_UP too fast (immediately) after being released - I would like ACTION_DOWN to 1) display for 1-2 seconds then revert back to ACTION_UP OR 2) remain in action_down until the called event is complete - in this case loading another screen).
Thank you!
Related
I want to create a dual side swipe button which gives haptic feedback on reaching the end. The image is attached.
https://i.stack.imgur.com/eIECf.png
This is not a question with a simple answer, but ill try to give you a general direction on how to go about implementing something like this:
You need handle Touch events on your button, yourself.
something like:
button.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down code
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return false;
}
});
In onTouch, you need to do two things:
Move the button to the appropriate location, base on the motion of the user's finger.
for this, you should probably store the position of ACTION_DOWN, and then calculate relative movement in ACTION_MOVE and move your button to match it.
If you want your button to only move along some axis, you need to calculate the appropriate position on this axis, yourself.
in ACTION_UP, check the position of your button, and if its close enough to where you want your swipe to end, execute the action
i am creating an android game and have an onTouchEvent like this:
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!action) {
action = true;
}
} else {
action=false;
}
return true;
}
My problen is now, that action is false as soon as i swipe my finger a little bit.
thanks for helping,
Florian
If you want to return true even when a swipe input occurs, you could store the last action to compare if the last action was ACTION_DOWN and if the current 'event.getAction()' is also ACTION_DOWN, then you can assume that the swipe action occurs (also if the previous 'x' and 'y' positions differ to the current 'x' and 'y' positions), therefore still return true. Any other scenario, you can return false.
You could also try looking in the android API documentation about a swipe input action/gesture.
Hope that is helpful.
First app ever for me, and first time experiencing with java coding.
I'm building an app for assisting my Rubiks cube speed training, and i'm stuck on my timer.
My general plan is:
I have my main activity screen, where with 1 touch (anywhere on the screen except my 2 buttons) can start a timer. When the timer starts the buttons will fade, and only the timer is visible.
At this moment I have my MainActivity, and a Timer activity. When pressing the screen the timer activity "launches". I then have to press once more to start the timer, this is ok for now, I will fix it somewhat later.
My main goal for now is:
On the timer activity, I want to press and HOLD for at least 2 seconds, and THEN upon release, the timer must start. Hereafter, with a new click, the timer must stop.
I have searched, but it is difficult with minimum knowledge. I have tried a few different things. And what I have is basically something like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
TimerView = (TextView) findViewById(R.id.TextTimerView);
View StopTimeView = (View) findViewById(R.id.StopTimeView);
StopTimeView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
starttime = SystemClock.uptimeMillis();
timerhandler.postDelayed(timerrunnable,2000);
timestarted = true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
if(timestarted = true){
timestarted = false;
timerhandler.removeCallbacks(timerrunnable);
}
}
return true;
}
});
}
This code stops when the touch is released. If I remove the last IF sentence, the timer automatically starts after the given 2 seconds, no matter if I hold or not. Furthermore, I need to add an additional button to stop the timer, which I do not want (the ability to be able to stop by touching everywhere is important).
I hope the question is understandable and not to vague. If more of the code is needed, please let me know.
Thanks for your time!
Simply check in your timerrunnable if the button is still pressed. Also, start the onTouch method with a check whether your timer is running. If it is, you know you want to stop it - if it isn't, you can continue with the code you already have.
I have a Activity. On this Activity I have a Relative Layout. On this layout I have placed several custom objects from a class that extends RelativeLayout.
It kind of looks like a chessboard (it isn't, but so you know what I'm talking about).
I now want to be able to touch one of this objects and drag it to one of the 4 adjacent fields, swapping it against the one there.
I thought the most simple way would be to use androids drag & drop.
I used setOnTouchListener and setOnDragListener for every object, so every object can be touched and dragged onto another object.
I used this tutorial, should be easier for you to read than just the code, but here it is:
#Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
return true;
} else {
return false;
}
}
#Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
MyCustomViewClass to = (MyCustomViewClass) v;
to.addView(view);
view.setVisibility(View.VISIBLE);
}
return true;
}
Now I am wondering how I could limit where a object could be dropped. I want it to be able to be moved just to the 4 adjacent fields (not only by limiting the draglistener, I also don't want the object to be moved beyond this objects, no matter how far the finger tries to drag it).
I hope you get what I mean, if not please ask.
So this question is all about what I can do with this drag & drop I guess. Is it possible to somehow detect where the finger is before dropping? Is it possible to limit the dragging to the straight x and y axis? Or is there a way to know the direction of that dragging movement, allowing me to 1) know the object I would swap with and 2) moving the other object before the drop, making it look like the objects are just swapping?
Edit:
I got the direction, since I can calculate it from view.getX, view.getY (coords are inside my startobject) and v.getX and v.getY (coords from the object I'm over) in onDrag when the dragEvent is ACTION_ENTERED.
Now I try to figure out either how to set the dragshadows position to the object I want it to be over, or to only allow dragging in my direction and ending the drag over the next object.
Further help or ideas still appreciated.
I am make a application on android that need multi-touch gesture! I found that my devices Novo 7 (ainol) supports 5 touch points. I implement onTouch for a View like this
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN || event.getAction() == MotionEvent.ACTION_DOWN){
Log.i("Touch", "Touch "+event.getPointerCount() + " -- Position " +
event.getRawX()+","+event.getRawY());
}
else if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP|| event.getAction() == MotionEvent.ACTION_UP){
Log.i("Touch up","Pointer = "+event.getPointerCount());
}
return true;
}
At first, everything is ok, the log is printed out perfectly with 5 message. But things happened when i touch and release the 6th point on screen. The log re-printed out 5 message. Can you tell me what is going on here. Sorry for my bad english and if this is a noob question! Thank you very much!
----------- EDITED--------------------
After logging for ACTION_POINTER_UP and ACTION_UP, i found out that when the 6th finger touch (touch down) the screen, all the current pointers (5 pointers) is release from the Event (e.g the action_up and action_pointer_up is call on 5 previous pointers)! Now, the question is, how the android process this situation when the limitation of touch point is reach.