Android Params parameter ONLY changes width and height

I know how to set the width and height of a view using LayoutParams by doing the following:

 android.view.ViewGroup.LayoutParams params = button.getLayoutParams(); params.width = height/7; params.height = height/10; button.setLayoutParams(params); 

Now, as soon as I want to apply the same height and width to another button, I need to create another LayoutParams or overwrite the existing one, like here:

 android.view.ViewGroup.LayoutParams params2 = but2.getLayoutParams(); params2.width = height/7; params2.height = height/10; but2.setLayoutParams(params2); 

I have about 50 buttons in my application, and I doubt that this is good code to get all Params, just to change the width and height - I want to save the remaining parameters (toLeftOf, [...]).

Is there a way ONLY to change the width and height of the parameters, but save the rest of the parameters? Therefore, it looks something like this:

 android.view.ViewGroup.LayoutParams params = button.getLayoutParams(); params.width = height/7; params.height = height/10; button.setLayoutParams(params); button2.setLayoutParams(params); button3.setLayoutParams(params); butt[...] 

Thank you very much in advance.

+8
android view params
source share
4 answers

The whole purpose of using the getLayoutParams() method is to take ex exiting from the View (which saves all the rules and stuff). Then you can change certain parameters of these LayoutParams and save the rest unmodified. Therefore, you need to call getLayoutParams() and change the returned object if your Views changes in the settings in xml. If they use the same rules in xml, you can do it the same way as in your last example.

What would I advise you to do, just make a method that would wrap the whole process of updating LayoutParams . For example:

 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); } 

This will greatly simplify your code, because then you can simply call this method for each of your buttons with the correct values ​​for width and height.

 setDimensions(button, height/7, height/10); setDimensions(button2, height/7, height/10); setDimensions(button3, height/7, height/10); 
+25
source share

At Kotlin we can make it shorter and more elegant:

 view.layoutParams = view.layoutParams.apply { width = LayoutParams.MATCH_PARENT height = LayoutParams.WRAP_CONTENT } 
+1
source share

As the other answers said: you need getLayoutParams() , change the returned LayoutParams object and setLayoutParams() with it (will this be a reasonable API for discussion).

But, as comm1x says, Kotlin can make it very simple and easy to read.

How about this:

 button.alterLayoutParams { it.width = height / 7 it.height = height / 10 } 

This requires an extension function:

 private fun View.alterLayoutParams(callback: ((ViewGroup.LayoutParams) -> Unit)) { val p = this.layoutParams callback(p) this.layoutParams = p } 

If you want to apply the same layout options to multiple View s, you can do something like:

 listOf(button, button2, button3).forEach { b -> b.alterLayoutParams { it.width = height / 7 it.height = height / 10 } } 

PS. Kotlin is nothing short of fiction.

+1
source share

If you want to set different sizes for different components, I suggest you do it inside XML instead of doing it in JAVA. You will get much better results, and it will be optimal.

-one
source share

All Articles