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
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
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.
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
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
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....