In my program, I extensively use weight components, adjusting the text of the buttons using a small function using paint.getTextBounds . My function is looking for the size of the text that fits into the borders of the button. In this function, I pass the container and the text, where the measurements take place like this:

  paint.setTypeface(typeface); paint.getTextBounds(text, 0, text.length(), textRect); int cWidth = container.getMinimumWidth(); int cHeight = container.getMinimumHeight(); int tWidth = textRect.width(); int tHeight = textRect.height(); if ((tWidth>cWidth)||(tHeight>cHeight)){ //ั‚ะตะบัั‚ ะฝะต ะฒะปะตะท } else { //ั‚ะตะบัั‚ ะฒะปะตะท } 

On the emulator with a small screen size, I got a few buttons

 mMinHeight = 168 mMeasuredHeight = 102 

accordingly, the text size is chosen too large for the component. But at the time of the creation of the button mMesauredHeight does not exist yet, more precisely equal to 0. Therefore, I used getMinimumHeight . The approach through MinHeight obviously does not work for small screens. I look towards onMeasure and onLayout.

Actually, how else can we find out the size of the components at the time of their creation? Maybe there is another simple way to adjust the text to the size of the view?

    1 answer 1

    Yes, look right. You can also add the onSizeChanged() method to the same category. Just keep in mind that, depending on the complexity of the layout, these methods can be called several times, and the final dimensions will be already on the last call.