Search code examples
androidandroid-layoutandroid-custom-viewdevice-orientationlayoutparams

android LayoutParams updates ONLY after device orientation change


In my activity i have multiple views in a ViewFlipper with a button, like following, in every layout:

<com.keckardt.pairs.views.SquareButton
    android:id="@+id/button_multi_wifi"
    android:layout_width="@dimen/buttonSize"
    android:layout_height="@dimen/buttonSize"
    android:layout_margin="@dimen/buttonMargin"
    app:layout_column="0"
    app:layout_row="1"
    android:onClick="onMultiplayerWifi"
    android:src="@raw/ic_wifi"
    android:text="@string/wlan" />

In my custom view(derived from RelativeLayout) im chaning the layout the following way:

 params = (LayoutParams) icon.getLayoutParams();

 params.width = iconSize;
 params.height = iconSize;
 params.topMargin = topMargin;

 icon.setLayoutParams(params);

But the LayoutParams only "work" on the first View of the ViewFlipper. The other buttons dosen't update their layout until I change the device orientation. But when changing the orientation, the method for editing and setting the LayoutParams is never called! Instead, the "old" parameters are displayed suddenly.

Is there any special about CustomViews and LayoutParams of their children?


Solution

  • params = (LayoutParams) icon.getLayoutParams();
    
    params.width = iconSize;
    params.height = iconSize;
    params.topMargin = topMargin;
    
    icon.setLayoutParams(params);
    //Old way - your way.
    

    Try to set the width and the height like this:

     //first iconSize is width, second is height.
     LayoutParams params = new LayoutParams(iconSize, iconSize);
     params.setMargins(left, top, right, bottom); //Set whatever you need here and zero to where you need no margins.
    
     icon.setLayoutParams(params);
    

    This is how I always set layout's params programmatically.Also, you can specify exactly what sort of params are.For example LinearLayout.LayoutParams.Or RelativeLayout.LayoutParams, or whatever sort of Layout you'v got.

    If you need more info, leave a comment.