Ricardo Filipe
Ricardo Filipe

Reputation: 445

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.

Upvotes: 2

Views: 8989

Answers (4)

Frohnzie
Frohnzie

Reputation: 3569

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
}

Upvotes: 6

Ricardo Filipe
Ricardo Filipe

Reputation: 445

The solution.....

      LinearLayout contentLayoutAddressPostal = (LinearLayout)findViewById(R.id.se_contentAdressPostal);

     ImageButton imbtRemoveAddress2 = (ImageButton)findViewById(R.id.sfsp2_ivHide);

       if(imbtRemoveAddress2.getParent()==contentLayoutAddressPostal){
          imbtRemoveAddress2.performClick();
                                    ............................

Upvotes: 0

Frank Sposaro
Frank Sposaro

Reputation: 8531

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
}

Upvotes: 13

comodoro
comodoro

Reputation: 1566

Try searching the UI tree via the getParent() method

Upvotes: 0

Related Questions