Android Tabager #1

A while back I did a series of articles for a website, the site was a tablet ROM community. The intention of the articles was to demonstrate the basics of creating an Android app to a community of highly skilled people but with little coding experience. I tried an ambitious project were I would create an App for the community that they could publish in there ROMs, that would allow the user to check for updates of the custom ROM and to easily determine their device details and versions.

As I created the App I would create articles on what I was doing so that the community could follow along. In theory it was great, but it was very time consuming and never got the support it needed to advance past a proof of concept stage. Due to the hours put in I feel obliged to post it up.

Step 1: Create your project

Well be using eclipse to create the app, instructions for setting this up can be found in the introduction post.

To begin go to File -> New -> Android project

Give the app a name, Im calling mine Tabager School but you can call it what you want. Then click next.

On the next screen select android 3.0.

On the next screen fill in the application name and the package name.

Step 2: Make a toast

The code we will be using is the following:

Lets take a quick look at this, the first line we are declaring a variable which simply put is a way storing and transferring data within the program. 'String' is the type of variable were using and is used to hold text information. 'incremental' is the name of the variable.

In the second line we are populating the variable with the rom version.

The 3rd line we make the toast call. In the toast you have incremental, so were passing the text from the variable here, alternatively we could put "test" in and the toast would display test. The Toast.LENGTH_LONG determines how long the text will appear for, you could also use Toast.LENGTH_SHORT. So have a little mess round with this if you want.

Copy this and place it into your main activity, which would of been created with your app and opened by default. Paste the code in the onCreate method below setContentView(R.layout.main);. There is a screen shot further down if you're unsure.

You'll notice that there are red lines under toast, this is because toast is currently not recognised. We need to import the toast package into our application so that we can use its functionality. There are couple of ways to do this but the best is the short cut as you'll be importing a lot: press ctrl+shift+o. Youll notice that android.widget.Toast; has been added to the import section.

Step 3: Toast Review

At this point you should be looking at something like this:

image one

Lets have a brief rundown of what were looking at.

The first line is the package that this activity belongs to.

Below this we have the import section, which is were additional code that we are using in ours is located.

Below this we have our activity class, an activity is essentially a screen in an android app.

Below this we have "public void onCreate(Bundle savedInstanceState)" this is a method that runs when the activity is first launched. Tasks that need to start when then program is first started such as displaying text,images etc to the user is placed in here.

Below this we have the setContentView(R.layout.main); this is declaring the layout file for the activity which determines were objects are placed on the screen. This is not essential but is a common way to do things. I will briefly touch on it later and will talk more about in the next class.

You might be thinking lets run this sucker. But there are a couple of over things we need to do first and will be a good introduction as you will be using these files a lot in the future.

Step 4: The manifest

Firstly open the manifest file and alter it to look like the below:

image one

The manifest file is the config file for the app, and contains the app version, what devices it can work with, permissions and a few other things. What we are doing here is saying that we want it to run on sdk 14 which is 4.0 ics. But it can run as far back as sdk 11 which is 3.0 hc. This way the app will run on all tablet versions of android. Well be coming back to the manifest in the future.

Step 5: Main.xml

Next were going to look at the main.xml layout file. Have a look below:

image one

This file is layout file for our activity, which if you recall was being called with setContentView(R.layout.main);. When you create a new project it always adds a textview which displays the text "Hello World, TabagerSchoolActivity!". I have commented this out with . If you wish you can leave it or change it to whatever you want. If you want to change it you will need to open the strings.xml file which is in res-> values. This file is used to store text so that you can use it over again without needing to retype it, and so that you can change many instances of text from one place. The "@string/hello" is saying to look into the strings.xml file at the element called hello.

Step 6: Run the code

Right so let's run it... If you haven't already create a vm, to do this click virtual device manager, which is the 6th icon in the tool bar, it looks like a phone with an android on the screen. From here click new, I would recommend for this creating a 4.0 vm as it loads a lot quicker than the hc ones. Name the vm something like 4.0 and then select 4.0 for the target and click create. Let's run this and then select run which is the icon that looks like a play button.

But wait thats not my ROM... A vm is not a very good example for this, so were going to export our app to an apk and load it on our devices. If your a glass half full person, then great more learning. If your a glass half empty your probably screaming at your monitor "why won't it end!!!"

To create an apk we simply right click on the project in the package explorer on the right hand side and click export. From here we select export android application, then follow the instructions and create a new key store. It's all pretty much self explanatory. Then load your newly created apk to your tablet and fire it up. Personally I think the easiest way is to use dropbox.

And there we have it we've created our first app, that tells us the os version of the device...

Next time well pull more info and display it in the activity.

TabagerSchool:#2 Displaying info, and using layouts...