EditText View Does Not Return to its Original Size After It Loses Focus

Mahares

Expert
Licensed User
Longtime User
I have the following code where I want the text box to return to its original size before the Hasfocus =true, but it stays wider and taller, hence consumes all the spacing between the other edittext views and they all become contiguous.

B4X:
Sub MyEdit_FocusChanged(Hasfocus As Boolean)
         Dim Send As EditText
         Send=Sender
         If Hasfocus=True Then
               Send.Color=Colors.Cyan
            Send.Typeface=Typeface.DEFAULT_BOLD
         Else
            Send.Color=Colors.white
            Send.Typeface=Typeface.DEFAULT
             End if
End sub
 

Mahares

Expert
Licensed User
Longtime User
All the edittext views were created on the designer. There is no code for changing their height or their width. It just happens that as soon as an edittext gets focus, its width and height expand slightly without code for the height or width, and when it loses focus it does not return to the size before the focus. Therefore, for 2 text boxes that are closely spaced, they become one with no separation between them.
I hope this explanation helps.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The Typeface code lines do not affect it one way or the other. The culprit is the color code lines. If I disable the color code lines (remark them), the behavior disappears and it is ok, but I like to use the color to enhance the edittext view when it has focus so it is more visible to the user. Is there a solution to this?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Changing the color of EditText (or any other view) replaces the default Drawable object with a ColorDrawable.
You can store the default drawable in the Tag property:
B4X:
Sub Globals
   Dim et1, et2 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   et1.Initialize("et")
   et1.Tag = et1.Background
   Activity.AddView(et1, 10dip, 10dip, 200dip, 50dip)
   et2.Initialize("et")
   et2.Tag = et2.Background
   Activity.AddView(et2, 10dip, 110dip, 200dip, 50dip)
End Sub
Sub et_FocusChanged (HasFocus As Boolean)
    Dim Send As EditText
    Send=Sender
    If HasFocus=True Then
      Send.Color=Colors.Cyan
        Send.Typeface=Typeface.DEFAULT_BOLD
    Else
        Send.Background = Send.Tag
        Send.Typeface=Typeface.DEFAULT
    End If
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
That is very clever Erel. Outstanding. Caveat: What happens if I already have an object inside the tag. How would you handle multiple objects in a tag? Can someone tackle this question?

B4X:
Dim MyTag as object
MyTag =et2.Tag
MyTag = MyTag   & "," & et2.Background
MyTag=MyTag.SubString(MyTag.IndexOf(",")+1)  'to extract the background of edittext
Your tip will be very valuable.
Thanks
 
Last edited:
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Cool little trick. It seems, at least while it is focused, that the editbox still draws "ugly" as just a solid color without rounded edges, etc. You might try a canvas on it and set the DrawColor of the canvas with Colors.ARGB making a semi-transparent Cyan, Red, or Yellow then invalidate the view to make it look like a highlight over the field. Or, you could also use it to draw an outline rectangle around the edit box. Either way still keeps the look of the editbox without it becoming a solid box.

It would be really cool to somehow get the inner area/clip of the box and just color it too.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Since the tag may hold 2 objects, I tried to use TYPE as Erel suggested. When the Edittext view loses focus, it does not return to its original size. This exercise is giving me fits. Please see my code below:

B4X:
Type MyTagType (Meter As String, BG As ColorDrawable)   'in Globals
Dim MyTag As MyTagType     'in Globals
      
MyTag.Initialize      'in Sub Activity_Create(FirstTime As Boolean)
MyTag.BG.Initialize(Colors.RGB(255,255,255),0dip)  'in Sub Activity_Create(FirstTime As Boolean)


Sub MyEdit_FocusChanged(Hasfocus As Boolean)      
         Dim Send As EditText
         Send=Sender
         If Hasfocus=True Then         
            Send.Color=Colors.Cyan         
            Send.Typeface=Typeface.DEFAULT_BOLD
         Else
            Send.Background=MyTag.BG
            Send.Typeface=Typeface.DEFAULT
            End if
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Process_Globals
End Sub

Sub Globals
   Type MyTagType (Meter As String, BG As Object)
   Dim e1, e2 As EditText
End Sub


Sub Activity_Create(FirstTime As Boolean)
   e1.Initialize("e")
   e2.Initialize("e")
   Dim m1, m2 As MyTagType
   m1.BG = e1.Background
   m2.BG = e2.Background
   e1.Tag = m1
   e2.Tag = m2
   Activity.AddView(e1, 0, 0, 100dip, 50dip)
   Activity.AddView(e2, 0, 100dip, 100dip, 50dip)
End Sub
Sub e_FocusChanged (HasFocus As Boolean)
    Dim Send As EditText
    Send=Sender
    If HasFocus=True Then            
        Send.Color=Colors.Cyan            
        Send.Typeface=Typeface.DEFAULT_BOLD
    Else
      Dim m As MyTagType
      m = Send.Tag
        Send.Background = m.BG
        Send.Typeface=Typeface.DEFAULT
    End If
End Sub
 
Upvote 0

John Woodsmall

Active Member
Licensed User
Longtime User
when I 1st go to an editText it turns to the color. When I leave it it turns black.
How do I keep the same background color and any text that is in the editText.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Change the manifest. In the IDE, click Project, then manifest Editor and change the 14 to 10 like I have it here:
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="10"/>

Also, see my comment on the Send.background below
B4X:
Sub et_FocusChanged (HasFocus As Boolean)
    Dim Send As EditText
    Send=Sender
    If HasFocus=True Then
      Send.Color=Colors.Cyan
        Send.Typeface=Typeface.DEFAULT_BOLD
    Else
        Send.Background = Send.Tag    'comment this line if you want it to stay cyan
        Send.Typeface=Typeface.DEFAULT
    End If
End Sub
 
Upvote 0
Top