0

Hi all,

I'm banging my head with a problem that seems intractable to me (perhaps my ignorance x).

I have a TextView in which a string when I write, I calculate the space available and if necessary I reduce the font size to a minimum size set, then add a line and keep writing.

PROBLEM:

Writing strings of buttons 2 and 3, display the lines of the TextView are filled in correctly: the first line is complete and the rest of the string goes on the second line. ES.:

56688+(555556565555566
555555

56688x(555556565555566
555555

Writing strings of buttons 1 and 4, show the lines of the TextView are filled incorrectly: the first line is filled up to the '/' or '-' remained largely empty, and the text after the character is on the second line. ES.:

                56688/
(555556565555566555555

                56688-
(555556565555566555555

The intended behavior is that of the strings of the buttons 2 and 3.

I can not understand why it behaves abnormally in the presence of the characters '/' and '-', and how to remedy this anomaly.

I enclose code:

In the file "activity_main.xml" I have a custom component that I report below:

<LinearLayout android:id="@+id/mainLayout" 
android:layout_width="fill_parent"         
android:layout_height="wrap_content" 
androidrientation="vertical"  
android:layout_alignParentTop="true">

<com.example.component.NumTextView
 android:id="@+id/textViewInput"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:focusable="true"
 android:gravity="right"
 android:maxHeight="64dp"
 android:minHeight="64dp"
 app:lines="1"
 app:maxLines="3"
 app:mintextsize="16"
 app:maxtextsize="48"
 androidadding="0dp"
 android:scrollbars="vertical"
 android:singleLine="false" android:text="" />

<View android:layout_width="fill_parent" 
 android:layout_height="1dp"  
 android:background="#000000" />

In the class "NumTextView" that inherits from "TextView" I have the following code:

private void refitText(String text, int textWidth)
{
 float trySize = 23;
if( textWidth > 0 )
{
int lineCount = this.getLineCount();
this.setLines(lineCount + 1);
this.setVerticalScrollBarEnabled(true);
this.setMovementMethod(ScrollingMovementMethod.get Instance());
this.setTextSize(trySize);
}
}


@Override
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after)
{
refitText( text.toString(), this.getWidth() );
}

Finally, in "MainActivity" I have the following code:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textInput = (NumTextView) findViewById(R.id.textViewInput);
}

public void onClick1(View v)
{
String stringa ="56688-(555556565555566555555";
textInput.setText(stringa);
}

public void onClick2(View v)
{
String stringa ="56688+(555556565555566555555";
textInput.setText(stringa);
}

public void onClick3(View v)
{
String stringa ="56688x(555556565555566555555";
textInput.setText(stringa);
}

public void onClick4(View v)
{
String stringa ="56688/(555556565555566555555";
textInput.setText(stringa);
}
jnthnjns
  • 8,962
  • 4
  • 42
  • 65
Puck
  • 21
  • Ti dispiace se si tenta di tradurre in inglese? – jnthnjns Nov 05 '12 at 21:31
  • 1
    Ho usato Google Translate per ottenere il vostro messaggio in inglese. Le risposte che si ottiene sarà in lingua inglese. Se hai problemi, vai a http://translate.google.com/ - sto usando Google Translate per scrivere questo, vi prego di scusare il mio italiano. – jnthnjns Nov 05 '12 at 21:55

1 Answers1

0

Why: The slash(/) and hyphen(-) are breaking characters, so it will naturally seek to break the lines at those points. That's how word wrap works, by looking for breaking chars and inserting new line characters at the last break spot that will fit.

Fix? If you can measure the lines to see how many characters will fit on each line, you may be able to insert a zero-width space(U+200B) to force a line break at that spot. Not all fonts support this character, however, so this may not work on all devices.

Various answers to this question may help you if you're unable to use a zw-space, or if you're interested in auto-sizing in general.

Community
  • 1
  • 1
Geobits
  • 22,218
  • 6
  • 59
  • 103