Eclipse Simple Android App (ESAA)


ESAA:   SOLD       




The word pelontar? Why not a foreword? It's very official, this book hasn't been published yet, so whether it's a word pelontar, a foreword, it's up to me, hehehe.

This book is a short, clear, and very substantial book. Why is it so substantial? Yup, I deliberately made this book to explain the stages of creating a good Android application, not messy? What do you mean?

Most people who learn Android learn Android directly without understanding the concepts and design patterns in Java, even though if they just make an application, it can be difficult to develop, especially if the work is done by many people.

Here I will discuss the procedures for making applications, don't look at the simple examples, but look at the stages and concepts that I will discuss hehehe

1. Create a project first

The first time you want to create an application, of course you have to create a project first, don't just create it suddenly. BTW, do you know how to create an Android project? If you really don't know, just click File > New > Android Project in Eclipse.


1. Create a project first

Please name the project, whatever you want, it's free, no charge. For example, I named the project Contacts. Why Contacts? Because the application I want to create now is a simple contact management application. But remember, it has nothing to do with the contact application on Android. This is just a fun application, hehehe. Once done, please click the Next button >


target version of the application for what version of Android

If you have, now select the target version of the application for which Android version, because my cellphone's operating system is Android version 2.3, so I chose 2.3, if you are, please choose the one that suits your cellphone, I don't care. If you are done, click Next > again.


name the application

Now just give the application a name, here I give the application the name Contact. In addition, also enter the package name, don't forget to check Create Activity, then enter the Activity class name. Why do you need to check it? Because if it's not checked, I'm lazy if I have to create an Activity class manually, if there is an automatic one, why do it manually? It's just tiring

If so, click the Finish button, Eclipse will automatically create the project for us (us? You know, not me)


Eclipse automatically makes the project for us

2. Create a Domain Class or Model 

Domain Class? Model Class? If you don't know what that means, then you're lucky to read this book, if you never read this book, you're doomed to twelve. Okay, let me explain...

Domain or Model class is a class that represents data, what data? Yes, any data, it can be entity data, tables, or anything else. In this book, because I am making a Contact application, it means there will be contact data, which means the Domain or Model class is the Contact class.

The contents of the domain class are just attributes, for example in the Contact class there will only be attributes, name, telephone and email, if you want to add it, it's up to you, I don't want to add any more, taranjangan make the book

Before making a Contact class, now we first make the package, don't stack them in one package, that's not good? Who said that? Grandma said that of course it's not good, if they are stacked in one package, imagine if there are 1000 classes, you could get drunk looking for them

Back to topic, to create a package, you can select File -> New -> Package:


Fill in the package name

Fill in the package name, for example I filled in com.stripbandunk.android.kontak.model, you can enter whatever you want, I don't care.

If so, now just create the Contact class, to do this select File -> New -> Class:


Don't forget to include the package you created earlier

Don't forget to enter the package that was created earlier, then please give the class a name, for example here I give the name Contact, because I want to create a class that contains Contact data. If so, click the Finish button.

Eclipse will automatically create a Contact class, the contents of which are as follows:


Now it's time to add attributes

Now it's time to add attributes, name, email and phone, I want to use all the data types using String data type, why? Well, it's impossible for the data type name to be a number, are you crazy? 


Sorry, I used the picture on purpose

Sorry, I deliberately used images, not text, so you can't copy-paste.   That's great, I'm tired of coding, you just copy-paste. 

If you often code in Java, it must be a little strange. Why are the attributes public? Not private? And where are the getters and setters? Calm down, calm down. 

Don't think that we are coding on a desktop or web application, now we are coding on a mobile application, the more code we create, the slower the application on the mobile will automatically be, because the performance of the mobile is not like on a computer, so make sure to make the code as economical as possible,   including here, no need to use getters and setters. 

3. Create a Database 

Calm down, calm down, don't be surprised yet.   Here I'm not really telling you to make your own database.   You'd be crazy if you told me to make your own database, hehehe.

What I mean is we first create a database class, pretending that the data is stored in a database, whereas here I only store it in a List. As usual, create a package first:


make a package first

If so, now create the class:


make the class

Here I use the name ContactDatabase, so it is clear that this class is a database class for Contact data.

If you have, click the Finish button.


The contents of this ContactDatabase class are just a few methods.

The contents of the ContactDatabase class are only a few methods, namely add(Contact) and findAll().

The add(Contact) method is used to add contact data, and the findAll() method is used to retrieve all contact data. Because this ContactDatabase class is accessed everywhere, I made this class a singleton class.


I made this class a singleton class

Now add the add(Contact) and findAll() methods


Now add the add(Contact) and findAll() methods

Now I have added the add(Contact) and findAll() methods, please retype them   hehehe.

To use the ContactDatabase class, now you can use the code as below: 


use this code

4. Create a Menu Page

Now it's time for us to create a menu page first, here I will create a menu page containing two buttons, the "Create New Contact" and "Contact List" buttons. The "Create New Contact" button when clicked will display a page to create a new contact, if the "Contact List" button is clicked, a page will appear containing a list of all contacts that have been created, more or less like that 

5. Create a Menu Page Layout

To create the page, here I use xml layout. When you first create an Android project, Eclipse automatically creates a main.xml file in the res/layout folder, try opening that file, the contents are more or less like this:


5. Create a Menu Page Layout

Now it's time for me to change it, I added two buttons that I explained earlier, so it looks like this; eng ing eng!!!


I added two buttons

6. Create a MenuForm Class 

If you have created a menu page layout, now is the time to create a MenuForm class. MenuForm? Yup, most people if they want to manipulate the xml layout like taking components, usually in the Activity class. It's not wrong, but imagine if there are 100 components that have to be taken? It can be dizzy looking at the code 

Remember that good code is effective code that is small in number, not a lot of complicated code. 

Now create a package first:


make a package first

Just created the class 


Just created the class

The class name is MenuForm, given the name Form so that it understands that this is a user interface class, or it can also be named MenuView. The result is like this:


Menu Form

Now just add the Activity attribute. Activity? What's it for? Well, for later so we can take the components in the layout, you know. 


add Activity attribute

If so, then we add the components in the main.xml layout.


add components

Next, add getters for the Activity and its Buttons.


add getter

Ok, finished creating MenuForm 

7. Edit ContactActivity 

When you first create a project, there is a class called KontakActivity, it is the main class that is run when the application runs on Android. Now we edit the class. Previously the class was like this:


Now we edit the class

Now we add the MenuForm attribute:


add MenuForm attribute

Once added, we just need to add OnClickListener to the buttons on the MenuForm. First, we implement the KontakActivity class:


add OnClickListener

Just add the OnClickListener to the buttons in the MenuForm:


OnClickListener to the buttons in the MenuForm

Once done, then what about the contents of onClick()? That will be later, when we have created the contact list page and the create new contact page.

8. Create a New Contact Page

Now it's time for us to create a page for a new contact, as the title suggests, this page is for creating a new contact.

9. Create a New Contact Page Layout 

Okay, as usual, now we first create an XML layout for a page to create a new contact, to do this, select File -> New -> Android XML File.


9. Create a New Contact Page Layout

Select the resource type as Layout, the project as Contact and the file name is up to you, I use the name create   And make sure to select the root element as LinearLayout. Once done, click the Finish button. Now just edit the file to be like this:


edit the file to be like this

This is not enough, here is the continuation:


finished making the page layout

Done, finished making the page layout, now create the form class again.

10. Create a Class CreateForm 

The purpose is the same as creating a MenuForm class, this is a class representation for the layout we created earlier.

Now there is no need to create a new package, because we have created a form package before. Just create the class directly:


just create the class

As usual, add the Activity attribute in the CreateForm class, then add the EditText Name, Phone, Email and Save Button, the result is like the image below:\

add the Activity attribute in the CreateForm class

If so, just create a getter for all the attributes, like this:


just make a getter

And now it's finished making CreateForm 

11. Create a Class CreateActivity 

To display the menu page, we use ContactActivity, but to display the page to create a new contact, we have to create a new Activity class, I named it CreateActivity.


11. Create a Class CreateActivity

Don't forget, the superclass must be android.app.Activity class. If you have clicked the Finish button. The result is like this:


The superclass must be the android.app.Activity class

Don't forget to override the onCreate() method, then change the contentView to the create.xml layout:


override onCreate() method

After that create the CreateForm attribute:


create attribute CreateForm

Add OnClickListener to the save button in CreateForm, but first implement OnClickListener in the CreateActivity class.


OnClickListener to save button in CreateForm

Just add it to the save button:


Just add it to the save button

Finally, just add the code to save the contact data in the onClick() method:


code to save contact data

12. Adding reset() method 

Usually, if you have saved the data, the contents of the form are deleted again, so for that we can create a reset() method in the CreateForm class, so the CreateForm class adds a reset() method as below:


12. Adding reset() method

And this method is accessed in the CreateActivity class in the onClick method, so it is changed as below:


change code

13. Add CreateActivity to AndroidManifest.xml

It doesn't end here.   When we create an Activity class, we have to add the Activity class to AndroidManifest.xml, so it was like this:


13. Add CreateActivity to AndroidManifest.xml

We add to it:


add label and name

We add a new <activity>, where label is the title of the page, and name is the name of the class, no need for the package, just use .CreateActivity (start with a dot, don't forget).

14. Implementation of Create Contact Button Action in Menu

This create new contact page will appear if the create new contact button on the menu page is clicked, so we need to add this code inside the ContactActivity class:


14. Implementation of Create Contact Button Action in Menu

And that's it 

Continue working on the others

15. Create a Contact List Page

After creating a page for a new contact, now we create a contact list page. This page is used to display all contact data that has been added previously.

16. Create a Contact List Page Layout 

As usual, we first create the XML layout.


16. Create a Contact List Page Layout 

It only contains a ListView to display contact data.


It only contains ListView

17. Create a ListForm Class

After creating the xml layout, now we create another DaftarForm class. As usual, create a new class first:


17. Create a ListForm Class

Click Finish, and now the result is as shown in the image below:


Class Results RegisterForm

Don't forget to add the Activity attribute and ListView attribute taken from the xml layout:


add Activity attribute and ListView attribute

Then make the getter too:


make the getter too

And that's it, you're done making the RegisterForm. 

18. Create a ListActivity Class

After creating the contact list page, now it's time to create DaftarActivity to display the contact list page. First, we create the DaftarActivity class:


18. Create a ListActivity Class

Don't forget the superclass android.app.Activity. If you've clicked the Finish button. Next, override the onCreate() method and change the contentView to a list layout:


override the onCreate() method and change the contentView

Next, create the DaftarForm attribute:


create ListForm attribute

After that, now it's time to display the list of contacts in the database to ListView, so that it's easy to read, I want to create a new method, called loadKontak(), so it's like this:


create a method loadContact()

Now we fill in the loadKontak() method, so it looks like this:


fill in the loadContact() method

19. Adding ListActivity to AndroidManifest

Next we add DaftarActivity to AndroidManifest, so it looks like this:


19. Adding ListActivity to AndroidManifest

20. Implementation of Contact List Button

Finally, we implement the onClick() action in the KontakActivity class, which was previously implemented for the action of creating a new contact, now we just need to register the contact action.


20. Implementation of Contact List Button

Finished 

21. Trying the Application

Now we will try the application, I use my cellphone, more or less the result is like this. This is the display when the menu page appears:


21. Trying the Application

This is what it looks like when the Create New Contact button is clicked:


view when the Create New Contact button is clicked

Just enter the contact data, then click the Save Contact button. For example, I entered some contact data 

If you have, just click the Back button to return to the menu page, then click the Contact List button, the result will be like this:


Click the Contact List button

Hmm... it looks weird.   It can't be read by people   hehehe. How come? This is because the data in each row is the Contact object data, and the ListView will display its toString() method. The default for the Contact object's toString() is as above. So it seems like there needs to be some improvement first so that it can be read better.   Ok...

22. A Little Improvement

To make improvements to the display in ListView, it's easy, we just override the toString() method in the Contact class, then return the String data according to the desired format, for example:


22. A Little Improvement

And now if it is run the results are as follows:


Better than before, right? Hehehe..

Aha!!! 

By completing this booklet, now you know about data modeling, so don't just make an application, model it first, what data is there, it can be Food, Drink, Category, Goods, Users, and others, in this book the data model is Contacts.

Now you also know that after creating an xml layout, don't forget to create a Form or View class, don't pile up the code in the Activity class, imagine if there are 100 components, it can be dizzy reading. It's better to have many files and little code, than few files and thousands of lines of code, it's dizzy reading!

And for sure, now you understand the stages of making an application on Android, all you have to do is develop this application, and also make more complex applications!!!

Remember! People who are good at Android development are not people who memorize Android code, but people who often practice developing on Android, the more often, the better, the less often, the more oon 

Next Task

Don't be satisfied just because you've finished making this contact application, it's a very simple application, now your task is to:

  1. Add contact data attributes, such as address, website, yahoo, gtalk, etc.
  2. Add edit contact data 
  3. Add delete contact data 
  4. Add contact data search 
  5. And it's up to you... Have a good job!!!

Good Luck!!!


Post a Comment

Previous Next

نموذج الاتصال