Android: How to Programmatically set the size of a Layout
Java
This should work:
// Gets linearlayout
LinearLayout layout = findViewById(R.id.numberPadLayout);
// Gets the layout params that will allow you to resize the layout
LayoutParams 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
How to programmatically set the width of the LinearLayout?
To set the LinearLayout
or TextView
width to 1/3 of the device screen:
first of all get the device screen width:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
display.getRealSize(size);
} catch (NoSuchMethodError err) {
display.getSize(size);
}
int width = size.x;
int height = size.y;
now just create a LayoutParams
and set it to the Text:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
LinearLayout.LayoutParams.WRAP_CONTENT); // or set height to any fixed value you want
your_layout.setLayoutParams(lp);
// OR
your_textView.setLayoutParams(lp);
Change LinearLayout height to 0 programmatically
Try this.....
LinearLayout layout = (LinearLayout)findViewById(R.id.yourLayoutId);
LinearLayout.LayoutParams lp = (LayoutParams) layout.getLayoutParams();
lp.height = 0;
Adjusting height of linear layout programmatically
This should work
LinearLayout lLayout = new LineaLayout(context);
LayoutParams params = lLayout.getLayoutParams();
params.height = 200;
params.width = 200;
lLayout.setLayoutParams(params);
see the accepted answer at Android: How to Programmatically set the size of a Layout
How can I set layout height and width programmatically which support all screen size in Android?
You can get the device density like this:
float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20 * scaledDensity,20 * scaledDensity);
param1.weight=1;
How to set specific width/height for view programmatically?
You can do like this. Width/Height should be a pixel value.
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
getResources().getDimensionPixelSize(R.dimen.width),100);
textView.setLayoutParams(layoutParams);
You can Read from dimens resource or hardcode a pixel value.
How to resize a custom view programmatically?
this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));
Problem solved!
NOTE: Be sure to use the parent Layout's LayoutParams
. Mine is LinearLayout.LayoutParams
!
Android set height and width of Custom view programmatically
If you know the exact size of the view, just use setLayoutParams()
:
graphView.setLayoutParams(new LayoutParams(width, height));
Or in Kotlin:
graphView.layoutParams = LayoutParams(width, height)
However, if you need a more flexible approach you can override onMeasure()
to measure the view more precisely depending on the space available and layout constraints (wrap_content
, match_parent
, or a fixed size). You can find more details about onMeasure()
in the android docs.
Related Topics
How to Change Background Color Popup Menu Android
How to Finish an Android Application
How to Get Free and Total Size of Each Storagevolume
How to Do Page Flip/Turn/Curl Effect in Android
Load Large Images with Picasso and Custom Transform Object
How to Pop Fragment Off Backstack
Android Listview Setselection() Does Not Seem to Work
Android Studio: Unable to Start the Daemon Process
How to Tell Which App Was Selected by Intent.Createchooser
Start a Fragment via Intent Within a Fragment
Render Error in Android Studio 3.0 Layout Editor
Error Java.Lang.Runtimeexception: Stub! in Android with Fitnesse Testing
Android Youtubeplayer with Unauthorized Overlay on Top of Player
Android Internet Connectivity Check Problem
How to Hide a Folder in Sdcard in Android
Error Non-Default Constructors in Fragments