I made a Recylerview and added a button to the left bottom of the Element.
Now when I use:
private View.OnTouchListener onWidthClickListener =
new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
int x = (int) event.getX();
Log.d(TAG, "Width: " + x);
}
return false;
}
};
I get the position of the courser but it is absolute to the button and I want it relative to the hole Screen.
Because it is a RecylerView the button has no fixed position relative to Screen, therefor I can't just add or subtract the values of the layout.
If you want the coordinates relative to the top left corner of the device screen, then use the raw values.
int x = (int)event.getRawX();
int y = (int)event.getRawY();
Exaplain:
getX() and getY()--> return you coordinates relative to the View .
MotionEvent.getRawX() and MotionEvent.getRawY() -->return absolute coordinates,regarding the screen of the device.
For more details:doc
You can use event.getRawX() and event.getRawY() to get position relative to screen.
Related
How can I set any view to the middle of my touch position using onTouchEvent?
Red Circle Is My Click Position
Black Circle Is The View Like Button, ImageView, And So On...
Can someone leave an example?
Edit
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
imgCircle.setX(event.getX() - imgCircle.getWidth() / 2);
imgCircle.setY(event.getY() - imgCircle.getHeight() / 2);
}
return false;
}
Example of a solution:
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
yourView.setX(x);
yourView.setY(y);
return false;
}
You get the coordinates from click and set them to the view. If you have multiple views inside of one you might have to get relative position of the parent view and then subtract the clicked position from parent position.
If you want to set the view position to the centre just subract half of view width from x and half of height from y.
EDIT
How to set position to centre of view:
yourView.setX(x-yourView.getWidth()/2);
yourView.setY(y-yourView.getHeight()/2);
I've created an OnTouchListener that will allow me to drag views around the screen. This works well, except for one problem:
My views resize instead of leaving the parent. They are in a RelativeLayout, which I suspect is contributing to the problem. They only resize when touching the right or bottom boundaries of my RelativeLayout.
I'm hoping there is a simple solution to this problem, (ideally not involving resizing the RelativeLayout), but I haven't found a solution yet.
Ideally, this solution would be applicable for all views, as I use this OnTouchListener for TextViews as well as ImageViews. I was hoping to find an XML solution. I'm assuming others have dealt with this issue??
PS: Before I post this..I've just remembered I believe there is a flag for hitting a RelativeLayout's border, so maybe I can apply negative margin to the TextView when that happens. Thoughts?
EDIT
Here is my OnTouchListener:
// Dragging functionality for all views and double tap to remove
// functionality for ImageViews
final OnTouchListener onTouchListener = new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
v.bringToFront();
//if(v instanceof android.widget.ImageView)
//mScaleDetector.onTouchEvent(event);
layoutParams = (LayoutParams) v.getLayoutParams();
switch (event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
// Where the user started the drag
pressed_x = (int) event.getRawX();
pressed_y = (int) event.getRawY();
if (v instanceof android.widget.ImageView)
{
curTime = System.currentTimeMillis();
if(curTime - prevTime <= DOUBLE_TAP_INTERVAL)
{
v.setVisibility(View.GONE);
}
prevTime = curTime;
}
case MotionEvent.ACTION_MOVE:
// Where the user's finger is during the drag
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
// Calculate change in x and change in y
dx = x - pressed_x;
dy = y - pressed_y;
// Update the margins
layoutParams.leftMargin += dx;
layoutParams.topMargin += dy;
v.setLayoutParams(layoutParams);
// Save where the user's finger was for the next ACTION_MOVE
pressed_x = x;
pressed_y = y;
break;
default:
break;
}
return true;
}
}
How to getthe X and y coordinates of an image in Layout in Android?
And how to get top, bottom, left and right of an image in Layout in Android?
by using Touch listeners you can get X and Y values of view
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
return false;
}
});
for x and y coordinates of any view compared to its parents , use :
getLeft() , getTop() , getRight() ,and getBottom() .
for raw x and y coordinates of any view compared to the screen/window , use:
getLocationInWindow(int[] location) , getLocationOnScreen(int[] location)
do note that all of those methods won't work till the view is positioned and layout procedure has finished.
more methods are written on the API here.
I have an android code which starts vibrating for a random amount of time when the user touch a screen. Now I want to change it a bit. I want to ask the user to touch a predefined position, for example in the middle of screen. what should I do that only in some special coordinates touching become effective?
You can use a onTouchListener. The Listener gets a MotionEvent from which you can get th coordinates and check the position:
something.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// get coordinates from touch event
int x = (int) event.getX();
int y = (int) event.getY();
// check if the coordinates are in a specific area
return true;
}
});
To get the width and height you can use the following code:
Display d = getWindowManager().getDefaultDisplay();
width = d.getWidth();
height = d.getHeight();
onTouch(View v, MotionEvent event)
onTouch give you a MotionEvent, with this, you can simply do
event.getX();
event.getY();
for getting coordinates and do something with it (or not :))
can any one help me how to drag and drop the text view on entire image view. i found from this drag n drop textview in android here the text view is static, in my app textview and image view's are dynamically change. `#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Remember our initial down event location.
startX = event.getRawX();
startY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float x = event.getRawX();
float y = event.getRawY();
// Calculate move update. This will happen many times
// during the course of a single movement gesture.
scrollByX = x - startX; //move update x increment
scrollByY = y - startY; //move update y increment
startX = x; //reset initial values to latest
startY = y;
invalidate(); //force a redraw
break;
}
return true; //done with this event so consume it
}
i wright this code, but it's not working properly.
You can always change the image and text of existing view programatically using setText sort of methods.
There are many drag and drop tutrials if you google it. strongly suggest you try them out.
Basically what you do is implement a OnTouchListener on the TextView like
textview.setOntouchListener(listen);
OnTouchListener onThumbTouch = new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch(v.getId())
{
case R.id.slider_thumb_id:
{
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
{
}
case MotionEvent.ACTION_UP:
{
}
}
break;
}
}
return true;
}
};
Here from the event attribute you get the value of x and y and the touch event and
use that to alter the position of the textview. I did it by add add/subtracting the margin values.
EDIT:
This is what I did to update the position of my view
Look event.getRawY(); is going to give you the distance from the parent view. So if I get event.getRawY() = 50. I know I need the text view to be 50 pixels from the top.
LinearLayout.LayoutParams iconParams = (LinearLayout.LayoutParams)
v.getLayoutParams();
iconParams.topMargin = 50;
v.setLayoutParams(iconParams);
So now the view that i selected is 50 px from the top of the parent view.