ActionBar / Adapter / Animation / ArrayAdapter / SharedPreferences / Text

Dirty Phrasebook – Part 5

On 1st April 2015 I published a joke app to Google Play named Dirty Phrasebook which is based upon Monty Python’s Dirty Hungarian Phrasebook sketch. I this series of articles we’ll take a look in to the code (which will be open-sourced along with the final article). In this article we’ll continue looking at the UI logic.

ic_launcherNow we’ll turn our attention to our MainActivity which hooks up the translation engine, layouts, and InputView that we’ve already looked at. Let’s start with the onCreate() method which is the logical starting point for any Activity:

As with InputView there’s quite a lot going on here. Firstly I elected to include Crashlytics reporting which will notify me of any crashes which occur out in the field. I would consider some kind of crash reporting virtually essential for any app being published to Google Play or another app store. I chose Crashlytics for ease of integration, and because I already have an account, but other crash reporting solutions are available.

Next we get a handle to a SharedPreferences instance which will be used to maintain the app state even if the app is killed and restarted. We then get an instance of our Translator, and load the list of readable language strings which we’ll use to populate the language selection Spinner.

Next we have the findViews() method which gets references to the Views in the layout:

Then we have the setupToolbar() method which configures the ActionBar to use the Toolbar which we defined in the layout:

After that we have setupLanguageSpinner() which is responsible for creating the Adapter for our language selection Spinner:

onItemSelected() gets called when a new language is selected. This sets the language on the translation engine, updates the language for TextToSpeech (more on this later), updates the translation text to change the translation to the new language, and saves the language selection in SharedPreferences to store the app state.

Next setupCopy() gets called which sets up a long press handler on the translation TextView which will copy the translation text to the clipboard:

Next restoreAppState() gets called which will restore the app state from SharedPreferences:

The next thing that we do is register MainActivity as an InputListener with InputView – we covered this in the previous article – which ensures that MainActivity receives callbacks when the input mode state changes:

The inputChanged() method will cause the translation to be shown or hidden depending on whether or not the input text contains any text. Whenever the input text changes we save it to SharedPreferences so that we can maintain the app state.

The startInputMode() and endInputMode() methods enable MainActivity to create any necessary animations as input mode is entered and exited. One thing worthy of mention is the OnClickListener which gets set and unset on container. The purpose of this is to enable edit mode to be exited if the user clicks outside of InputView whilst in input mode. Once again I’m indebted to Benoit Duffez for this suggestion.

The updateTranslation() method gets called whenever input mode is ended and the input is not empty. This calls the translation engine to perform the actual ‘translation’ and update the translation text.

Next we set up the Adapter for the language selection Spinner and onItemSelected() gets called when a new language is selected. This sets the language on the translation engine, updates the language for TextToSpeech (more on this later), updates the translation text to change the translation to the new language, and saves the language selection in SharedPreferences to store the app state.

The final thing that happens in onCreate() is the setup of the TextToSpeech engine. we’ll cover the actual implementation of this in the next article, but here we need to handle the engine initialisation and also add the handlers for the user clicking on the TextToSpeach playback link, and also change the visibility of this depending on the availability of the target language in the TextToSpeech engine:

We also have yet another method of the user ending input mode by clicking the back button:

Finally we have our onDestroy() method which unregisters MainActivity as an InputListener with InputView, and shuts down the TextToSpeech engine:

We’re almost there! All that remains is the TextToSpeech implementation which we’ll cover in the final article in this series.

The source code for this series is available here.

I am deeply indebted to my fantastic team of volunteer translators who generously gave their time and language skills to make this project sooo much better. They are Sebastiano Poggi (Italian), Zvonko Grujić (Croatian), Conor O’Donnell (Gaelic), Stefan Hoth (German), Hans Petter Eide (Norwegian), Wiebe Elsinga (Dutch), Imanol Pérez Iriarte (Spanish), Adam Graves (Malay), Teo Ramone (Greek), Mattias Isegran Bergander (Swedish), Morten Grouleff (Danish), George Medve (Hungarian), Anup Cowkur (Hindi), Draško Sarić (Serbian), Polson Keeratibumrungpong (Thai), Benoit Duffez (French), and Vasily Sochinsky (Russian).

© 2015, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Dirty Phrasebook – Part 5 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

Leave a Reply

Your email address will not be published. Required fields are marked *