Android Layout Params change ONLY width and height
The whole purpose of using method getLayoutParams()
is to take the exisiting ones from a View
(that keep all the rules and stuff). Then you can modify specific parameters of those LayoutParams
and keep the rest unmodified. Therefore you need to call getLayoutParams()
and modify the returned object if your Views
vary in settings in xml. In case they were using the exact same rules in xml, you could do it just like you wrote in your last example.
What I would advice you to do is just to make a method that would wrap the whole process of updating LayoutParams
. Like so:
private void setDimensions(View view, int width, int height){
android.view.ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
That would simplify your code significantly, because then you can just call this method for every single of your buttons, with proper values for width and height.
setDimensions(button, height/7, height/10);
setDimensions(button2, height/7, height/10);
setDimensions(button3, height/7, height/10);
In Kotlin we can do it shorter and more elegant:
view.layoutParams = view.layoutParams.apply {
width = LayoutParams.MATCH_PARENT
height = LayoutParams.WRAP_CONTENT
}
If you just want to change one parameter:
view.getLayoutParams().width = 400;
view.requestLayout();