Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a view belongs to a linearlayout or not

I need a little help. How I can check is a view belongs to a linearlayout or not?

I have an ImageButton and i need a condition that verify if belongs to a linearlayout or not.

like image 701
Ricardo Filipe Avatar asked Nov 29 '25 06:11

Ricardo Filipe


2 Answers

Haven't tried this, but it should work. Assuming that your ImageButton is always a direct child of your LinearLayout.

View parent = (View)mContent.getParent();
if (parent instanceof LinearLayout) {
    // do stuff
}
like image 148
Frank Sposaro Avatar answered Nov 30 '25 21:11

Frank Sposaro


You can use the findViewById method on the LinearLayout. From the JavaDoc "Look for a child view with the given id. If this view has the given id, return this view."

LinearLayout layoutWithButton = (LinearLayout)findViewById(R.id.layout_with_button);
ImageButton buttonInLayout = (ImageButton)layoutWithButton.findViewById(R.id.button_in_layout);

if (buttonInLayout != null) {
  // Found
}
like image 40
Frohnzie Avatar answered Nov 30 '25 19:11

Frohnzie