Android Question Add "$" Symbol to Edittext

German Buchmuller

Member
Licensed User
Longtime User
Hi, I want to add the Currency symbol to the begining of the editext, so that when the user writes a number, automatically the symbol "$" will be added to the begining of the editext. Thanks to all
 

LucaMs

Expert
Licensed User
Longtime User
B4X:
Dim CurrencySymbol As String
CurrencySymbol = "$"


Sub EditText1_TextChanged (Old As String, New As String)
    If New.Length > 0 Then
        If Not(New.StartsWith(CurrencySymbol)) Then
            EditText1.Text = CurrencySymbol & EditText1.Text
            EditText1.SelectionStart = EditText1.Text.Length
        End If
    End If
End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim CurrencySymbol As String
CurrencySymbol = "$"
=
B4X:
Dim CurrencySymbol As String = "$"

Modifying the text while the user changes it can result in a bad UX. For example if the user adds a character before the $ sign then it will add a new $ sign. I think that you will get better result if you add it when the focus is removed.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Dim CurrencySymbol As String = "$"
:eek: Is it possible?

Just to emphasize the fact that it is a variable and that will be set based on localization.
I prefer to use your "kind of statement" for constants.

;)


Modifying the text while the user changes it can result in a bad UX. For example if the user adds a character before the $ sign then it will add a new $ sign. I think that you will get better result if you add it when the focus is removed.
I agree (of course :D)
 
Upvote 0

German Buchmuller

Member
Licensed User
Longtime User
Thanks to both!! It worked. Now, how can I do so that I can do math calculation with this values? As it has the currency symbol before, It shows no result after the "calculate" button is pressed. Its a bit difficult to understand, guess you could. Thanks again!
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You could add an image to the edittext that will always be there and not interfere with the editing. You could of course get the currency symbol based on the locale as well if needed)

B4X:
Sub EditTextSetup(EditTxt As EditText)
    
    'Create a bitmap to draw on
    Dim BMP As Bitmap
    BMP.InitializeMutable(40dip,40dip)
    
    'Create a Canvas and assign to the bitmap
    Dim C As Canvas
    C.Initialize2(BMP)
    C.DrawText("$",20,40,Typeface.DEFAULT,20,Colors.White,"CENTER")
    
    'Convert the bitmap to a Drawable
    Dim BMD As BitmapDrawable
    BMD.Initialize(C.Bitmap)
    
    'Add the image to the edit text
    Dim ETxt As JavaObject = EditTxt
    ETxt.RunMethod("setCompoundDrawablesWithIntrinsicBounds",Array As Object(BMD,Null,Null,Null))
End Sub
 
Upvote 0
Top