android PopupMenu showAtLocation y offset - android

Does anyone know why when I use this code:
popupMenu.showAtLocation(containerView, Gravity.BOTTOM|Gravity.LEFT, x_offset, y_offset);
The y offset doesn't actually move the popup until it's greater than a certain number (in this case approx 120). For values under 120, nothing happens; for values over 120, the popup moves vertically up.
I'm guessing it's to do with the size of the popup, for which there doesn't seem to be a way to actually get the dimensions of to compensate for this threshold.
I'd like to know because a) there's scant information about this method, and b) I can't seem to get it to work to position my popup correctly.

Pretty old question but I would like to mention here that in case if anyone else is wondering what is the threshold for the yOffset - it is the top insets of the view you are passing to the showAtLocation() method.
In most cases this is your system bar height.
You can try to calculate the yOffset by
int desiredOffsetPx = 250;
int yOffset = desiredOffsetPx + systemWindowInsetTop
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, yOffset)
In order to get systemWindowInsetTop please check this answer

void showAtLocation (View parent,
int gravity,
int x,
int y)
Display the content view in a popup window at the specified location. If the popup window cannot fit on screen, it will be clipped. See WindowManager.LayoutParams for more information on how gravity and the x and y parameters are related. Specifying a gravity of NO_GRAVITY is similar to specifying Gravity.LEFT | Gravity.TOP.
You can find whole detail on this link : deveoper.android.com

Related

Setting the x axis position to y = 0 in MPAndroid Bar Chart

I've been struggling with this for a while now, but I can't find a way to have an x axis, with its labels, lie at the position y = 0. This is important when I have both positive and negative values- I don't want to put the axis at the defaults given by AndroidMPChart (TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE), as none of these apply.
I have managed to edit the xAxisRenderer, and have been able to pass the ratio of my maxY value to my minY value, hoping to manually find the position to set the axis. The issue with this, however, is that I need the position of the highest Y bar and lowest Y bar. I can find the position of the lowest Y bar no problem, using mViewPortHandler.contentBottom(), but using
mViewPortHandler.contentTop() does not give me the top of the Y bar for some reason.
List<Float> floatList = new ArrayList<>();
floatList.add(0f);
floatList.add(mViewPortHandler.contentTop());
floatList.add(mViewPortHandler.offsetTop()+mViewPortHandler.getScaleY());
floatList.add(mViewPortHandler.getContentCenter().y);
floatList.add(mViewPortHandler.contentBottom());
floatList.add(mViewPortHandler.getChartHeight());
if (mDebug) {
for (int i=0; i<floatList.size(); i++) {
Paint paint = new Paint();
paint.setColor(getRandomColorInColorFormPrintString("i = " + i));
paint.setStrokeWidth(3);
c.drawLine(mViewPortHandler.contentLeft()+15*i,
floatList.get(i), mViewPortHandler.contentRight()+15*i,
floatList.get(i), paint);
}
}
Notice how contentBottom (yellow) nails the bottom of the bar, but content top and offset top (pink/red) are not accurate in finding the top of the content. I've tried this with many different values, contentBottom is consistently accurate, whereas contentTop/offsetTop are not, and are not offset by the height of the value label (I already tried this as well).
All I really need is a dynamic way to set the position of the x axis right between positive and negative values. I am so surprised that this option doesn't come with Android MP Charts. If it does, please let me know if I am overcomplicating things.
If there is no way to set this through Android MP Charts, please let me know if you know what I'm doing wrong in trying to get the top of the Y bar.
This is how I solved the problem--
in the renderAxisLine function =>
MPPointD pos = mTrans.getPixelForValues(0f, 0f);
c.drawLine(mViewPortHandler.contentLeft(),
(float)pos.y, mViewPortHandler.contentRight(),
(float)pos.y, mAxisLinePaint);

Pop up window / show as drop down

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

View coordinate in Screen

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)

Determining translationX/Y values for ObjectAnimator; how to move a view to an exact screen position?

I'm trying to move a view to the upper right corner of the screen, using ObjectAnimator.ofFloat(...) But, I'm not getting the results I expect. I'm getting the coordinates of the view beforehand, using ViewTreeListener, etc, and I already know the x value I need to offset from the end of the overall width. I can't get either dimension to move to where I want it. Relevant code:
Getting the starting coordinate; where the view currently is:
int[] userCoords = new int[]{0,0};
userControlLayout.getLocationInWindow(userCoords);
//also tried getLocationInScreen(userCoords); same result
userUpLeft = userCoords[0];
userUpTop = userCoords[1];
Surprisingly, I get the same value as userUpLeft (which is in screen coordinates, and not relative to parent) when I call userControlLayout.getLeft() I'd expect them to be different per my understanding of the docs. Anyway...
Constructing the ObjectAnimators:
//testXTranslate is a magic number of 390 that works; arrived at by trial. no idea why that
// value puts the view where I want it; can't find any correlation between the dimension
// and measurements I've got
ObjectAnimator translateX = ObjectAnimator.ofFloat(userControlLayout, "translationX",
testXTranslate);
//again, using another magic number of -410f puts me at the Y I want, but again, no idea //why; currently trying the two argument call, which I understand is from...to
//if userUpTop was derived using screen coordinates, then isn't it logical to assume that -//userUpTop would result in moving to Y 0, which is what I want? Doesn't happen
ObjectAnimator translateY = ObjectAnimator.ofFloat(userControlLayout, "translationY",
userUpTop, -(userUpTop));
My understanding is that the one arg call is equivalent to specifying the end coordinate you want to translate/move to, and the two arg version is starting at...ending at, or, from...to I've messed around with both and can't get there.
Clearly, I'm missing very fundamental knowledge, just trying to figure out what exactly that is. Any guidance much appreciated. Thanks.
First, userControlLayout.getLeft() is relative to the parent view. If this parent is aligned to the left edge of the screen, those values will match. For getTop() it's generally different simply because getLocationInWindow() returns absolute coordinates, which means that y = 0 is the very top left of the window -- i.e. behind the action bar.
Generally you want to translate the control relative to its parent (since it won't even be drawn if it moves outside those bounds). So supposing you want to position the control at (targetX, targetY), you should use:
int deltaX = targetX - button.getLeft();
int deltaY = targetY - button.getTop();
ObjectAnimator translateX = ObjectAnimator.ofFloat(button, "translationX", deltaX);
ObjectAnimator translateY = ObjectAnimator.ofFloat(button, "translationY", deltaY);
When you supply multiple values to an ObjectAnimator, you're indicating intermediate values in the animation. So in your case userUpTop, -userUpTop would cause the translation to go down first and then up. Remember that translation (as well as rotation and all the other transformations) is always relative to the original position.

PopupWindow at a specific coordinate with no anchor view

I have a custom ImageView and in the OnDraw I have added some bitmap overlays. I can capture the click event for any overlay using action up type event in the OnTouchEvent callback. The thing is for me to display a popup window I must supply a view anchor for the popup window to show but I am using the bitmaps as anchors not a particular view, so I am stuck. I cannot find any sollution on the web regarding this issue. Passing the parent ImageView and adding the overlay x-y offsets (with Gravity.NO_GRAVITY) are not producing the desired result(Also the window arrow will not point correctly).
Use PopupWindow.showAtLocation
The problem is that x/y params into that function is offset relative to Window, to which anchor view belongs. You can see it clearly from function's source, where nothing from 'parent' is used except of its window token.
public void showAtLocation(View parent, int gravity, int x, int y) {
showAtLocation(parent.getWindowToken(), gravity, x, y);
}
So to make your offsets good, you have to determine anchor's position relative to its window.
For this, use
Rect rc = new Rect();
View.getWindowVisibleDisplayFrame(rc);
int[] xy = new int[2];
View.getLocationInWindow(xy);
rc.offset(xy[0], xy[1]);
now you have
inx x = rc.left, y = rc.top;
This is point of your ImageView's left-top corner relative to its window.
Finally show your PopupWindow at location relative to this point, and it should display correctly on top of your ImageView.
You referring to the below kind of view right...I would suggest you to follow the below link and implement the same in your case as well..If found difficult post ur comment and will get back to you..MAPViewBallons

Categories

Resources