How to create a swipe button like this? - java

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

Related

AndroidPlot: Detecting touch event on point

I'm writing an application using AndroidPlot in which the user needs to be able to touch a point on a scatter plot and bring up information about that specific point. In other words, the application needs to identify the closest point to the location touched or otherwise recognize that a point has been touched, and be able to return the specific identity of the point. All of the points in this scatter plot will always be of one series, so identifying between series isn't an issue, but I don't know how to implement finding or identifying the touched point.
I can get as far as:
plot.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
PointF click = new PointF(motionEvent.getX(), motionEvent.getY());
if(plot.getGraphWidget().containsPoint(click)) {
AlertDialog.Builder builder = new AlertDialog.Builder(GraphView.this);
builder.setTitle("Point: ");
builder.setMessage("Description: ");
AlertDialog dialog = builder.create();
dialog.show();
}
return false;
}
});
}
Which creates the AlertDialog whenever the graph is touched.
The DemoApp's BarPlotExampleActivity has this functionality implemented in it's onPlotClicked(...) method. It could certainly be improved but should give you a good starting point.
The basic steps are:
Filter for clicks within the plot's graph area. (You've got this piece already)
Convert the screen coords to domain/range values using XYPlot.getXVal(screenX) & XYPlot.getYVal(screenY).
Find the closest point(s) in your model for the above domain/range values.

How to center image in android action bar, with action items on each side

Yes this question has been asked before. Two times actually,
ActionBar - custom view with centered ImageView, Action Items on sides
ActionBar logo centered and Action items on sides
Yes I have thoroughly read both and it works except, I cant figure out how to put items to the LEFT of the image. To quote from the second link:
Of course, make sure you also do the other necessary setups for the
Action items in your menu xml's as well. This will be for any
right-sided action items.
Not much help. This doesn't apply to placement:
To take control of the left action item, you could do this in your Activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.yourRightActionItem:
//do something
return true;
case android.R.id.home: //<-- this will be your left action item
// do something
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have tried many things, but never can get buttons to the left. Can someone clear this up for me?

Android: Limit drag & drop of object

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.

Timed display for graphic button on action_down MotionEvent

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!

Android - ACTION_UP not detected

I am unable to figure out why the ACTION_UP event is not being fired when I move my finger away from contact in the following code.
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionIndex()){
case MotionEvent.ACTION_UP:
Utils.log("Touch Up");
break;
case MotionEvent.ACTION_DOWN:
Utils.log("Touch Down");
break;
}
return true;
}
You should use getActionMasked().
getAction() returns single pointer events (and deprecated multiple pointer events...)
getActionMasked() returns single and multiple pointer events (use with getActionIndex() to determine which pointer),
getActionIndex() returns only the pointer index.
So getActionIndex() returns which finger performed a down / up action (0, 1, 2, etc.) While getAction() or getActionMasked() return the single pointer events you want (ACTION_DOWN, ACTION_UP, etc.)
Replace your code with mine.
public boolean onTouch(View v, MotionEvent event) {
//Here's the problem
switch(event.getAction()){
case MotionEvent.ACTION_UP:
Utils.log("Touch Up");
break;
case MotionEvent.ACTION_DOWN:
Utils.log("Touch Down");
break;
}
return true;
}
For others having a similar problem, one of the reasons why ACTION_UP is not fired could be overly sensitive ACTION_MOVE. In this case, ACTION_UP never gets fired because system interprets touch action as ACTION_MOVE and fires it instead.
A solution for that is to check when ACTION_MOVE happens if x and y offset is big enough to handle it as ACTION_MOVE, otherwise, you can threat it as ACTION_UP.
Here is SO example.

Categories

Resources