Android get x y coordinates of tab - android

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 .

Related

Find tapped icon using X and Y coordinates

I have file list in my android application. when the user double tap on a file I need to find out it. I could write the code to find out double tab action and X and Y coordinates of the user tap. But how can I find out the particular file or folder that the user did double tap?
This is the code what I used:
private View.OnTouchListener onTouchListener=new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
long thisTime=System.currentTimeMillis();
if(thisTime - lastTouchTime <500){
float x=event.getX();
float y=event.getY();
System.out.println("x :"+x);
System.out.println("y "+y);
}else{
lastTouchTime=thisTime;
}
}
return true;
}
};
I cannot find out it using icon sizes because they are not similar and user able to change it as they wish for icon image. Is there anyway to find out particular icon using X and Y coordinates of user tap?

How to get canvas area click?

I have drawn piechart using canvas method of view , but now i want get click of individual pie ? how can i dot that?
I got perfect answer for this question:
get color code of click area and check if color match with your color code this will get click you want.
#Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
Logger.debug("X-->"+touchX+" Y---->"+touchY);
//get drawing cache of your view
Bitmap bitmap = getDrawingCache(true);
//Get color code of pixle where you have tap
int colorCode=bitmap.getPixel((int)touchX,(int)touchY);
if(colorCode == context.getResources().getColor(R.color.pie_blue)) {
Logger.debug("Color blue");
onPieClick.onBluePieClick(touchX,touchY);
}else if(colorCode == context.getResources().getColor(R.color.pie_green)) {
Logger.debug("Color green");
onPieClick.onGreenPieClick(touchX,touchY);
}
return super.onTouchEvent(event);
}
What you can do is,
Override onTouch event & You will get Motion event,
You will get x & y co-ordinates of the click by event.getX() &
event.getY() respectively.
identify where this x & y intersect in pie.
Sample code:
1)Simple
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
float xCord=event.getX();
float yCord = event.getY();
....
Write condition to identify where this x & y intersect in pie.
...
}
return true;
}
2) Another way getting touch (good way)
OnGestureListener mGestureListener=new GestureDetector.SimpleOnGestureListener(){
public boolean onSingleTapConfirmed(MotionEvent e) {
float xCord=e.getX();
float yCord = e.getY();
....
identify where this x & y intersect in pie.
...
};
};
GestureDetector gestureDetector=new GestureDetector(context, mGestureListener);
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
You can't. Well, at least not directly.
You could do the following though:
In the click handler for the view, determine the xy coordinates of the click
Compare the drawing code you wrote, thereby determining in which piece of the pie the click was

x and y coordinates of image in Layout

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.

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 touch in predefined position

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 :))

Categories

Resources