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 MainActivity.java 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:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" 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.
Practical books for the most technical people on the planet. Newly available books include:
- Agile Product Development by Ted Schmidt
- Improve Business Processes with an Enterprise Job Scheduler by Mike Diehl
- Finding Your Way: Mapping Your Network to Improve Manageability by Bill Childers
- DIY Commerce Site by Reven Lerner
Plus many more.
- Happy GPL Birthday VLC!
- Unikernels, Docker, and Why You Should Care
- Building a Multisourced Infrastructure Using OpenVPN
- diff -u: What's New in Kernel Development
- What's New in 3D Printing, Part III: the Software
- Giving Silos Their Due
- Controversy at the Linux Foundation
- Non-Linux FOSS: Snk
- Don't Burn Your Android Yet
- Server Hardening