This project will help you practice the skills and concepts you learned related to structs and classes in Java. For this project you'll build an app that is a basic contacts manager app. It will allow you to save and view contact information for your friends.
- Create a new Android Studio project
- Name the project "Friends"
- Open the app's activity_main.xml file.
- Add two edit text fields to the main screen.
- Add a button to the main screen
- Make the button's text "Create Contact"
- Add a scroll view with a linear layout child to the screen.
- Create an even listener for the button in MainActivity.kt
- Create data members for all other widgets MainActivity.kt
- Create a new file called "Contact.kt"
- Create a class called
Contact
- Add
name
andphoneNumber
properties to the signature, both areString
objects. - Write an constructor that takes
name
andphoneNumber
as arguments and initializes the corresponding properties usingthis.
syntax. - Write a property called
display
with a getter that combines thename
andphoneNumber
and returns it
- Create a new file called "ContactsManager.kt"
- Create a class called
ContactsManager
- It should contain a property called
contacts
whose type isMutableList<Contact>
this property should be in the primary constructor definition and can be given a default value of a new empty list
- Add a method with the signature
fun createContact(name: String, phone: String)
. It should create a new instance ofContact
, and add it to thecontacts
ArrayList. - In
MainActivity.kt
, add a data member calledcontactsManager
instantiate and assign an instance ofContactsManager
to it
val contactsManager = ContactsManager()
can do this
- In the action for the "Create Contact" button, get the strings entered in the text field.
- Use these strings to call the contacts manager's
createContact()
method. - Write a method that will generate and return a
TextView
that accepts aString
as the text value. - Refresh the the text view by adding the new contact as a new text view to your linear layout view.
- Every contact from the array should be in this list
- Build and run your app using the your preferred testing environment.
- Enter various contacts making sure that they text view shows each contact you create.
If you finish and want another challenge try adding the ability to delete contacts.
TIP: You'll have to find a way to identify which contact to delete. The "TAG" attribute allows you to attach hidden information to the UI element. This could be used to add a unique id that would then be matched in the
Contact
class to ensure that the correct contact is deleted.