user529543
user529543

Reputation:

RelativeLayout check runtime if is removed or not

In an activity I am changing one of the ViewGroup content runtime: buttons action, other events.

At a specific case I need to check if a child is in this layout or not ( in this case the child is another RelativeLayout, which hold other views)

How can I check runtime, programmatically if child_1_RelativeLayout is there or is already removed from view tree, his parent is parentRelativeLayout

The getParent() is usefully? - not much explanation how to use it, thanks.

Upvotes: 0

Views: 853

Answers (2)

MatrixDev
MatrixDev

Reputation: 1458

In case you have stored your views you can use getParent() to check if view is a direct child for other view. After removing view its parent field will be cleared. For example:

ViewGroup parent = ...;
View child = ...;

assert(child.getParent() == parent); // <-- true

parent.removeView(child);

assert(child.getParent() == parent); // <-- false

Upvotes: 2

baske
baske

Reputation: 1352

Not really sure if I understand your question correctly, but:

  • when you add the relative layout, be sure to first give it an id (either in your layout.xml file, or from code)
  • to check existence of the relative layout within the ViewGroup, use ViewGroup's findViewById() method (inherited from View) and pass it the id you've given to the relative layout
  • if it returns null, the relative layout is not there, otherwise findViewById() will find it

So in short, findViewById() is not only defined for an Activity, but you can call this on any view you would like to use as a starting point for your search.

Upvotes: 1

Related Questions