I am trying to get coordinates of frame layout present in my screen in android.
I tried getLocationOnScreen but it is giving me 0. How to get the right value?
int[] location = new int[2];
face_oval_layout.getLocationOnScreen(location);
Util.log("X axis is " + location[0] + "and Y axis is " + location[1]);
Above snippet called inside camera take picture callback. Value of X and Y, both are 0.
frmLayout = (FrameLayout)findViewById(R.id.frameLayout1);
frmLayout.setFocusable(true);
EditText et = new EditText(this);
frmLayout.addView(et,100,100);
frmLayout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("TESTING","touch x,y == " + event.getX() + "," + event.getY() );
frmLayout.setPadding(Math.round(event.getX()),Math.round(event.getY()) , 0, 0);
return true;
}
});
Related
Hello i would like to ask, how can i Display Toast near the user click point Like on image below using GraphView library:
http://android-graphview.org/
Thanks for any advice
EDIT:
I tried it using
seriesSin = new GraphViewSeries("Sinus curve", new GraphViewSeries.GraphViewSeriesStyle(Color.rgb(200, 50, 00), 3), data);
// SET ON TOUCH
graphView.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
int size = seriesSin.size();
float screenX = event.getX();
float screenY = event.getY();
float width_x = v.getWidth();
float viewX = screenX - v.getLeft();
float viewY = screenY - v.getTop();
float percent_x = (viewX/width_x);
int pos = (int) (size*percent_x);
System.out.println("X: " + viewX + " Y: " + viewY +" Percent = " +percent_x);
System.out.println("YVal = " +seriesSin.getY(pos));
return true;
}
return false;
}
});
But I cannot to get seriesSin.size and seriesSin.getY(pos)
You can't decide where to display the Toast. However, you could set up a custom view and show it using the coordinates (x and y) you get when detecting the touch. When you show the view, simply use those values to set up the layout params of the view, and voila.
Version 4.0.0 added an onTap() listener, that replicates your onTouch method above, it doesn't solve your Toast positioning issue, but could save you a few lines of code.
I have a linear layout that I have assigned an onTouchListener. I have gotten the width and height of the device but the problem is, the origin is at the top left of the screen and it's giving me coordinates like (534, 211) for example.
I would like the origin to be at the center of the screen and I would like the very ends of the screen coordinates to be 1.
How can I implement this?
This is the code I got so far...
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
System.out.println(width + " " + height);
mGLView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println(event.getX() + " " + event.getY());
return false;
}
});
Here is an image showing the kind of coordinate system I am trying to implement:
You should get get the coordinates (getX() and getY()) from the MotionEvent and normalize them into the coordinates you want. For example:
#Override
public boolean onTouch(View v, MotionEvent event)
{
float x = 2f * (event.getX() / v.getWidth()) - 1f;
float y = -2f * (event.getY() / v.getHeight()) + 1f;
System.out.println(x + " " + y);
return false;
}
(This assumes that you want your coordinate system relative to the view itself -- in your code you also query the display's dimensions, but I'm not sure why).
In general you can map a point from any range to any range using the following function:
For your case: toRange is (-1,1) and fromRange is (0,width) and (0,height)
double map(double input,double toMin, double toMax,
double fromMin, double fromMax){
return ((input - fromMin)/(fromMax - fromMin)) * (toMax - toMin) +toMin
}
You can then using it as follows:
map(250,-1,1,0,screenWidth)
map(300,-1,1,0,screenHeight)
You can build on top of this function whatever mapping you wish, for example
double center(double input, double maxValue) {
return map(input,-1,1,0,maxValue);
}
Then use it in your code:
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println(center(event.getX(),v.getWidth()) +
" " + center(event.getY(),v.getHeight()));
return false;
}
I'm trying to implement some kind of drag and drop on ImageView. It's working not too bad except one thing :
when I begin to drag object, there is a slight shift on ImageView and I can't resolve this problem.
For example, when I touch the view, I get on logcat :
ACTION_DOWN (margin) : 12x300
then (without moving)
ACTION_MOVE : (margin) 12x245
So the wiew is immediatly redraw and it's not very pretty !
The offset on Y axis between ACTION_DOWN and ACTION_MOVE seems to be depending on bitmap.
Here is my onTouch event code :
the layout left and top margin are set on screen.
public boolean onTouch(View v, MotionEvent event) {
FrameLayout.LayoutParams layout = (LayoutParams) v.getLayoutParams();
if (layout != null){
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.d( tag, "ACTION_DOWN (margin) : " + layout.leftMargin + "x" + layout.topMargin);
// finger position on current view
localX = (int) event.getX();
localY = (int) event.getY();
// initial position
parentX = (int) event.getRawX();
parentY = (int) event.getRawY();
v.bringToFront();
Log.d(tag, "ACTION_DOWN : Local " + localX + "x" + localY);
Log.d(tag, "ACTION_DOWN : Raw " + parentX + "x" + parentY);
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
// move current view on screen
layout.leftMargin = (int) event.getRawX() - localX;
layout.topMargin = (int) event.getRawY() - localY;
Log.d(tag, "ACTION_MOVE : (margin) " + layout.leftMargin+ "x" + layout.topMargin);
v.setLayoutParams(layout);
}
}
return true;
}
Is anybody can pointed me what i am doing wrong ?
Thanks.
I had a problem, which seems a bit similar. I solved it by setting gravity on the imageView's layoutParams to Gravity.NO_GRAVITY.
i want to add a view to a relativelayout.
this view must be added to a specified position.
i have this:
int x = ...;
int y = ...;
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
v.layout(x, y, x + v.getWidth(), y + v.getHeight());
return true;
}
});
relativelayout.addView(button);
button.layout(x, y, x + button.getWidth(), y + button.getHeight());
the ontouchlistener works great.. the view stays with my finger.
however the startpostion is always 0, 0... it doesn't matter what my x and y is.
anyone a idee?
i have a workaround.
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.leftMargin = x;
lp.topMargin = y;
relativelayout.addView(tmpTextButtonView, lp);
it is not great to use margin for positioning floating views. however it works for me.
You might want to try setting the position of your button before you add it to the parent view. So just swap these two lines:
relativelayout.addView(button);
button.layout(x, y, x + button.getWidth(), y + button.getHeight());
to be this:
button.layout(x, y, x + button.getWidth(), y + button.getHeight());
relativelayout.addView(button);
I've been working on Android for a while and would like to know if it is possible to retrieve the position of a button in android.
My target is to get the X & Y coordinates and print them on the LOGCAT.
Some example to show me how would be appreciated.
Thanks
Sure, you can get these, make sure the views are drawn atleast once before you try to get the positions. You could try to get the positions in onResume() and try these functions
view.getLocationInWindow()
or
view.getLocationOnScreen()
or if you need something relative to the parent, use
view.getLeft(), view.getTop()
Links to API definitions:
getLocationInWindow
getLocationOnScreen
getLeft
getTop
Like Azlam said you can use View.getLocationInWindow() to get the coordinates x,y.
Here is an example:
Button button = (Button) findViewById(R.id.yourButtonId);
Point point = getPointOfView(button);
Log.d(TAG, "view point x,y (" + point.x + ", " + point.y + ")");
private Point getPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
return new Point(location[0], location[1]);
}
Bonus - To get the center point of the given view:
Point centerPoint = getCenterPointOfView(button);
Log.d(TAG, "view center point x,y (" + centerPoint.x + ", " + centerPoint.y + ")");
private Point getCenterPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
int x = location[0] + view.getWidth() / 2;
int y = location[1] + view.getHeight() / 2;
return new Point(x, y);
}
I hope the example can still be useful to someone.
buttonObj.getX();
buttonObj.getY();