Android: How to Programmatically set the size of a Layout Android: How to Programmatically set the size of a Layout android android

Android: How to Programmatically set the size of a Layout


Java

This should work:

// Gets linearlayoutLinearLayout layout = findViewById(R.id.numberPadLayout);// Gets the layout params that will allow you to resize the layoutLayoutParams params = layout.getLayoutParams();// Changes the height and width to the specified *pixels*params.height = 100;params.width = 100;layout.setLayoutParams(params);

If you want to convert dip to pixels, use this:

int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>, getResources().getDisplayMetrics());

Kotlin


my sample code

wv = (WebView) findViewById(R.id.mywebview);wv.getLayoutParams().height = LayoutParams.MATCH_PARENT; // LayoutParams: android.view.ViewGroup.LayoutParams// wv.getLayoutParams().height = LayoutParams.WRAP_CONTENT;wv.requestLayout();//It is necesary to refresh the screen


LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(                       /*width*/ ViewGroup.LayoutParams.MATCH_PARENT,               /*height*/ 100,               /*weight*/ 1.0f                );                YOUR_LinearLayout.setLayoutParams(param);