Android Tabager #2 Displaying info

Hey, so last time we took a look at how to create and export a simple android app. We also looked at a few key files, such as layout and string xml files. But it didn't really do much, so were going to change it in today's class, and well grab some more info.

Toast really isn't practical for this, for more data were going to have to display it in the activity like all those other apps out there you love so much. There are two ways to go about this:

1. Make a layout file,

2. Define the layout info of the object as its created in the activity

Step 1: Modifying the main.xml file

We'll be using the layout file, as it's the most common, it's a lot easier and has more functionality. So open up your project and open the main.xml file which is in res -> layout.

We want it to look like this:

Step 2: Review

Now there's a lot in there so let's jump in...

We're using a LinearLayout, essentially it allows you to create rows and columns in your layout. In these rows and columns you place your views. The benefits to this are that it will always keep its shape and layout no matter what the screen size. For more info go to Linear Layout | Android Developers, and one for your bookmarks is Hello, Views | Android Developers which goes into detail on all the different ways you can display information in your app.

Now with layout files everything is wrapped in nodes, and each node has an open and a close, if we take the first one for example

The scroll view allows things to displayed of the screen and to be navigated to by scrolling around much like a web page, as you can see all the other views are nested inside this so we will always be able view the information were going to be returning.

Next we have

Also this contains all the other views and is acting as giant container for everything else.

After this we have a horizontal layout with two more linear layouts inside it

As there are two objects inside the horizontal layout, two columns will be created. The android:layout_width="fill_parent" is saying expand as wide as the container you're in, android:layout_height="wrap_content" is saying only be as tall as you have to be. The android:layout_weight="1" is declaring the importance. The two columns are just as important as each other and will be as wide as they can be so they will split the screen in half, and as pre mentioned this will split any screen size in half.

Into one column We've put

So here we have a vertical layout which will fill the column, inside this we have several Text views which will be displayed rows inside the column. Let's take a look at one of the text views:

So a text view is just what you think it is, it is used to display text on the screen. We're saying that the text will only be as big as it has to be and with android:layout_gravity="center" and android:gravity="center" and were saying that we want it to stay in the middle of the row. textSize="20pt" is as I'm sure you've guessed the size of the text, pt is Points - 1/72 of an inch based on the physical size of the screen so it will scale based on the tablet size. The final line is the text that will be displayed, in this case it will be "Hardware Details".

If we look at another one

There are two points of interest on this one, the first is android:id="@+id/RAMinfo". This is the id of the text view so that we can make changes to one we want from the activity later on. The text is also blank as we intend to change later from the activity.

Step 3: Main Activity

So that's the layout done and dusted you should have a good idea of how things are getting displayed. Let's open up the main activity and start populating some of those text views.

So inside the create method were we had the toast from the last class, were going to place the following code

Step 4: Main Activity Review

At the top we've declared a few more string variables to hold our os details. Further down we are populating these in the same way as in the previous class

These are set to be displayed through the text views with the following code

With this we our creating a Text view in the activity called osDevice, and were setting the text view as Device from the layout file. Then osDevice.setText(device) sets the Text to the device variable.

There is also

The difference with these two is that were calling a method ReadCPUinfo() when we set the text. The two methods are

I'm not going to go into too many details about these, but there essentially sending a Linux based command to a group of files to return info on the cpu and memory. These two methods can be place just outside the onCreate method.

Step 5: Summary

So the entire activity file will look a little something like this

Give this a test run on a vm and then export to your device (just as you did in the last class) and you should have a much more useful app that displays hardware and os specs.

What's that I hear you say, it's not much on the eyes. Well join me next time for TabagerSchool:#3 All the colours of the rainbow, well cyan at least.

TabagerSchool:#3 All the colours of the rainbow, well cyan at least...