Stellarworks
Welcome Guest [Login] or [Register] 
    
Ordering FAQ    View Shopping Cart (0)

Code Snippet - Change Layout Parameters at Runtime
Android
25-10-2011

The layout parameters of UI controls that are normally set by xml layouts can be changed at run time. Just cast the returned LayoutParam object from getLayoutParams() to the appropriate object type to change element specific properties. Each UI element has it's own LayoutParam, accessed through an explicit namespace. The following code works in Android 1.5, and should work in subsequent versions.

 Code 
//Change Layout Weight when in landscape mode
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
  LinearLayout mainLayout = (LinearLayout)findViewById(R.id.linearLayout4);
        
  if(mainLayout != null){
   android.widget.LinearLayout.LayoutParams currentParams = (android.widget.LinearLayout.LayoutParams)mainLayout.getLayoutParams();
    if(currentParams != null){
       currentParams.weight = 1.6f;
       mainLayout.setLayoutParams(currentParams);
    }
  }
}//End in landscape



ABN 16685017405
Stellarworks® 2011-15