Android Question Copy Text in different TextFields / EditText

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello experts,
I have a text that is always in the same order:

title
name
familyname
date of birth
street + no
postcode
location
phone
e-mail
ID number

... so just like above, the text is one below the other.
I would like to automatically copy this text into the associated fields (by clicking on a button).

Which way is best for a solution?

Thank you in advance for your ideas!
 

Mahares

Expert
Licensed User
Longtime User
I would like to automatically copy this text into the associated fields (by clicking on a button).

Here is one way to do it:
B4X:
Private MyList As List
    MyList.Initialize
    MyList.Addall(Array ("Title", "Name", "Family Name", "Date of Birth"))

Sub  btn_CLick
    Dim et() As EditText =Array (EditText1, EditText2, EditText3, Edittext4)  'must declare this after you load the layout, not before
    For i =0 To MyList.Size -1
        et(i).Text= MyList.Get(i)
    Next
End sub
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello Mahares,

thank you for your proposed solution. :)

The problem is: :rolleyes:
The data are sent by E-Mail.
The data are always in the same order, but they don't have the same text, they are variables.

1. Step: the data are copied manually into the "L21_TextField_16.Text" field
2. Step: from there they should be copied by clicking an a button into the individual fields "L21_TextField_02.Text", "L21_TextField_03.Text", ... etc.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
YOU only need to know the reference for the correct field(s). The names of the fields DO NOT MATTER at all.

Load the Layout. Put all views you need to reference into a Map giving the "correct" name as name and put the editfield as value.
You then later can get the correct edittext reference from the map using the key you defined.

B4X:
define a global map and initialize it
Sub Globals
  dim myedits as map
End sub
' load your layout
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    myedits = CreateMap("Field1": editfield_1, "Field2": editfield_2:,"L21_16", L21_TextField_16) ' whatever the names are....
END SUB
sub something
    dim btn as edittext = myedits.Get("L21_16")
    btn.text = "New text"
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
First of all, I would like to apologize for not getting back to you until now, as I have little time for work.

... but somehow both solutions don't work that well.

myedits = CreateMap("Field1": editfield_1, "Field2": editfield_2:,"L21_16", L21_TextField_16)

@DonManfred: Are you sure that this line of code is correct ... I am irritated by the comma after "L21_16", ?

I made a short sketch (see attachment) to explain the problem better:
Sketch CopyText.jpg


In the "copytext" field, the data is always in the same order, but the data is recreated for each test customer.

After clicking the "CopyText" button, the data placed one below the other should automatically be copied into the "name", "familyname", "birthday" fields. and so on ...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Are you sure that this line of code is correct ...
yes.
Snap11.png

I made a short sketch (see attachment) to explain the problem better
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private EditText1 As EditText
    Private EditText2 As EditText
    Private EditText3 As EditText
    Private viewsmap As Map
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    viewsmap.Initialize
    Activity.LoadLayout("Layout")
    viewsmap.Put("Name",EditText1)
    viewsmap.Put("FamilyName",EditText2)
    viewsmap.Put("Birthday",EditText3)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim name As String = "Hans"
    Dim familyname As String = "Muster"
    Dim bday As String = "19.12.1968"
    
    Dim edt As EditText = viewsmap.Get("Name")
    edt.Text = name

    Dim edt As EditText = viewsmap.Get("FamilyName")
    edt.Text = familyname

    Dim edt As EditText = viewsmap.Get("Birthday")
    edt.Text = bday
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
How do you get the text ?
What is the text format ?
If it is a String with a line feed character you could split the string into an Array of strings, one for each line with Regex.Split.
And then use the principle suggested by Mahares.
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Sub Activity_Create(FirstTime As Boolean)
If i want to use it in B4XPages, can i set this code into this Sub?
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    
    Root = Root1
    neue_TestPerson.Initialize

'-- load the layout to Root --
    Root.LoadLayout("Layout_20_Testpersonen")
    
    viewsmap.Put("Name", L21_TextField_02)
    viewsmap.Put("FamilyName", L21_TextField_03)
    viewsmap.Put("Birthday", L21_TextField_04)
...
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Do you have an example of the text ?
Max
Muster
01.01.1900
and so on ...

It is a part of the E-Mail. The E-Mail is generated as auto-respond from a website and I haver to put the data into my App.
I do this by copy-and-paste on my smartphone.

I know this is not the best way, but I need a fast solution for the first time.
Normally I shoud synchronise the data with MySQL between the website and the app ... but it takes too long time to make a perfect solution.

 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
myedits = CreateMap("Field1": editfield_1, "Field2": editfield_2:,"L21_16", L21_TextField_16)
I made a copy of this line and set my own fieldnames inside, but there is an error-message:
Kompiliere den Code. Error
Fehler beim Kompilieren des Programms.
Fehlerbeschreibung: Syntax-Fehler:
Fehler in Zeile: 1317
neue_TestPerson = CreateMap("Key02": L21_TextField_02, "Key03": L21_TextField_03:,"L21_16", L21_TextField_16)
Word: :
... an with that code-line is no error-message:
B4X:
neue_TestPerson = CreateMap("Key02": L21_TextField_02, "Key03": L21_TextField_03, "Key04": L21_TextField_16)
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I do this by copy-and-paste on my smartphone.
But how and where do you copy the text ?
As I do still not understand how you define the text, attached a small B4XPages project showing how I would do it.
Up to you to adapt it according to your texts.
It works on all three platforms.

1630434866871.png
 

Attachments

  • TextEingabe.zip
    15.9 KB · Views: 202
Upvote 0

emexes

Expert
Licensed User
Longtime User
You should probably include some sanity checks on the data before copying it to the edit fields eg:

UserData(0) is title, which should only contain letters, spaces, hyphens, commas and full-stops
UserData(2) is familyname, which should only contain letters, spaces and hyphens
UserData(3) is date of birth, which can be checked that it is within a specified range (eg, not in the future) and represents a plausible age (eg, no toddlers at university)
UserData(5) is postcode, which is usually some checkable format (eg, in Australia is 4 digits, in USA is 5 digits, in UK is letters-number-separator-number-letters)
UserData(7) is phone, which should not contain any letters (ie usually only contain digits, spaces, plus, hyphens and brackets) (hmm- sometimes has "ext")
Userdata(8) is e-mail which should contain exactly one "@", at least one full-stop after the "@", and no spaces
Userdata(9) is ID number, which presumably should contain a set number of digits (eg, 5-to-8 digits long), and possibly either no letters or maybe on letter at end.

Also, the number of fields seems small enough that you could just assign them directly, eg:

B4X:
PRIVATE SUB btnCopy_Click
    PRIVATE UserData() AS STRING
   
    UserData = Regex.Split(CRLF, UserText)

    edt_Title.Text = UserData(0).Trim
    edt_Name = UserData(1).Trim
    edt_FamilyName = UserData(2).Trim
    'etc
 
Upvote 0
Top