x and y coordinates of image in Layout - android

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.

Related

How to get the absolut position of a touch event?

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.

How to increase SeekBar scrolling touch position

I have a seekbar.Normally when we scroll on seekbar setOnSeekBarChangeListener is fire.Now I want when we scroll anywhere on the screen setOnSeekBarChangeListener should be fire(So seekbar will be scrolled).Actually I'm not getting how to do this.So can any help me out.Thanks
use this override method for touchevent `
#Override public boolean onTouch(View v, MotionEvent event)
{
int x = event.getX();
int y = event.getY();
return true;
}
now when you will click on screen you will have x and y axis. suppose you have 240px width and u have click it in centre than u will have x = 120. you can assign it to seekbar. and make your seekbar max value to max value of widht. or convert that x=120 wrt to 100 and than assign it to seek bar.

How to get X and Y values when click on item in gridView

I want to get the X and Y values for an item of grid view when I click on it, but how do I do this or how do I get these values?
I know I'll use the setOnItemClickListener method, but I can only get the position from it.
Here is how I get x and y coordinates of where the user clicks on Google Maps. Try if replacing it with other kinds of views works with you
public boolean onTouchEvent(MotionEvent e, MapView m) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
long start = e.getEventTime();
x = (int)e.getX();
y = (int)e.getY();
touchedPoint = map.getProjection().fromPixels(x, y);
}
Here map is name of my MapView . Basically it fetched X, Y from MotionEvent
Set a touch listener by calling
grid.setOnTouchListener().
One of the parameters is a MotionEvent object, which would include the x and y positions.

Android get x y coordinates of tab

In Android tabs, how does one get the x,y coordinates of the currently selected tab? My goal is to automatically scroll to that tab (using scrollTo(x,y)) when it is selected in code.
You can find the x and y location of place where screen has been touched using this
main.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent e)
{
float x = e.getX();
float y = e.getY();
return true;
}
});
Hope it helps .

How to know the X and Y coordinates of a ImageView?

I want to know the X and Y co-ordinates of a Imageview . I have to click on a single imageview , but on different positions i have to perfrom different tasks.
Kindly suggest me how can i know the X and Y co-ordinates of Imageview.
Sorry , I am not posting any code regarding this.
OnTouchListener has a paramater of event where you can get the x and y of the touch.
View.getX() and View.getY() will give you the left most and top most pixels on the screen respectively. Use those in conjunction with View.getWidth() and View.getHeight() and you got the entire pixel grid that the ImageView takes up.
set onTouchListener ........... then use even.getX() and event.getY() ;
you could add an OnTouchListener() to image view
img.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
event.getX() //cordinates wrt view
event.getRawX() //abs cordinates on screen
event.getY() //cordinates wrt view
event.getRawY() //abs cordinates on screen
return false;
}
});

Categories

Resources