I am working on a feature in my app where I need to know the exact height of the parent view. But apparently this value is wrong on some devices (e.g. my Huawei P8 lite).
I realized it was wrong when I tried drawing something at point (x, view.getheight), where the point didn't show up at the bottom, but rather near the middle of the screen. This was not the case on my Google Nexus.
Here is the snippet of code where the problem lies:
final OverlayBuilder builder = new OverlayBuilder(context, parentView, tag);
builder.setOnLayoutListener(new ServiceCallback<OverlayBuilder.OverlayRelativeLayout>() {
@Override
public void onSuccess(OverlayBuilder.OverlayRelativeLayout overlayLayout) {
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) createAgentBtn.getLayoutParams();
LinearLayout childContainer = overlayLayout.getChildContainer();
int parentHeight = parentView.getHeight();
View iconView = childContainer.getChildAt(0);
View textView = childContainer.getChildAt(1);
int noContentViewHeight = iconView.getHeight() + textView.getHeight();
int btnHeight = createAgentBtn.getHeight() + lp.topMargin + lp.bottomMargin;
int bottomOffset = ((parentHeight - btnHeight) / 2) - (noContentViewHeight / 2);
int aStartY = bottomOffset + noContentViewHeight;
int aEndY = parentHeight - btnHeight;
ArrayList<Integer> layoutRules = new ArrayList<>(RelativeLayout.ALIGN_PARENT_BOTTOM);
builder.setGroupParams(layoutRules, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT, 0, bottomOffset, LinearLayout.VERTICAL)
.drawCurvedArrow(300, aStartY, 300, aEndY, -225, R.color.turtle_green, OverlayBuilder.STANDARD_LINESIZE, OverlayBuilder.STANDARD_ARROWSIZE).layout().commit();
}
@Override
public void onFail(Throwable t) {
Log.e(tag, "Failed to display empty state onboarding");
}
});
For the record, the class called OverlayBuilder is a builder class, used for drawing different things on the screen at runtime. In the case I am drawing an arrow between 2 points, but both the beginning and the end of the line/arrow is completely wrong on my Huawei P8.
I suspect that getHeight() returns dp in some cases, and in px on my Google Nexus. If this could be the case, how can this be fixed properly? The variable parentHeight, is the one that contains the wrong height.