Programmatically integrate third-party code into your Android UI Grabbing third-party code from GitHub or another repository might make you feel like a kid in a candy store, but it takes some finesse to integrate that code with your Android UI. This month, Andrew Glover shows you how to take the Overheard Word demo app to the next level with a JSON-based word engine and some pre-baked swipe gesture functionality. Android, it turns out, can accommodate third-party code with ease, but you still have to implement some careful logic if you want your app's UI to run with it. If you've read and followed the demonstrations in this series so far, you've built up some basic Android development skills. In addition to setting up an Android development environment and writing your first Hello World application, you've learned how to replace button taps with swipe gestures and implement menus (or toolbars) and icons in a custom mobile app. In this article, you continue along this trajectory and learn how to use third-party libraries to increase or enhance the functionality of your application. First, we'll install some open source libraries and read documentation, and then we'll programmatically integrate new functionality with the UI of a demo application. As I did in previous articles, I'll demonstrate using my Overheard Word application. If you haven't already cloned the Overheard Word GitHub repository, you should do so first so that you can follow the steps that follow. About this series Mobile app releases are exploding, and mobile development technology is here to stay. This series of articles will introduce developers who have programming experience but are new to the mobile space to what's happening in this space. This series starts with writing native apps in Java code, then expands your toolbox to include JVM languages, scripting frameworks, HTML5/CSS/JavaScript, third-party tools, and more. You'll gradually gain the skills you need to meet the needs of almost any mobile development scenario. Thingamajig: A pluggable word engine Overheard Word is an English language app that helps users learn new words and build up their vocabulary on the fly. In previous articles, we first developed a basic app, then added swipe gestures for easier navigation and icons for a prettier UI. So far, so good, but this app won't go very far because it's missing a certain ingredient: Overheard Word needs some words! To make Overheard Word a true multi-word app, I've built a small word engine, Thingamajig, that encapsulates the concept of a word and its corresponding definition. Thingamajig handles creating words and their definitions via JSON documents, and has absolutely no Android dependencies. Like the Overheard Word app, my word engine is hosted on GitHub. You can clone the repository, or download the source code and run You will get a lightweight 8KB JAR file that you can copy to your At the time, the code in my word engine was initialized with a JSON document instance. The JSON document could be a file residing in a file system on a device, a response to an HTTP request, or even a response to a database query — it doesn't really matter for now. What matters is that you have a useful library that lets you work with Third-party libraries in Android Integrating third-party libraries into an Android project is easy; in fact, every Android starter project includes a special directory Environmental constraints While you can imagine how to add any third-party libraries you like to your application, never forget the limitations of the mobile environment. The device running your application is not a reliable web server after all! Users will thank you for improving data processing capabilities and minimizing the download size of third-party add-ons. In addition to plugging the word engine library into my application, I'm also going to add another third-party application called Gesticulate. Just like with the word engine library, you can get Gesticulate by cloning or downloading its source code from GitHub. Then run Update UI Now we get to the meat of this article, updating the UI to integrate all that third-party code you just snagged for free. Fortunately, I had planned ahead for this moment with my Overheard Word application. Back when I wrote the first iteration of the application, I defined a simple layout that included a word, its part of speech, and a definition, as shown in Figure 1: Figure 1. Overheard Word's default view The layout is defined with placeholder text, which I'm going to replace with actual words fetched from my pluggable word engine. So, I'll initialize an array of words, grab one of them, and use its value to update the UI accordingly. In order to update the UI, I must be able to get a handle to each view element and provide values for those elements. For example, a displayed word (such as Pedestrian ) is defined as a Listing 1. TextView defined in a layout
If you look closely, you'll see that I've set the text to " Before I can proceed, I need to build a word list. You may remember that my word engine can create Using files: res and raw directories By default, an Android application has permission to read the device's underlying file system, but you can only access the local file system underneath the application itself. Therefore, you can include files in your application and reference them accordingly. (This means that you can read and write files that are local to your application ; writing to the SD card, etc., a file system outside of your application requires special permissions.) Because my word engine can take a JSON document to initialize the word list, there is nothing to stop me from including a JSON document that the application will read at runtime. Just like I demonstrated with the icon assets in my previous article, you can store files in the I create a Figure 2. The raw directory containing new words Next, Eclipse rebuilds the project, and my Figure 3. Updated R file Once I have a handle to a file, I can open it, read it, and ultimately build a JSON document as the basis for generating a word list. Building a word list When the application starts, I start a series of steps to load the original JSON document and build a word list. I will create a method called Listing 2. Reading the contents of a file in Android
You should notice a few things that happen in the The JSON format for a word is shown in Listing 3: Listing 3. JSON representation of a word
My Listing 4. Initializing WordStudyEngine
Once I have an initialized engine instance, I can ask it for a word (randomized automatically) and then update the three elements of the UI accordingly. For example, I can update the word portion of the definition, as shown in Listing 5: Listing 5. Updating UI elements programmatically
In Listing 6, I follow essentially the same process to update the definition and part-of-speech elements of the application's UI: Listing 6. More programmatic updates
Notice in Listings 5 and 6 how the layout elements are referenced by name using the Now that I have updated these UI elements, I can launch my application and check the results. Voila! I am now learning a legal word! Figure 4. Overheard Word There is a word! Add gestures: connect swipes to words Now that I can effectively display a word, I want to give the user the ability to quickly swipe through all the words in my word engine. To make this easier, I'm going to use Gesticulate, my own third-party library that calculates swipe speed and direction. I've also put the swipe logic into a method called After initiating the swipe gesture, the first step is to move the logic for displaying a word into a new method that I can call to display a new word when someone swipes. The updated Listing 7. onCreate displays a word when the gesture is initiated
Notice the Next, I go into the Listing 8. initGestureDetector displays a word when swiping left
My word engine is represented by the Slide back and forth Right now, when I open the app and start swiping, every time I swipe left, I see a new word and definition appear. However, when I swipe in other directions, I just get a tiny message saying I've swiped. Wouldn't it be better if I could go back to the previous word by swiping right? Backtracking doesn't seem like it should be difficult. Maybe I could use a stack and just pop the last element that was put there by the previous left swipe? However, this idea doesn't hold up when the user swipes left again after backtracking. If the last position was popped, a new word would be displayed instead of the word the user previously saw. After thinking it over, I'm inclined to try a linked list approach that gets rid of previously viewed words as the user swipes back and forth. I'll start by creating a Listing 9. New member variables
In Listing 10, I initialize Listing 10. Initializing LinkedList
Now, when the first word is displayed, I need to add it to Listing 11. Don't forget to increment the view position
Sliding Logic Now, I get to the main part of the logic, which requires some thinking. When the user swipes left, I want to display the next word, and when he or she swipes right, I want to display the previous word. If the user swipes right again, then the second previous word should be displayed, and so on. This should continue until the user gets back to the first displayed word. Then, if the user starts swiping left again, the list of words should appear in the same order as before. The first part is a little tricky because the default implementation of my In Listing 12, I feel like I have figured out the logic for what I want to do. The first Boolean assignment is encapsulated in a method called If Listing 12. LinkedList for scrolling back and forth
Note that if Once you've mastered the left swipe, handling the right swipe logic is fairly easy. Essentially, if there is a word in the Try it: Start an instance of Overheard Word and swipe back and forth to learn some words. Each time you swipe, the UI will be updated accordingly. Conclusion Overheard Word has been working pretty well, and now we've started to add some more interesting features on top of the basic Android shell. Of course, there's more to do, but we now have a functioning application that handles scrolling, has icons and menus, and can even read from a custom third-party word file. Next month, I'll show you how to add more styles to Overheard Word, and then implement a flexible quiz feature. |
<<: Activities and icons in the Android application life cycle
>>: Mobile technology for the masses: signing, packaging, and distributing Android apps
Fan Deng: Decoding the advanced code of spiritual...
[[144037]] Recently, I have been in contact with ...
"Give a woman a pair of high heels, and she ...
The opening rate and reading volume of WeChat pub...
For a hundred years, there has been a famous &quo...
For every designer Posters are the most common an...
[[132525]] As Internet users around the world swi...
This is an era where idols are born faster than b...
Recently, Wang Chaoyang, a member of the U.S. Nat...
When it comes to charts that are often used in da...
On March 10, local time, WTO Director-General Aze...
The National Enterprise Credit Information Public...
There are usually two reasons why menstruation ap...
“No migratory bird flies in a straight line” This...
Starting a business requires costs, and mini prog...