Onmeasure gets called and I don't know why - android

I have an activity with multiple views in a linear layout, one is textual and one is the main view of my game, which is a regular view that I am doing with code.

When I added the following piece of code to my activity:

mTextView.setText("random text");


The onMeasure () function is called on the custom view, i.e. setText on a text view appears to cause another view to re-measure. Since my onmeasure () code is resetting the view in an unwanted way, I want to avoid that.

What is causing the call to onMeasure () in this case and how can I stop it?


source to share

1 answer

Answer your question: In a linear layout, the size and position of each element depends on the size and position of the elements that are in front of it. If you use layout dimensions to size elements instead of using fixed sizes, what's more, the size and position of the element also depends on the elements that appear after it.

Changing the content of one element (in your case when called setText()

) can resize that element. As a result, Android re-layout to define new sizes and positions for the LinearLayout children, and this requires the dimensions of all the elements in the Linear Layout.

Answer your problem: It would be reasonable to assume that onMeasure()

can be called at any time. A good solution would be to change onMeasure()

so that it has no side effects. Instead, wait onSizeChanged()

before you "reset the view."



All Articles