I have two controls: TextView and LinearLayout . Both have a width specified in the wrap_content interface wrap_content . Then in the code ( OnPostCreate ) I want to change the width of the LinearLayout and make it the same as the TextView . However, I do not succeed, because the width of the TextView displayed as -2.

How can I do?

  • And maybe the size distribution after your code goes? Try other methods like onStart. - IronVbif
  • Unfortunately, also not suitable. - rchugunov

3 answers 3

In this situation, it was necessary to override the View.onSizeChanged (...) method, but I did it easier (from my point of view) - I created a stream that listened to the sizes of the necessary elements and issued a message when they changed

    Try methods: textView.getMeasuredWidth (); textView.getMeasuredHeight ();

    Or set the size of the widget in dp.

      Why didn't you use addOnLayoutChangeListener ? it was enough to add this listener to TextView and then simply change the size of the layer when working on this event.

      I think for the purpose described, to create a separate stream is not the most elegant option ...