Contact phone number chooser

DEEFRAG2K

New Member
Licensed User
Longtime User
Hi, i was trying to create a button that, when clicked, shows a list of all the contact names and there Phone numbers. when the user clicks on a phone number (or contact name) that phone number is copied into a text box.
How can i make that happen?
 

DEEFRAG2K

New Member
Licensed User
Longtime User
There is another example in the manual: Basic4android - Phone which prints the contacts to LogCat.

awww, c'mon, are you kidding me?
i don't need all the stuff in the log, i just want to choose a particular number from the address book of the phone.
like i write before:
user clicks a BUTTON.
A list of all the contact names and saved telephone numbers shows up.
User selects one contact from that list.
The Mobile Phone Number of that contact is written in a TEXTBOX and stored into a variable.
I'm a noob programmer and need help to accomplish my goal.
I'm not able to abstract that given samples and use them for my needs.
I just need a working sample with the things i described above.
Is that so damn hard?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here: (don't forget to add a reference to the Phone library)
B4X:
Sub Process_Globals
    
End Sub
Sub Globals
    Dim EditText1 As EditText
    Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    EditText1.Initialize("") 'Need to call Initialize becuase views are added manually
    Button1.Initialize("Button1")
    Button1.Text = "Click me!"
    activity.AddView(Button1, 10dip, 10dip, 200dip, 100dip)
    Activity.AddView(EditText1, 10dip, 120dip, 300dip, 100dip)
End Sub
Sub Button1_Click
    Dim Contacts As Contacts
    Dim list1 As List
    list1 = Contacts.GetAll
    Dim listOfNames As List
    listOfNames.Initialize
    'Create a list with the contacts names
    For i = 0 To list1.Size - 1
        Dim c As Contact
        c = list1.Get(i) 'fetch the Contact from the original list
        If c.DisplayName.IndexOf("@") = -1 Then 'remove email only contacts
            listOfNames.Add(c.DisplayName)
        End If
    Next
    Dim res As Int
    res = InputList(listOfNames, "Choose contact", -1)
    If res <> DialogResponse.CANCEL Then
        Dim name As String
        name = listOfNames.Get(res)
        Dim c As Contact
        'find the original contact based on the chosen name
        For i = 0 To list1.Size
            c = list1.Get(i)
            If c.DisplayName = name Then Exit
        Next
        If c.PhoneNumber.Length > 0 Then
            EditText1.Text = c.DisplayName
        Else
            Dim m As Map
            m = c.GetPhones
            If m.Size > 0 Then
                EditText1.Text = m.GetKeyAt(0)
            Else
                EditText1.Text = "N/A"
            End If
        End If
        Dim PhoneNumber As String
        PhoneNumber = EditText1.Text
    End If
End Sub
 
Upvote 0

tremara1

Active Member
Licensed User
Longtime User
too nice Erel

I always remember the old saying....."Give a man a fish and you feed him once...Teach a man to fish and you feed him for life"
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
I was looking for this as well but on my 2.2 phone it doesn't work as it should, most of the contact details selected make the value of Phone number the contacts name not their number.

All my contacts are just mobile numbers but I tried every contact and only two came up with numbers instead of the contacts name.

Any idea why?

I just ran the code exactly as shown in this thread so not added or removed anything.

Dave
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can change this code:
B4X:
If c.PhoneNumber.Length > 0 Then
            EditText1.Text = c.DisplayName
        Else
            Dim m As Map
            m = c.GetPhones
            If m.Size > 0 Then
                EditText1.Text = m.GetKeyAt(0)
            Else
                EditText1.Text = "N/A"
            End If
To:
B4X:
            Dim m As Map
            m = c.GetPhones
            If m.Size > 0 Then
                EditText1.Text = m.GetKeyAt(0)
            Else
                EditText1.Text = "N/A"
            End If
 
Upvote 0

DEEFRAG2K

New Member
Licensed User
Longtime User
Thanks Erel, but its not quite what i was looking for because:
It just shows the contact names in the list where the user can choose from, i wanted the phone numbers to displayed as well, i want that the user has the option to choose a specific phone number from the list. most people have more then one phone number stored in a contact name. I need the mobile phone numbers, but often it selects the home phone number.
 
Upvote 0
Top