Android Question EditText.TextColor not work

Situ LLC

Active Member
Licensed User
HI.

I use the TextColor method of EditText in Design mode and it works. But when I build it in Runtime mode it doesn't work.

B4X:
    mEdit.Initialize("Edit")
    mEdit.TextColor  = Colors.Black    '<--- not work display Colors.White'
    mEdit.Hint       = "Enter Data"
    mEdit.HintColor  = Colors.Gray
    mEdit.TextSize   = 14
    mEdit.SingleLine = True
    mBase.AddView(mEdit, x, y, w, h)
 

Mahares

Expert
Licensed User
Longtime User
try it this way:
I build it in Runtime mode it doesn't work.

B4X:
mEdit.Initialize("Edit")   
    mBase.AddView(mEdit, x, y, w, h)
    mEdit.TextColor  = Colors.Black    '<--- not work display Colors.White'
    mEdit.Hint       = "Enter Data"
    mEdit.HintColor  = Colors.Gray
    mEdit.TextSize   = 14
    mEdit.SingleLine = True
 
Upvote 0

Situ LLC

Active Member
Licensed User
Hi Mahares. Making that change doesn't work.

I try from this sub and it doesn't work either

B4X:
Public Sub setTextColor(val As Int)
    mEdit.TextColor = val
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Making that change doesn't work.
I just tried it on my device and the text is black as it is supposed to be: See also attached project
B4X:
Sub Globals
    Private mEdit As EditText
    Private mbase As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    mEdit.Initialize("Edit")
    Activity.LoadLayout("main")  'has panel mBase
    Dim x As Float=10dip, y As Float=50dip, w As Float=100dip, h As Float=75dip
    mbase.AddView(mEdit, x, y, w, h)
    mEdit.TextColor  = Colors.Black    '<--- not work display Colors.White'
    mEdit.Hint       = "Enter Data"
    mEdit.HintColor  = Colors.Gray
    mEdit.TextSize   = 14
    mEdit.SingleLine = True
End Sub
 

Attachments

  • busmatic.zip
    8.9 KB · Views: 162
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
A lot depends on the color of mBase, and the values of x, y, w, h. White on White is invisible (as is any color the same as the background). If w or h are too small the thing won't show. If x or y exceeds the width height of mBase it won't show. Where you add mEdit to mBase doesn't matter. Also make sure mBase has been added to Parent/Activity.

B4X:
Sub test
    Private mbase As Panel
    mbase.initialize("")
    mbase.Color = Colors.White
    Activity.AddView(mbase, 0, 0, 100%x, 100%y)

    Dim mEdit As EditText
    mEdit.Initialize("Edit")
    mEdit.TextColor  = Colors.Black
    mEdit.Hint       = "Enter Data"
    mEdit.HintColor  = Colors.Gray
    mEdit.TextSize   = 20
    mEdit.SingleLine = True
    mbase.AddView(mEdit, 0, 0, 100dip, 50dip)
End Sub
 
Upvote 0

Situ LLC

Active Member
Licensed User
Mahares.

Thanks for your help. your code is correct and it works in an activity.
Now the problem is when I use the EditText from a class module, and TextColor doesn't work that way.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Where do you define/load mEdit? If it is in the Main activity you need to pass it as an object to instance of the class.
In the class you then need to cast the object to B4Xview in order to set it's color.

If you define mEdit in the class, then it works like @Mahares says.
 
Upvote 0

Situ LLC

Active Member
Licensed User
This my code class module :

module class uiEditText:
Sub Class_Globals
    
    Private mEventName As String 'ignore
    Private mCallBack As Object 'ignore
    
    Private mBase As Panel
    Private mEdit As EditText
    
End Sub

Public Sub Initialize (callbackModule As Object, EventName As String)
    
    mCallBack = callbackModule
    mEventName = EventName
    
End Sub

Public Sub CreateUI(Base As Panel, x As Int, y As Int, w As Int, h As Int)
    
    Log($"Base ${Base.Left} ${Base.Top} ${Base.Width} ${Base.Height}"$)
    addToParent(Base, x, y, w, h)
    
End Sub

Public Sub addToParent(parent As Panel, x As Int, y As Int, w As Int, h As Int)
    
    mBase = parent
    mBase.Tag = Me
    mBase.Color = Colors.Transparent
    
    CreateView(x, y, w, h)
    
End Sub

Private Sub CreateView(x As Int, y As Int, w As Int, h As Int)
    
    ' EditText
    mEdit.Initialize("Edit")
    #If B4A
        mEdit.Padding = Array As Int(8dip, 8dip, 8dip, 8dip)
    #End If
    
    mEdit.Text         = ""
    mEdit.Color       = Colors.rgb(0, 255, 255)
    mEdit.TextColor  = Colors.Black
    mEdit.Hint       = "Enter Data"
    mEdit.HintColor  = Colors.Blue
    mEdit.TextSize   = 14
    mEdit.SingleLine = True
    mBase.AddView(mEdit, x, y, w, h)
    
    Log($"mEdit ${mEdit.Left} ${mEdit.Top} ${mEdit.Width} ${mEdit.Height}"$)
    
End Sub

Public Sub setTextColor(val As Int)
    mEdit.TextColor = val
End Sub

This my code main activity :

main activity:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    
    Private base As Panel
    Private UI As uiEditText

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    
    base.Initialize("")
    base.Color = Colors.White
    
    Activity.AddView(base, 10dip, 20dip, 200dip, 40dip)
    
    UI.Initialize(Me, "UI")
    UI.CreateUI(base, 0dip, 0dip, 200dip, 40dip)
    UI.TextColor = Colors.Black
    
End Sub
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
In your Main, the 'EditText' nature of UI is unknown, so you'll have to use:

B4X:
UI.SetTextColor(Colors.White)

The rest of your code works as you intended. Note I use White because Black sets the color that was already Black.
Of course you have to enter some text to see the new color. I tested this and it works fine.

Although @Erel's recommendation to use the designer is in general the best choice, I too create Views programmatically when needed.
 
Upvote 0

Situ LLC

Active Member
Licensed User
Hi William, using UI.SetTextColor ( <color> ) works fine.
Erel here I leave the small project

Thank you
 

Attachments

  • edit_test.zip
    9.2 KB · Views: 149
Upvote 0

Situ LLC

Active Member
Licensed User
Erel, thanks for your suggestion. I will do it with B4XView
 
Upvote 0
Top