Question

I was surprised to learn that the Android API allows us to resizes imageView objects by calling the getLayoutParams() method. For example:

myTimerBar.getLayoutParams().width = timeRemaining;

There is no corresponding setLayoutParams() method.

Is it common for methods that have the word "get" to also allow programmers to "set" properties? I realize that this method is not technically a "getter" method, but it does start with the word "get."

Was it helpful?

Solution

getLayoutParams() returns ViewGroup.LayoutParams which it has an instance variable width that is declared as public, and in Java you can access a public instance variable and set a new value to it without a setter method.

For example:

public class Point
{
    public int x;
    public int y;
}
Point p = new Point();
p.x = 10;
p.y = p.x + 1;

OTHER TIPS

Is it common for methods that have the word "get" to also allow programmers to "set" properties? I realize that this method is not technically a "getter" method, but it does start with the word "get.

It does return the LayoutParams object associated with the View. This object does not have a mutable state. But View class does have setLayoutParams(), so does any sub-class of View. You will have to create a new LayoutParams to set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top