In this part of my Android Development Tutorial I continue building the Android Address Book app. If you missed the first part it is here.
This time I finish up the interface for my app. I walk you step-by-step through everything. I then go through every query I will be issuing to my SQLite database in the app. I show how to create tables, insert contacts, update contacts, delete contacts, output everything and even drop the table. Here is my SQLite3 Tutorial if you need it.
If you like videos like this, it helps to tell Google+ with a click here [googleplusone]
Code From the Video
add_new_contact.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000" > <TextView android:id="@+id/addContactTextView" // ORIG android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="@dimen/padding_5dp" android:text="@string/add_contact" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#FFFFFF" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/firstNameTextView" // ORIG android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/padding_5dp" android:text="@string/first_name" /> <EditText android:id="@+id/firstName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPersonName" android:paddingRight="@dimen/padding_5dp" > <requestFocus /> </EditText> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/lastNameTextView" // ORIG android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/padding_5dp" android:text="@string/last_name" /> <EditText android:id="@+id/lastName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPersonName" android:paddingRight="@dimen/padding_5dp" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow4" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/phoneNumberTextView" // ORIG android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/padding_5dp" android:text="@string/phone_number" /> <EditText android:id="@+id/phoneNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="phone" android:paddingRight="@dimen/padding_5dp" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow5" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/emailTextView" // ORIG android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/padding_5dp" android:text="@string/email_address" /> <EditText android:id="@+id/emailAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textEmailAddress" android:paddingRight="@dimen/padding_5dp" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow6" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/addressTextView" // ORIG android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/padding_5dp" android:text="@string/home_address" /> <EditText android:id="@+id/homeAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="text|textPostalAddress" android:paddingRight="@dimen/padding_5dp" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow7" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/save_data" android:onClick="addNewContact" android:layout_weight="1"/> </TableRow> </TableLayout>
edit_contact.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000" > <TextView android:id="@+id/editContactTitleTextView" // ORIG android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000" android:padding="@dimen/padding_5dp" android:text="@string/edit_contact_title" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#FFFFFF" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/firstNameTextView" // ORIG android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/padding_5dp" android:text="@string/first_name" /> <EditText android:id="@+id/firstNameEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPersonName" android:paddingRight="@dimen/padding_5dp" > <requestFocus /> </EditText> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/lastNameTextView" // ORIG android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/padding_5dp" android:text="@string/last_name" /> <EditText android:id="@+id/lastNameEditText" // ORIG android:id="@+id/lastName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPersonName" android:paddingRight="@dimen/padding_5dp" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow4" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/phoneNumberTextView" // ORIG android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/padding_5dp" android:text="@string/phone_number" /> <EditText android:id="@+id/phoneNumberEditText" // ORIG android:id="@+id/phoneNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="phone" android:paddingRight="@dimen/padding_5dp" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow5" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/emailTextView" // ORIG android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/padding_5dp" android:text="@string/email_address" /> <EditText android:id="@+id/emailAddressEditText" // ORIG android:id="@+id/emailAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textEmailAddress" android:paddingRight="@dimen/padding_5dp" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow6" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/addressTextView" // ORIG android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/padding_5dp" android:text="@string/home_address" /> <EditText android:id="@+id/homeAddressEditText" // ORIG android:id="@+id/homeAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="text|textPostalAddress" android:paddingRight="@dimen/padding_5dp" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow7" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/editButton" // ORIG android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="editContact" android:text="@string/edit_button" android:layout_weight="1"/> <Button android:id="@+id/deleteButton" // ORIG android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="removeContact" android:text="@string/delete_button" android:layout_weight="1"/> </TableRow> </TableLayout>
Android Address Book App SQL
sqlite3 addressbook.db first_name last_name phone_number email_address home_address id CREATE TABLE contact( id INTEGER PRIMARY KEY AUTOINCREMENT, first_name TEXT, last_name TEXT, phone_number TEXT, email_address TEXT, home_address TEXT); INSERT INTO contact (first_name, last_name, phone_number, email_address, home_address) VALUES ('Sally', 'Smith', '4125551212', 'sally@aol.com', '123 Main St Ida, Iowa'); sqlite> .separator " : " sqlite> select * from contact; INSERT INTO contact (first_name, last_name, phone_number, email_address, home_address) VALUES ('Paul', 'Long', '4125552222', 'paul@aol.com', '124 Main St Ida, Iowa'); UPDATE contact SET phone_number = '4125552122', last_name = 'Todd' WHERE id = 1; DELETE FROM contact WHERE id = 1; SELECT id, last_name, first_name FROM contact ORDER BY last_name; DROP TABLE IF EXISTS contact;
Hey Derek, how do i “connect” the sql file with the project? Do i have to put it inside the project and/or have an SQL platform or something?
Hey, In my Android Development Tutorial 12 I cover how the SQLite database is created directly from code in Android. Check that out and you will be ready to go.
Derek,
After rolling back to the previous version of Java, like you suggested, I am still having the same issue. Specifically, it appears that if an attribute is not pre-written into the code, I cannot add it via the graphical layout. Example… if I wanted to click on the box next to “Text” to change it… if the line is not there in the code… android:text=”@string/something”… the option in the graphical layout is grayed and will not let me choose anything. So it is effectively forcing me to hand code any attributes to items. I can drag and drop anything into my layout but can only change items in the “Properties” area that come with the item. Hopefully this makes some sense. Having not worked with eclipse before starting your tutorials I am sure I am just missing some little setting or option. Hopefully you are able to point me in the right direction. Google is not helping me much to figure this issue out.
Thanks!
Pam
Pam,
You need to update your ADT Eclipse plugin. (Help -> Check for Updates) in Eclipse. That should definitely solve it. Remember this because you’ll use it very often when Eclipse starts acting weird. You probably will have to restart Eclipse as well after updating.
Tell me if that doesn’t work.
Derek,
No updates were found. Maybe I didn’t install everything I needed to? I just got the latest versions of everything on the 10th(ish) of this month. I followed along with the instructions listed here… (Found your tutorials afterward. Maybe before I get too involved I should just uninstall everything and start over?
http://java.dzone.com/articles/getting-started-android?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3a+javalobby/frontpage+%28Javalobby+/+Java+Zone%29
However, I chose the latest versions of all installs. It isn’t a HUGE deal really. Just a pain to not be able to easily change properties.
Thanks!
Pam
Derek,
One more thing… after rolling back java, when it does act like it may work I actually get an error…
“Unhandled event loop exception
Item not added”
It then tells me that “An SWT error has occurred. You are recommended to exit the workbench. Subsquent errors may happen and may terminate the workbench without warning.”
Researching google right now. If you have any other suggestions I welcome them gladly. =) I appreciate all your assistance. Will let you know what I find.
Pam
Pam,
Try upgrading to Kepler like I cover here on how to set up Android Development Tools
This is the message from the Eclipse people
We’ve been seeing a very similar stacktrace running Eclipse 3.7.2 (Indigo SR2) 32-bit on a specific Windows 7 64-bit machine… but that specific machine only.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=287508#c22 has a deep investigation. From the symptoms, it looks like a Windows issue triggered by existence of certain drivers, that’s why it’s not reproducable on all machines.
I have the same issue running on Linux. I am unable to add new properties from the GUI, although I have not seen the error that Pamela reported. Usually, clicking in a previously unset property simply does nothing. Sometimes, if I click repeatedly, the options will come up, but it will not actually add my selection. This is clearly not a Windows only problem. Until I saw this post I thought that it might be a Linux only problem, but it seems to exist under both OSes.
Take a look at part 26 of my Android tutorial. I show how to install all the new Android software in it. I hope that helps.
You Sir are one great teacher. Please continue your good work to and for the world..for people like me. Thank you so much.
You’re very kind 🙂 Thank you for the nice compliments.
at first i tried bucky roberts with thenewboston.
then i tried adam khoury with developphp.
then i tried you.
with bucky i learned php.
with adam i learned oop.
with you i learned android.
without them i’d never have made it this far with you. It’s not that you assume everyone knows what you’re saying, it’s just that you don’t waste time in the explanation. You understand as well as I do that we’ll get it, eventually. The eureka will happen, eventually. I learn from example, from repetition. I like your methods, Derek. I’m sticking with your tutorials from episode 1. I have an idea for a great android app, that only you can make a reality. For that I am truly grateful. Keep up the good work.
Thank you 🙂 Yes I think it is great that most of the tutorial people on YouTube are working together and not against each other. This happened naturally even though we don’t know each other. I have always tried to make original tutorials and I’m glad that you have found them useful.