Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a view detect when it's being attached to its parent?

I have a View that needs to detect when it's being attached to its parent view.

In a ViewGroup I have the OnHierarchyChangeListener which allows me to know when a child view is being added / removed, but I need the opposite thing.

like image 238
user940016 Avatar asked Dec 28 '12 11:12

user940016


People also ask

Who can assess a child's attachment style?

Only specially trained and qualified professionals should assess a child’s attachment style. However, it’s important for all adults working with children to understand what attachment is and know how to help parents and carers become attuned to their child’s needs.

What happens to the parent view when the child is destroyed?

When a parent fragment ceases to exist, the child will also cease to exist. The child cannot exist without the parent but you can have the parent without the child. The bottom line for nested views is that once the parent view is destroyed, it takes the child view immediately with it. Are views measured before they are attached or after?

How do you know if your baby has an attachment?

This includes recognising if their baby is hungry, feeling unwell or in need of closeness and affection (Howe, 2011) 3. Forming an attachment is something that develops over time for a child, but parents and carers can start to form an emotional bond with their child before they are born.

Is your child not securely attached to their parents?

Recent reports reveal that a shocking high number of children are not securely attached to their parents. Forty percent of U.S. children lack strong emotional bonds with their parents and hence are likely to have an insecure attachment style, according to a report published by Sutton Trust.


Video Answer


2 Answers

You can create custom view and do your stuff in its onAttachedToWindow

public class CustomView extends View {

   public CustomView(Context context) {
       super(context);
   }

   @Override
   protected void onAttachedToWindow() {
       super.onAttachedToWindow();
       Log.d("CustomView", "onAttachedToWindow called for " + getId());
       Toast.makeText(getContext(), "added", 1000).show();
   }
}

[EDIT 1]

you can ensure that your customview added to correct viewgroup which you want

@Override
 protected void onAttachedToWindow() {
    // TODO Auto-generated method stub
    super.onAttachedToWindow();

    if(((View)getParent()).getId()== R.id.relativelayout2)
    {           
        Log.d("CustomView","onAttachedToWindow called for " + getId());
        Toast.makeText(context, "added", 1000).show();          
    }

}
like image 65
Talha Avatar answered Oct 27 '22 20:10

Talha


Solution without having to create a custom view

If the View is one you can't or don't want to modify, you can use View.addOnAttachStateChangeListener to get hooks when the view is attached to the view hierarchy and when it is detached from the view hierarchy.

Something like:

Kotlin

view.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
  override fun onViewAttachedToWindow(v: View) {
    // Do stuff when view has been attached to window
  }

  override fun onViewDetachedFromWindow(v: View) {
    // Do stuff when view has been detached from window
  }
})

Java

view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
  @Override
  public void onViewAttachedToWindow(View v) {
    // Do stuff when view has been attached to window
  }

  @Override
  public void onViewDetachedFromWindow(View v) {
    // Do stuff when view has been detached from window
  }
});
like image 20
Michael Krause Avatar answered Oct 27 '22 19:10

Michael Krause