How to convert EditText to TextView?

Pedro Naranjo

Member
Licensed User
Longtime User
I am making a sub that changes all EditText TextColor property in a Activity.

But its crash in this line:
Dim textBox As EditText = vw

Error:
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

What am I doing wrong?

Here is the code:

B4X:
Sub SetEditText_Textcolor(ActivityName As Activity, ForegroundColor As Int)
   Dim i As Int
   Dim vw As View
      
   For i = 0 To ActivityName.NumberOfViews - 1
         
      vw = ActivityName.GetView(i)
      
      If GetType(vw).IndexOf("TextView") > 0 Then
        'If vw Is EditText Then ' DO NOT WORK
          Dim textBox As EditText = vw '// This Line CRASH
         
                textBox.TextColor = ForegroundColor
      End If
               
   Next
      
End Sub
 

Mahares

Expert
Licensed User
Longtime User
How about something like this Pedro:
In Activity_Create, you have this call to the below sub:
B4X:
ETTextColors(Colors.Red)

Below is the sub to set your text colors:
B4X:
Sub ETTextColors(ForegroundColor As Int)
   For i=0 To Activity.NumberOfViews-1
      If Activity.GetView(i) Is EditText   Then
         Dim MyViewET As EditText
         MyViewET=Activity.GetView(i) 
         MyViewET.TextColor=ForegroundColor
     End If
   Next    
End Sub
 
Upvote 0

Pedro Naranjo

Member
Licensed User
Longtime User
Thanks Mahares, but... it does not work.

I don't understand why in debugging mode, code never enter in IF condition:

If Activity.GetView(i) Is EditText Then

Looging value of Activity.GetView(i) object this is printed:
GetType(Activity.GetView(i) ): (TextView): Left=10, Top=332, Width=300, Height=30, Tag=
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I tested it before I posted and it worked well for me. I loaded a layout called: Settings that had several edittext boxes like this.
B4X:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("settings")
ETTextColors(Colors.Red)
..
End Sub

Then below this you put the other sub I have for you
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@Pedro:
Here is a complete working example that I created for you. It illustrates the process for you.
 

Attachments

  • PedroMaharesEditText.zip
    7.2 KB · Views: 189
Upvote 0

Pedro Naranjo

Member
Licensed User
Longtime User
Yes i have used your function as i say code don't enter in IF condicion.

I don't undestand why this happens.

Here is my code:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   
   Activity.LoadLayout("Configuracion")
      
   'modMain.SetEditText_Textcolor(Activity, Colors.Cyan)
      
   ETTextColors(Colors.Cyan)
End Sub

Sub ETTextColors(ForegroundColor As Int)
    For i=0 To Activity.NumberOfViews-1
   
       LogColor("GetType(Activity.GetView(i)): " & Activity.GetView(i), Colors.Magenta)
        
      '// NEVER ENTER IN IF. WHY??
      If Activity.GetView(i) Is EditText Then
            Dim MyViewET As EditText
            MyViewET=Activity.GetView(i) 
            MyViewET.TextColor=ForegroundColor
        End If
     
    Next     
End Sub

In "Configuracion" layout there is only one EditText.

I am using B4A v 2.71
 
Upvote 0

bparent

Member
Licensed User
Longtime User
Change EditText color?

You show But its crash in this line:
Dim textBox As EditText = vw
but shouldn't it be But its crash in this line:
Dim textBox As EditText : textBox = vw



I am making a sub that changes all EditText TextColor property in a Activity.

But its crash in this line:
Dim textBox As EditText = vw

Error:
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

What am I doing wrong?

Here is the code:

B4X:
Sub SetEditText_Textcolor(ActivityName As Activity, ForegroundColor As Int)
   Dim i As Int
   Dim vw As View
      
   For i = 0 To ActivityName.NumberOfViews - 1
         
      vw = ActivityName.GetView(i)
      
      If GetType(vw).IndexOf("TextView") > 0 Then
        'If vw Is EditText Then ' DO NOT WORK
          Dim textBox As EditText = vw '// This Line CRASH
         
                textBox.TextColor = ForegroundColor
      End If
               
   Next
      
End Sub
 
Upvote 0

Pedro Naranjo

Member
Licensed User
Longtime User
Hi bparent

No, it fails again

See attached picture
 

Attachments

  • Error_Cast.PNG
    Error_Cast.PNG
    74 KB · Views: 351
Upvote 0

Pedro Naranjo

Member
Licensed User
Longtime User
Hi Mahares

Yes, your code works fine.

But, i was thinking the differences between your code and mine and... they are the same (or very similar).

The problem is not the code. The problem is the layout.

In my layout "Configuracion", i have an EditText view INSIDE A PANEL.

When a EditText is inside a PANEL it renders as type "TextView" instead of "EditText".

That is the problem.

Thank you for your time Mahares
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
In that case just replace activity with MyPanel (use the name of your panel) in the below sub. See below:
B4X:
Sub ETTextColors(ForegroundColor As Int)
   For i=0 To MyPanel.NumberOfViews-1
      If MyPanel.GetView(i) Is EditText   Then
         Dim MyViewET As EditText
         MyViewET=MyPanel.GetView(i) 
         MyViewET.TextColor=ForegroundColor
     End If
   Next    
End Sub
 
Upvote 0

ELCHARO

Member
Licensed User
Longtime User
I have similar problem:
The fist line have Tag, the second a TextView not.

I LOG THIS

(EditText): Left=691, Top=0, Width=76, Height=72, Tag=
(TextView): Left=5, Top=0, Width=-1, Height=-1 NO TAG
null
NO TAG


B4X:
' WORK CODE       For i=0 To Activity.NumberOfViews-1
'            If Activity.GetView(i) Is EditText   Then  'FILTER A TEXTVIEW
'                If Activity.GetView(i).Tag = var Then
'                        Activity.GetView(i).Top=pos
'                End If
'             End If
'        Next   

FAIL CODE       
        For Each V As View In Activity.GetAllViewsRecursive
            If V Is EditText  Then    'NOT FILTER A TEXTVIEW
                    Log(V)        
                    Log(V.Tag)    'NULL TAG LOG
                    If V.Tag=var  Then    FAIL HERE OF COURSE ADD TO NULL
                        V.Top=pos
                    End If
            End If
        Next

Begin the problems,when add a listview.

I try to compare a View whith TEXTVIEW but B4X don't reconigze a TEXTVIEW in code.



Work well because change to first code,
but somebody can explain
¿why the second not work? THANKS....
 
Upvote 0
Top