Hi i am using following layout structure inside LinearLayout
I faced this problem, the best solution for Change Relative layout width and height dynamically like this
RelativeLayout relMain = (RelativeLayout) convertView.findViewById(R.id.relMain);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width =200;
params.height =200;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relMain.setLayoutParams(params);
If you intending to change Height, then this would make a trick.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;
You can set views dimensions dynamically using LayoutParams. Something like this:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
Android does NOT refresh layout of views with wrap_content
once it has been displayed.
Even with invalidate()
or requestLayout()
.
So if you add a child view, or modify the content dynamically, you're screwed.
getLayoutParams().height = x
plus requestLayout()
are a good solution if you know the "x", and if you need to do it ONLY ONCE.
After that the wrap_content
in LayoutParams
is lost, since you have overridden it.
To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with wrap_content
. The code and instructions to use are available here:
https://github.com/ea167/android-layout-wrap-content-updater
Enjoy!
give an id to your relative layout in .xml :
android:id="@+id/relativelayout"
now in your java file write this:
RelativeLayout Rl = (RelativeLayout) findViewById(R.id.relativelayout);
RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedwidth,
RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedHeight);
Rl.setLayoutParams(layout_description);
From below code u can get device height and width:-
Display display = getWindowManager().getDefaultDisplay();
int width = (display.getWidth() );
int height = (display.getHeight() );
paramsTop ur realative layout:-
Now u can set height or width what you want.
paramsTop = new RelativeLayout.LayoutParams(width, height);