26

Suppose I have an Android View that is being partially masked by another View. For instance, imagine that the masking view has a transparent rectangular "window" in the center of it, and my view is centered beneath the window and possessing dimensions that are larger than the dimensions of the window. In such a case, some rectangular portion of the view will be visible, with the rest being obscured by the mask.

So my question is, is there any straightforward way to retrive the position and dimensions of the visible area without having any access to the masking view itself (so without knowing how big the mask's "window" itself is)?

I tried calling getLocalVisibleRect(), which sounded promising, but that only seems to return the view's layout rectangle and not the rectangle that is actually visible.

aroth
  • 54,026
  • 20
  • 135
  • 176
  • 4
    got any solution for this? having the same question.. – user936414 Jan 22 '13 at 06:37
  • Seems duplicate (but without answers): http://stackoverflow.com/questions/34724179/how-to-find-whether-an-android-view-is-obscured-by-any-other-view – Alex Cohn Jul 06 '16 at 21:46
  • ever figured this out? – Nima Jul 06 '17 at 19:14
  • 1
    Negative. And so long ago now that I can't even remember _why_ I needed to figure this out. – aroth Jul 07 '17 at 03:46
  • @aroth i want to use this, in my scenario my view is getting hidden by keyboard and i am not using `adjustresize` so keyboard is overlapping the View and i want only the visible area. – Anmol Dec 21 '18 at 05:48

3 Answers3

12

This is pretty late to the game, but I suspect that getGlobalVisibleRect does what you want.

Abhay Buch
  • 4,548
  • 1
  • 21
  • 26
  • It works for the simple cases only, when the view wasn't rotated. – aga Mar 18 '15 at 08:13
  • 15
    Late reply, but getGlobalVisibleRect only seems to calculate visible portion if the view is restricted (cut off) by one of its parents. Not if another view is overlaying it in something like a relativelayout. In that case, getGlobalVisibleRect gives the rect of the whole view, not just the portion that's uncovered by the masking view – jacosta Jun 11 '15 at 14:52
5

I have used getWindowVisibleDisplayFrame method to determine if view is partially visible or not to determine if a soft keyboard is open. You can try it out.

Rect r = new Rect();
// r will be populated with the coordinates of     your view
// that area still visible.
rootView.getWindowVisibleDisplayFrame(r);
mou
  • 323
  • 3
  • 10
1

Does this not work: http://developer.android.com/reference/android/view/View.html#getDrawingRect(android.graphics.Rect)

The documentation says that it does what you want.

Vikram Bodicherla
  • 7,133
  • 4
  • 28
  • 34
  • 2
    Nope, it gives the same result as just calling `getWidth()` and `getHeight()` (i.e. the total view size). As the documentation says, `getDrawingRect()` "fills in the output rectangle with the values from `getScrollX()`, `getScrollY()`, `getWidth()`, and `getHeight()`". The `scrollX` and `scrollY` values will be 0 in this case, because there is no scrolling going on. – aroth Jan 31 '12 at 05:42