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.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Profiles and RC Files
- Maru OS Brings Debian to Your Phone
- OpenSwitch Finds a New Home
- Understanding Ceph and Its Place in the Market
- Astronomy for KDE
- Git 2.9 Released
- Snappy Moves to New Platforms
- SoftMaker FreeOffice
- What's Our Next Fight?
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide