Android - Listener for catching changes in a View's properties (e.g. android:layout_marginTop)

Jack picture Jack · Oct 8, 2013 · Viewed 15.7k times · Source

In Android, can you create a Listener for catching changes in a View's properties (width / height / margin / position relative to top of the screen)?

I want to trigger an event when layout_marginTop="10dp" is changed to a different value.

Answer

chuck258 picture chuck258 · Oct 8, 2013

What about implementing a OnLayoutChangeListener that gets called when a View is moved due to Layout Change

new View().addOnLayoutChangeListener(new OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
                    int oldTop, int oldRight, int oldBottom) {
        // TODO Auto-generated method stub  
    }
});

Excerpt from Android API:

Add a listener that will be called when the bounds of the view change due to layout processing.