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.
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.
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?
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.
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.
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();
}
}
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
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With