AIDE—Developing for Android on Android

Now that you know a little about how projects are handled, let's start creating your very first app. If you have a project open right now, go ahead and close it. Change directories in the file pane until you are in the main directory for all of your projects. From here, you can select "Create new App Project here", and name your project "MyFirstApp". You can set the package name to whatever you like, as long as it follows the usual format. Also, be sure that you have selected the "Hello World" app template.

Once everything is filled in, go ahead and click the create button. This will create all of the required files and open the new project in the IDE. The file extends the class "Activity". This is the main class you will be dealing with when developing apps. Your app builds on this class and overrides the methods within it to make your app do its own thing.

The first method to override is "onCreate". This method is called when your activity first is created. The usual tasks involve creating the graphical elements of your program and getting them initially rendered and displayed. The first step (super.onCreate) runs the code in the main class "Activity" to handle all of the OS-related tasks in creating a new activity. You then add your own code, usually GUI creation.

In the Hello World template, it includes a call to the "setContentView" method, which assigns a particular layout as the content view. The beginning value is the layout "R.layout.main". Layouts are XML files, containing all the elements that make up your graphical interface. To get to this file, you can change to the directory res, then layout. Within this subdirectory, you will find XML files for each layout that is defined.

Figure 9. Layout files are stored in the subdirectory res/layout.

In this simple program, you just have a single file called main.xml. Selecting it in the file pane loads it into the editor pane. The outermost container in the XML file is the type of layout being defined. A number of options are available. In the Hello World template, the layout type being used is a LinearLayout. You can set a width and height for the entire layout with the properties android:layout_width and android:layout_height. In this example, let's leave the defaults of "fill_parent" for both. You also can set the display's orientation; in this case, it is vertical.

The individual elements for the graphical display are defined as internal XML containers within the outermost Layout container. In this example, there is a "TextView" object, where you can set various properties like the width, height or the text to be displayed. The proper way to include things like text strings, or anything else that may be reused, is to store them once as a resource and to refer to this string with a resource identifier. In this example, the identifier is "@string/hello". The actual text string is stored in the file strings.xml, which is located in the directory res/values. The outermost container of this XML file is the tag "resources". Within this, is the list of all of the resources available to your program. In this case, there are only two strings: one for the app name and one for the text content of the main window.

Figure 10. Resources for your program are stored in a series of XML files.

To this example, let's add a button to change the text being displayed. If you need to do any amount of typing, you will want to download and install a more complete soft keyboard or use an external Bluetooth keyboard. A good soft keyboard is the hacker's keyboard. It includes a lot of extra keys that are useful in code editing, including arrow keys to aid navigation and real control, Tab and Escape keys. Most soft keyboards included on Android devices lack these.

Open the main layout file, and below the TextView, add a new tag for a Button entry with the following code:

   android:text="@string/button_title" />

AIDE actually does a continuous code check to make sure there aren't any errors in the code. This means that while you are typing the above additions, you likely will see errors until you finally finish.

The editor includes tab completion, so you can start typing "<Butt" and then press Tab, and the editor will fill out the rest of the word for you. This is because "Button" actually is a special word in Android development.

Once you add the above, you can click the menu button, select More, and then select Refresh Build. This will try to do a full rebuild of your app, and you will see an error about the missing string resource. If you have multiple errors, clicking on the error in the list will bring you to the location where the error appears. To fix this particular error, you need to add the following text to the file res/values/strings.xml:

<string name="button_title">My Button

Figure 11. Any errors that crop up during a rebuild are listed here.


Joey Bernard has a background in both physics and computer science. This serves him well in his day job as a computational research consultant at the University of New Brunswick. He also teaches computational physics and parallel programming.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Join us after the break for

sollen's picture

Join us after the break for an exploration of what makes LED Light Bar Tesla’s latest EV such an amazing ride — and where the company must improve if it truly wants to compete with the BMWs and Mercedes-Benzes of the world.

Reply to comment | Linux Journal

how to Make dubstep's picture

Thank you for the good writeup. It in truth was a
amusement account it. Glance advanced to far added agreeable from you!
By the way, how could we be in contact?

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
On Demand
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot