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.
- The Tiny Internet Project, Part I
- Machine Learning with Python
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Free Today: September Issue of Linux Journal (Retail value: $5.99)
- Bitcoin on Amazon! Sort of...
- Securing the Programmer
- Epiq Solutions' Sidekiq M.2
- Android Browser Security--What You Haven't Been Told
- The Many Paths to a Solution
Pick up any e-commerce web or mobile app today, and you’ll be holding a mashup of interconnected applications and services from a variety of different providers. For instance, when you connect to Amazon’s e-commerce app, cookies, tags and pixels that are monitored by solutions like Exact Target, BazaarVoice, Bing, Shopzilla, Liveramp and Google Tag Manager track every action you take. You’re presented with special offers and coupons based on your viewing and buying patterns. If you find something you want for your birthday, a third party manages your wish list, which you can share through multiple social- media outlets or email to a friend. When you select something to buy, you find yourself presented with similar items as kind suggestions. And when you finally check out, you’re offered the ability to pay with promo codes, gifts cards, PayPal or a variety of credit cards.Get the Guide