I want to display the popup in the position where the user clicked on the screen, I use the function - showAtLocation (mainView, Gravity.TOP or Gravity.START, posX, posY), everything is fine in the x coordinate, but the Y coordinate of the popup is added higher than that place where the user clicked, and if the main view (mainView) is scrolled, then the popup generally flies to the very bottom and I just can't figure out what's wrong with the Y position.
posX and posY is the position where user touch the screen
UPDATED
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
posY = event.y.toInt()
return super.dispatchTouchEvent(event)
}
Thank you for updating the question!
I think showAtLocation() uses total coordinates, which start at the top,left of your screen. But (without seeing the complete context of your code) I think you are taking relative coordinates for the x and y coordinates.
If the viewgroup where you override the function dispatchTouchEvent() has for example a 24dp margin top, the (0;0) coordinate of this view is placed 24dp below the global (0;0) coordinate. Thus when you press at the top,left corner of your viewgroup, event.y.toInt() will return (0;0) and you will return 0 as posY. But your click was 24dp below the total 0 coordinate on your screen. So you will show the popup window 24dp too high.
The same will happen when you scroll down in your mainView. your screen is e.g. 1000px high and you scroll down two times the screenlength. event.y.toInt() could return now 2000px as a relative y coordinate. But the highest screen height is 1000px, so the popup window will be shown at the bottom of your screen, even when you clicked in the middle of your screen.
Related
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);
Why is it that it is needed to specify absolute x and y co-ordinate values for pop up window object to be shown as drop down? Why does it eventhough not automatically show nearby of the control item that has been clicked enough, only for a one single time all though, once upon a time?
Thank you in advance.
From the android docs:
without cords
void showAsDropDown(View anchor) Display the content view in a popup
window anchored to the bottom-left corner of the anchor view.
with cords:
void showAsDropDown(View anchor, int xoff, int yoff) Display the
content view in a popup window anchored to the bottom-left corner of
the anchor view offset by the specified x and y coordinates.
From these 2 methods we can learn that there are 2 (actually 3) showAsDropDown methods.
The one without cords will do what you want automatically in the bottom-left corner. And the one with cords will just take some offset from the bottom-left corner. the offset will be determined by you (int xoff, int yoff), xoff stands for x offset and yoff stands for y offset.
Source
I have a smartphone of 2560 x 1440 px. Now I am using this function for my TextView:
int[] locationOnScreen = new int[2];
txtAp.GetLocationInWindow(locationOnScreen);
It is supposed to give me the total x and y coordinates in pixels.
My TextView is pretty much in the middle of the screen, so supposedly at
(1280,770). But the function returns [69, 1111].
How can that be? If that is not the way of doing that, then what is?
I have a smartphone of 2560 x 1440 px... My TextView is pretty much in the middle of the screen, so supposedly at (1280,770).
Not correct, unless you talk about the center of the TextView. Any view is rendered on screen within its rectangle, so its coordinates are considered to be of [left, top, right, bottom].
The GetLocationInWindow() method returns (left, top) coordinates of a view. So (x = 69, y = 1111) looks to be meaningful for the left top corner of your TextView located in the middle of screen.
Important note: GetLocationInWindow() returns the coordinates w.r.t the root view, not actual window. You should take the status bar height into consideration. And here is how you can calculate Height of status bar in Android.
I have the next question:
Im developing an app that when i move a imageView and drop it, if the view drops under the half height of the screen goes to a (X,Y) position and if is over the half height screen, goes to another position.
I need to calculate the half of the screen generic, so i use the next code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
halfHeight = displaymetrics.heightPixels / 2;
This works great, im trying in a screen 1920x1080, the code returns 540.
But when im going to see if when i drop the view is under or over the half, here is what i dont understand. i get the Y position of the view and is ok, what i dont understand is why the Y = 0 is not on the TOP of the screen, if i move the view to the top, i get a negative Y position like -260.
Someone can explain me why this happen?
Is there i way that the (0,0) position starts in the top left of the screen?
Greets, hope you understand
If you call getX() or getY() then you are getting relative values for x and y (relative to the view that the call was dispatched from). If you call getRawX() or getRawY() it will give you the absolute position of the view relative to the device's screen.
Most likely you are getting negative values of -260 because you are dragging the ImageView 260 away from the view in which the call was made (perhaps the view on the top of the screen has a height of 260). If you are trying to use getX() or getY() to calculate the middle of the screen then you would have to take all sizes of all views into consideration but I think you want to use getRawX() and getRawY()
An angle of 0 degrees correspond to the geometric angle of 0 degrees (3 o'clock on a watch.)
try using translate to translate the co-ordinates to top of the screen.
http://developer.android.com/reference/android/graphics/Canvas.html#translate(float, float)
I know I can use:
myPopup.showAtLocation(layout, Gravity.NO_GRAVITY, x, y);
// OR
myPopup.showAtLocation(layout, Gravity.TOP|Gravity.LEFT, x, y);
To open a PopupWindow that's drawn from [x, y] as the Top-Left of the PopupWindow, drawn towards to Bottom-Right.
What I want instead however, is to draw from [x, y] as the Top-Right of the PopupWindow, drawn towards the Bottom-Left.
Here is a picture to make it more clear (The dot is my [x, y] position and the rectangle is my PopupWindow. The first picture shows how it's normally done, and the second is what I want to achieve.):
So how do I correctly calculate the x and y of the second picture's gray point's location, while knowing the black point's location? I know the y can stay the same, but the x should be changed to something like:
x minus PopupWindow-width
The problem is that the PopupWindow's width and height are both set to wrap_content, so I don't know the size until after I draw it.
Does this mean I have to draw it (but make it invisible at first), then calculate the new x with the PopupWindow's MeasuredWidth in it's ViewTreeObserver's OnGlobalLayoutListener (to know when it's done rendering and the MeasuredWidth is known), then apply this new x and then make it Visible? Or is there an easier way to just let it draw at the correct position?
PS: I've also changed Gravity.NO_GRAVITY to Gravity.TOP|Gravity.RIGHT, if the PopupWindow is out of the screen it will automatically place it at the border of the Right/Top side (whichever side it's out of the screen).
You could get the size of your popup window by overriding the onMeasure method of the popup window (note that you have to subclass a View in order to do this). After that, you can calculate the offset of the x and y coordinates. Hope this helps.
I just want to know, isn't the left side coordinate of any UI element is the Y, and the top coordinate is the X, the bottom coordinates is the X and the right coordinates is the Y?
is it the correct coordinate system of the screen, because the (0,0) are placed at the top-left corner of the screen? if so, why oval.set(.., .., .. ..) does not comply to the coorsinates system that starts from the top-left corner (x,y)? in the documentation of, it is states the following:
Parameters
left The X coordinate of the left side of the rectangle
top The Y coordinate of the top of the rectangle
right The X coordinate of the right side of the rectangle
bottom The Y coordinate of the bottom of the rectangle
You probably want to use View.getLocationOnScreen() to get your view coordinates
It will return pix offset values relative to screen's TOP-LEFT point.
Note: getLocationOnScreen() cannot be called before layout buildup is completed.
if in doubt is a ViewTreeObserver to be notified when layout is completed.
Note 2: The above offsets will change once screen orientation changes (landscape vs. portrait).
If important to your app's processing you might want to declare a listener for that event.
See function onConfigurationChanged()