The problem
I have a custom Android view in which I want get the user set gravity in order to layout the content in onDraw. Here is a simplified version that I am using in onDraw:
// check gravity
if ((mGravity & Gravity.CENTER_VERTICAL) == Gravity.CENTER_VERTICAL) {
// draw the content centered vertically
} else if ((mGravity & Gravity.BOTTOM) == Gravity.BOTTOM) {
// draw the content at the bottom
}
where mGravity is obtained from the xml attributes (like this).
If I set the gravity to Gravity.CENTER_VERTICAL it works fine. But I was surprised to find that if I set it to Gravity.BOTTOM, the Gravity.CENTER_VERTICAL check is still true!
Why is this happening?
I had to look at the binary values to see why:
- Binary:
0001 0000,Gravity.CENTER_VERTICAL: Constant Value: 16 (0x00000010) - Binary:
0101 0000,Gravity.BOTTOM: Constant Value: 80 (0x00000050)
Thus, when I do
mGravity = Gravity.BOTTOM;
(mGravity & Gravity.CENTER_VERTICAL) == Gravity.CENTER_VERTICAL
// (0101 & 0001) == 0001
I get a false positive.
What do I do?
So how am I supposed to check the gravity flags?
I could do something like if (mGravity == Gravity.CENTER_VERTICAL), but then I would only get an exact match. If the user set gravity to something like center_vertical|right then it would fail.