So here we are on part three of our guide to developing for Android, and you haven't even written one solitary line of code yet. Sheesh. It's time to get your hands dirty in functions, variables, classes and methods. There are countless languages out there; each has its benefits, and each has its irritants. Which language you use is often dictated by what you're creating and the platform you're developing for.
Teaching Java is something that is sadly outside the realms of this series -- that is a project all of its own. So it's time to let you in on...
I'm going to let you into a select club -- it's the thing that makes coders seem to know everything they need to do their jobs. It's...yup, it's Google. Isn't it always?
The fact is, no matter how unique your app, program, web page or script, it'll be made up of different functions and routines that will have been tackled by someone else before. It doesn't mean you'll necessarily find the best solution, but often studying and exploring other people's code helps you to understand what you need to do and opens up options you hadn't considered. There are hundreds, if not thousands, of excellent Java resources out there, so go have a look around and familiarise yourself with the language. (I'd recommend Javadoc for the official documentation; Stackoverflow for instant access to experts, in all languages, and lastly, Oracle's Java tutorial.)
In addition to Java, there is another language you'll need to 'understand' to create an Android app -- XML. XML is a mark-up language -- similar to HTML, the language that builds the layout of web pages. It is effectively a hierarchy of tagged fields that is used in a variety of ways. Android uses XML to define page and app layouts and you can have different XML for different devices, so you can build a mobile and a tablet version of your project and sit it on the same code base. XML can also be used to share and supply data and, for our app, the RSS feed we access is a subset of XML designed for the sharing of website data.
We'll need to create layouts for both the 'presentation' of the Gizmodo content and the settings page. We'll start with the settings page -- as we need settings in order to work out what we're doing with the content and how we retrieve it!
Within your project, navigate to res > layout and then right-click on the layout folder. Select New > File and name it activity_settings.xml.
We'll be building a linear layout, which means that each item we place in the XML will appear in order and makes the XML and layout as simple as possible. The core XML for this layout is shown in the screenshot below.
Breaking this down, we can see we're creating a full screen layout -- the layout_width and layout_height settings are set to 'match_parent' -- the parent being the main device screen. All of the elements that then make up the page will be placed between the LinearLayout tags.
Our settings page will, for now, just allow us to set the RSS feed location -- that way if the feed moves, we can update the settings and the app won't break. This is known as defensive coding -- we're coding to ensure as much flexibility as possible and to ensure that any unforeseen changes or events don't break our product. If we hard coded the feed URL and it then changed, the app would no longer update with content.
With this in mind, we need to add a text field and a button to save. These elements are easy to add once you know the options available to you. We'll be using an EditText node for the field that will contain the RSS URL and Button note which will be the save button.
As you can see above there are various attributes you can pass in to ensure your app knows what it's doing.
android:id -- simply a unique identifier for the layout that allows you to access the content stored within the field. In the example above the value has an @ symbol -- this means it is a resource and the + symbol is used the first time you define the resource ID.
android:layout_width and android:layout_height -- setting these to wrap_content means that the field won't be a fixed size, but instead will expand or contract to the appropriate size depending on the content
android:hint -- this is another resource, this time it's what will fill the field if it is empty to ensure the user knows what the field is supposed to contain. In this case @string/rss_url says to use the value in the resource called rss_url. We define this in the default resource file in res > values > strings.xml.
We'll also add our button using the following XML:
Save your layout and open the above strings.xml file -- ensure you select the 'strings.xml' tab at the bottom rather than the Resources tab. First, you can delete the hello_world line -- we're not using it so you may as well save a couple of bytes by removing it. Then add the following lines in its place:
<string name="rss_url">Gizmodo UK RSS URL</string>
The resulting file should look like this...
Next time, we'll take the settings page and learn how to save the URL you enter. We'll also then look at how we start using the Java we're all going to be learning over the next two weeks to start making our app actually do something...
Colin Polonowski is a developer at Giz UK’s parent company Future Publishing, working across a number of their sites. He also runs The Digital Fix in his spare time. Check in on the 18th of April for his next App Millionaire guide.
To get on-the-spot news, app tips and the full lowdown on Samsung’s latest mobile announcements check out Samsung’s Your Mobile Life over here.