Android Question Problem with NumberFormat2(

Mitch

Member
Licensed User
Longtime User
Hi All,

Just learning, and I need a little help.

The code below crashes on a Nexus 7 when the first character in the field is entered, with this error:

"Unfortunately, FLLCalc has stopped."

Sub txtbase_TextChanged (Old As String, New As String)
'txtBase.text=currFormat(Old,New)
Dim numb As Double
numb=New
txtBase.Text= NumberFormat2(numb,0,2,2,True)
End Sub

I've tried several variations of this, with no luck. It works fine without NumberFormat2(, without formatting, of course.

Thanks in advance.

Mitch
 

Mitch

Member
Licensed User
Longtime User
Thank you. Here's the log:

Installing file.
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_txtbase_textchanged (B4A line: 93)
txtBase.Text= NumberFormat2(numb,0,2,2,True)
Message longer than Log limit (4000). Message was truncated.
java.lang.StackOverflowError
at com.android.internal.util.ArrayUtils.emptyArray(ArrayUtils.java:110)
at android.text.SpannableStringBuilder.getSpans(SpannableStringBuilder.java:847)
at android.text.SpanSet.init(SpanSet.java:46)
at android.text.TextLine.set(TextLine.java:148)
at android.text.Layout.getLineExtent(Layout.java:997)
at android.text.Layout.getLineStartPos(Layout.java:504)
at android.text.Layout.getHorizontal(Layout.java:861)
at android.text.Layout.getHorizontal(Layout.java:830)
at android.text.Layout.getPrimaryHorizontal(Layout.java:805)
at android.text.Layout.getPrimaryHorizontal(Layout.java:795)
at android.widget.TextView.getFocusedRect(TextView.java:5121)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:120)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:94)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:65)
at android.view.ViewGroup.focusSearch(ViewGroup.java:668)
at android.view.ViewGroup.focusSearch(ViewGroup.java:670)
at android.view.ViewGroup.focusSearch(ViewGroup.java:670)
at android.view.ViewGroup.focusSearch(ViewGroup.java:670)
at android.view.ViewGroup.focusSearch(ViewGroup.java:670)
at android.view.ViewGroup.focusSearch(ViewGroup.java:670)
at android.view.ViewGroup.focusSearch(ViewGroup.java:670)
at android.view.ViewGroup.focusSearch(ViewGroup.java:670)
at android.view.View.focusSearch(View.java:6392)
at android.widget.TextView.onCreateInputConnection(TextView.java:5627)
at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1120)
at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:1070)
at android.widget.TextView.setText(TextView.java:3684)
at android.widget.TextView.setText(TextView.java:3622)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3597)
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
at b4a.example.main._txtbase_textchanged(main.java:457)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.objects.EditTextWrapper$1.afterTextChanged(EditTextWrapper.java:82)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7320)
at android.widget.TextView.setText(TextView.java:3771)
at android.widget.TextView.setText(TextView.java:3622)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3597)
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
at b4a.example.main._txtbase_textchanged(main.java:457)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.objects.EditTextWrapper$1.afterTextChanged(EditTextWrapper.java:82)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7320)
at android.widget.TextView.setText(TextView.java:3771)
at android.widget.TextView.setText(TextView.java:3622)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3597)
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
at b4a.example.main._txtbase_textchanged(main.java:457)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.objects.EditTextWrapper$1.afterTextChanged(EditTextWrapper.java:82)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7320)
at android.widget.TextView.setText(TextView.java:3771)
at android.widget.TextView.setText(TextView.java:3622)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextVie
Message longer than Log limit (4000). Message was truncated.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Will this work for you:
B4X:
Sub txtBase_EnterPressed
  If IsNumber(txtBase.Text) Then
    txtBase.Text= NumberFormat2(txtBase.Text,0,2,2,True)
  Else
    Msgbox("You need to enter a number.","")
  End If
End Sub
after you enter 34987.876 and press the enter key, your text box will show: 34,987.88
 
Upvote 0

Mitch

Member
Licensed User
Longtime User
Thanks again for your replies. I would like to stop input after two digits to the right of the decimal, but from what you say, that may not be possible since the value needs to be updated in TextChanged.

I was able to get this to work:

B4X:
Sub txtrebate_TextChanged (Old As String, New As String)
Dim xwhere,xlen As Int

xlen=txtRebate.Text.Length
If xlen<>0 AND txtRebate.Text<>"." Then
    rebate = txtRebate.Text
Else
    rebate=0
End If

xwhere=txtRebate.text.indexOf(".")+1
If xwhere<>0 AND xlen-xwhere>2 Then
txtRebate.Text=Old
txtRebate.SelectionStart=txtRebate.Text.Length 'put cursor at the end of the line
End If

End Sub

But if it's not good practice to modify the value in TextChanged, I will use the solution kindly posted by Mahares. Many fields will require this code so I tried to make it work as a function, with no luck.

Other ideas will be appreciated. Thanks again.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Usually Informatix solutions are dead on, but if you still want to use my quasi solution and apply it to several edit text views, then you can give them all the same event name and use the sender keyword. See sample below:
B4X:
Sub txtBoxes_EnterPressed    'event name txtBoxes
Dim et As EditText
et=Sender
If IsNumber(et.Text) Then
   et.Text= NumberFormat2(et.Text,0,2,2,True)
Else
  Msgbox("You need to enter a number.","")
End If
End Sub
 
Upvote 0

Mitch

Member
Licensed User
Longtime User
When using Masked EditText, is it possible to set the input field to look similar to the standard for later versions of Android (underlined, black background, etc)? I set a mask, but the mask was displayed within the field. Is there a way to make it invisible?

I see how to use one sub using Sender. Thanks for that. The event does not appear to execute when leaving an EditText entry by selecting the next field, so I don't see how to get it to execute, since there is no enter key.

Thanks again.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
When using Masked EditText, is it possible to set the input field to look similar to the standard for later versions of Android (underlined, black background, etc)? I set a mask, but the mask was displayed within the field. Is there a way to make it invisible?

I see how to use one sub using Sender. Thanks for that. The event does not appear to execute when leaving an EditText entry by selecting the next field, so I don't see how to get it to execute, since there is no enter key.

Thanks again.

The Masked EditText is based on a standard EditText so it looks like a standard EditText. The contrary would be surprising. ;) And if you set a theme, it will reflect the theme.
If you want that your mask is not visible, use a space as the placeholder character (see Placeholder).
I don't understand the question about events. There's an event to filter the input, another when Enter is pressed, another when the focus is changed....
 
Upvote 0

Mitch

Member
Licensed User
Longtime User
I see. I probably need to use the focus changed event, not EnterPressed. I'll spend more time with Masked EditText to figure out how to set themes.

Thanks again.
 
Upvote 0
Top