While I may not fully understand your problem, I think this is along the lines of what you are looking for. This will get the current view and measure the height and width. If you are doing automated tests then I think ViewTreeObserver
is
safer than using View
, which is why I included ViewTreeObserver
instead. I am pretty sure that this will return the width and height of the current view, which will be different then what the screen resolution is.
ViewTreeObserver viewTreeObserver = YOUR_VIEW_TO_MEASURE.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
YOUR_VIEW_TO_MEASURE.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int viewHeight = YOUR_VIEW_TO_MEASURE.getHeight();
int viewWeight = YOUR_VIEW_TO_MEASURE.getWidth();
}
});
}
If you want to compare this to the current resolution of the device, not the view, it looks like you have already found and linked a correct solution in your question.
Note: if you are testing your program, make sure that you are calling this after you have created your view because if you call this before it might return 0
or the incorrect view sizes.