0

I'm 17 and I'm italian. This is my first app.

I've foolishly thought this code for maintain 1 line on my switch independently from the screen:

Inside at OnCreate()
   
while(SWITCH.getLineCount()>1)
                SWITCH.setTextSize(SWITCH.getTextSize()-1);

obviously does not work.. why?

I've read a lot of ask but I have not yet figured out how to set the font size dynamically based on the size of the screen.

Thank you very much.

EDIT: for distraction I had written

(SWITCH.getLineCount()-1)

instead of

(SWITCH.getTextSize()-1),

but still does not work.

Community
  • 1
  • 1
Leonardo Rignanese
  • 865
  • 11
  • 22
  • possible duplicate of [How to change the size of the font of a JLabel to take the maximum size](http://stackoverflow.com/questions/2715118/how-to-change-the-size-of-the-font-of-a-jlabel-to-take-the-maximum-size) – jbutler483 Sep 03 '14 at 11:03
  • You sure you want `getLineCount()` in `setTextSize()`? Wouldn't it be `getTextSize()`? – Mike M. Sep 03 '14 at 11:13
  • @mikeM. I've used getTextSize() to get the size and reduce it by one for loop. Do you understand me? – Leonardo Rignanese Sep 03 '14 at 14:57
  • I understand what you're trying to do, but your logic is flawed. You want to decrement the size until the text fits on one line. `getLineCount()` does not return the text size, so subtracting 1 from that and setting it as the new size makes no sense. The line in the loop should be `SWITCH.setTextSize(SWITCH.getTextSize()-1);` – Mike M. Sep 03 '14 at 15:05
  • @mikeM. absolutely right! I'm a wimp and I was convinced that he had written getTextSize() , thanks boy. I'll try.. – Leonardo Rignanese Sep 03 '14 at 15:10
  • I should mention, though, that I'm not sure this way will work. You might give cyanide's answer a shot, too. – Mike M. Sep 03 '14 at 15:16
  • @MikeM. SWITCH.setTextSize(SWITCH.getTextSize()-1) not work.. I want try the cyanide's code but i don't understand some things. – Leonardo Rignanese Sep 03 '14 at 15:23

3 Answers3

0

Assuming your TextView has android:maxWidth value.

The following function:

  • Gets Maximum width

  • Uses Paint.measureWidth to check the actual width

  • If the actual width is more than maximum, the font size is reduced proportionally.

  • The loop continues until the width becomes less than maximum.

  • For safety reason I introduced iterCount to ensure than the loop will eventually end.

I didn't test the code, but the idea should be clear:

private void adjustTextSize(TextView tv,  final String text) {
    float maxWidth = tv.getMaxWidth() * 0.9f;

    Paint p = tv.getPaint();
    float fontSize = p.getTextSize();
    float txtWidth;

    int iterCount = 10;

    while (--iterCount >= 0 &&  (txtWidth = p.measureText(text)) > maxWidth)  
        p.setTextSize(fontSize *= (maxWidth / txtWidth));

}
cyanide
  • 3,885
  • 3
  • 25
  • 33
0

You can omit maxWidth and use layout_width="match_parent". The trouble is that in this case you don't know the size until layout is complete. See How to know when an activity finishes a layout pass? about notification when layout is complete.

From onGlobalLayout() you can use tv.getWidth()

 final TextView tv= (TextView) findViewById(R.id.myView);

 tv.getViewTreeObserver().addOnGlobalLayoutListener(
       new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                float maxWidth = tv.getWidth() * 0.9f;

                String text = getResources().getString(R.string.myTextId); 
                float fontSize = tv.getTextSize();
                float txtWidth;

                int iterCount = 10;

                 while (--iterCount >= 0 &&  (txtWidth = tv.getPaint().measureText(text)) > maxWidth)  
                     tv.setTextSize(fontSize *= (maxWidth / txtWidth));

                 tv.setText(text);
             }
         });

I slightly changed the code to assign textSize to the TextView, rather than to the underlying Paint. It's more correct this way.

Community
  • 1
  • 1
cyanide
  • 3,885
  • 3
  • 25
  • 33
0

Take a look at this library here later, it does what you want to do

Just add the compile to your gradle and replace the <TextView in xml with <me.grantland.widget.AutofitTextView, done, the magic is done, rs

Daniel Santos
  • 61
  • 1
  • 3