How to detect a layout resize?

AndroidUser InterfaceLayoutResize

Android Problem Overview


Dianne Hackborn mentioned in a couple threads that you can detect when a layout as been resized, for example, when the soft keyboard opens or closes. Such a thread is this one... http://groups.google.com/group/android-developers/browse_thread/thread/d318901586313204/2b2c2c7d4bb04e1b

However, I didn't understand her answer: "By your view hierarchy being resized with all of the corresponding layout traversal and callbacks."

Does anyone have a further description or some examples of how to detect this? Which callbacks can I link into in order to detect this?

Thanks

Android Solutions


Solution 1 - Android

One way is View.addOnLayoutChangeListener. There's no need to subclass the view in this case. But you do need API level 11. And the correct calculation of size from bounds (undocumented in the API) can sometimes be a pitfall. Here's a correct example:

view.addOnLayoutChangeListener( new View.OnLayoutChangeListener()
{
    public void onLayoutChange( View v,
      int left,    int top,    int right,    int bottom,
      int leftWas, int topWas, int rightWas, int bottomWas )
    {
        int widthWas = rightWas - leftWas; // Right exclusive, left inclusive
        if( v.getWidth() != widthWas )
        {
            // Width has changed
        }
        int heightWas = bottomWas - topWas; // Bottom exclusive, top inclusive
        if( v.getHeight() != heightWas )
        {
            // Height has changed
        }
    }
});

Another way (as dacwe answers) is to subclass your view and override onSizeChanged.

Solution 2 - Android

Override onSizeChanged in your View!

Solution 3 - Android

My solution is to add an invisible tiny dumb view at the end of of the layout / fragment (or add it as a background), thus any change on size of the layout will trigger the layout change event for that view which could be catched up by OnLayoutChangeListener:

Example of adding the dumb view to the end of the layout:

 <View 
    android:id="@+id/theDumbViewId"
    android:layout_width="1dp"
    android:layout_height="1dp"
     />

Listen the event:

	View dumbView = mainView.findViewById(R.id.theDumbViewId);
	dumbView.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) {
            // Your code about size changed
		}
	});

Solution 4 - Android

With Kotlin extensions:

inline fun View?.onSizeChange(crossinline runnable: () -> Unit) = this?.apply {
    addOnLayoutChangeListener { _, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
        val rect = Rect(left, top, right, bottom)
        val oldRect = Rect(oldLeft, oldTop, oldRight, oldBottom)
        if (rect.width() != oldRect.width() || rect.height() != oldRect.height()) {
            runnable();
        }
    }
}

Use thus:

myView.onSizeChange { 
     // Do you thing...
}

Solution 5 - Android

Thank to https://stackoverflow.com/users/2402790/michael-allan this is the good and simple way if you don't want to override all your views. As Api has evolved I would suggest this copy paste instead:

    String TAG="toto";
    ///and last listen for size changed
    YourView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v,
                                   int left, int top, int right, int bottom,
                                   int oldLeft, int oldTop, int oldRight, int oldBottom) {

           boolean widthChanged = (right-left) != (oldRight-oldLeft);
            if( widthChanged )
            {
                // width has changed
                Log.e(TAG,"Width has changed new width is "+(right-left)+"px");
            }
            boolean heightChanged = (bottom-top) != (oldBottom-oldTop);
            if( heightChanged)
            {
                // height has changed
                Log.e(TAG,"height has changed new height is "+(bottom-top)+"px");
            }
        }
    });

Solution 6 - Android

View.doOnLayout(crossinline action: (view: View) -> Unit): Unit

See docs.

> Performs the given action when this view is laid out. If the view has been laid out and it has not requested a layout, the action will be performed straight away, otherwise the action will be performed after the view is next laid out.

The action will only be invoked once on the next layout and then removed.

Solution 7 - Android

Kotlin version base on @Michael Allan answer to detect layout size change

binding.root.addOnLayoutChangeListener { view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
    if(view.height != oldBottom - oldTop) {
        // height changed
    }
    if(view.width != oldRight - oldLeft) {
        // width changed
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncottonBallPawsView Question on Stackoverflow
Solution 1 - AndroidMichael AllanView Answer on Stackoverflow
Solution 2 - AndroiddacweView Answer on Stackoverflow
Solution 3 - AndroidTonyView Answer on Stackoverflow
Solution 4 - AndroidSlionView Answer on Stackoverflow
Solution 5 - AndroidMathias Seguy Android2eeView Answer on Stackoverflow
Solution 6 - AndroidLuisView Answer on Stackoverflow
Solution 7 - AndroidLinhView Answer on Stackoverflow