Sohaib Sohaib - 4 months ago 17
Android Question

Subclassing LinearLayout, how to determine gravity is LEFT or RIGHT

I am subclassing

LinearLayout
. In the subclass I can't figure out how to get the gravity value. I simply want to determine if the gravity set in xml layout is
Gravity.LEFT
or
Gravity.RIGHT


Thanks in advance for any help.

Answer

Try to get gravity from parent layout params :

int gravity = ((LinearLayout.LayoutParams)((LinearLayout)childView.getParent()).getLayoutParams()).gravity;
if(gravity == Gravity.LEFT){
   // write your code when parent gravity is left           
}else if(gravity == Gravity.RIGHT){
   // write your code when parent gravity is right           
}else if(gravity == (Gravity.LEFT | Gravity.CENTER_VERTICAL)){
  // write your code when parent gravity is left with center vertical
else{
  // write your code when parent gravity is neither right nor left.
}