Building An Anagram Finder Android Application

A simple application to develop but ideal for learning the basics.

Sylvain Saurel
Jan 1 · 7 min read

I remain convinced that the best way to learn a new technology is through practice. In fact, if you want to get into Android development and have followed the first basic tutorials present on the Android developer site, I think you should create your first application based on a real case.

In this story, I propose to teach you how to create an Android application allowing you to find the anagrams of a sequence of letters entered by the user.

By developing this Android application, you will learn how to create a first graphical interface, to connect it to an activity or to launch processes that do not block the user interface. In short, you’ll learn by practice how to manipulate the concepts at the heart of the majority of Android applications that you download from the Google Play Store.


Anagram Finder Application Specifications

Before detailing the specifications of the Anagram Finder application that you are going to develop through this article, it seems interesting to go back with you on the exact definition of what an anagram is.

To do so, nothing better than starting from the definition given by Wikipedia:

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

The Anagram Finder application will therefore ask the user to enter characters. Once these characters have been entered, the user will click on a “Validate” button that will start the anagram search for these letters.

The Android application will rely on a dictionary of words embedded inside when searching for anagrams.

Once the search is complete, the application will either display the anagrams found or a message to the user that it cannot find any anagrams for the characters entered.


Android Project Configuration

The first step is to create an Android application project in Android Studio. Once this project is created, we will be able to configure its dependencies within the build.gradle file.

In order to facilitate the retrieval in the application activity of the views defined within the UI XML file, I will rely on the Butter Knife code library.

This requires the addition of the following lines within the build.gradle file:

dependencies {
// ...
// we add butter knife dependency
implementation 'com.jakewharton:butterknife:10.2.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
}

For Butter Knife to work properly but also for us to be able to use the Java 8 Lambdas, it is also necessary to add the following configuration lines in the build.gradle file:

android {
// ...
// Java 1.8 for Butter Knife and Lambdas
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}

Using A Word Dictionary

Applications such as the Anagram Finder that we build require a dictionary of words. Indeed, this word dictionary serves as a reference when searching for anagrams.

The Anagram Finder application will use a dictionary of the 10,000 most frequently used words in the English language.

This word dictionary will be directly embedded in the application and placed in the /src/main/assets directory. You can download this word dictionary here: https://www.ssaurel.com/anagram/dict.


Writing The Core Code Of The Application

The core of the application will be located within the Anagram class. This class will have the following responsibilities:

  • Loading words into memory from the word dictionary located in the /src/main/assets directory.
  • Search for anagrams in this word dictionary using an input string.

In the Anagram class, I define a static List object named WORDS that will contain the words loaded from the word dictionary. It is also important that clients of this class can know if the words have been loaded into memory.

In order to know if the words have been loaded in memory, I will define a static LOADED property of boolean type initialized to false.

The loading of words from the word dictionary is done within a loadWords method. Within this method, we will read line by line the content of the dict file contained in the /src/main/assets directory.

Each word read is added to the WORDS list and transformed into uppercase by calling the toUpperCase method. The goal is to simplify comparisons afterwards.

Once all the words have been successfully loaded, I set the LOADED boolean to true indicating that the word dictionary is loaded:

In order to compare two strings and verify that they are anagrams, I will write a sameLetters method that will take two String objects as input.

A clever solution is to transform these String objects into an array of char and then sort the arrays in alphabetical order.

So, all that remains is to check that the contents of the arrays are identical using the equals utility method of the Arrays class :

Using these two methods, the search for anagrams of a word entered by the user will be easy. To do this, I create a listWords method in the Anagram class.

This listWords method takes as input a String containing the words whose anagrams we want to search for. We iterate on the list of words loaded in memory and for each word we call the sameLetters method.

If the word entered into the method has the same letters as the current word in the iteration, we add it to the list of anagrams that we will return at the method output.

All this gives us the following complete code for the Anagram class:


Building The User Interface

The next step will be to build the graphical user interface for the Anagram Finder application. This user interface will consist of the following elements:

  • An EditText to let the user to enter the letters whose anagrams he wants to find.
  • A Button to let the user to launch the anagram search.
  • A TextView placed inside a ScrollView to display the list of anagrams found.

In this application, I limit myself to using a TextView to display the anagrams. For an application whose goal would be to be published on the Google Play Store, I will rather use a RecyclerView component more adapted to this type of display and which would allow the user to interact with the displayed anagrams.

All this gives us the following XML code within the activity_main.xml file:


Writing The Main Activity Code

Within the main activity of the Anagram Finder application, I will first retrieve references to user interface elements. Thanks to Butter Knife, I just have to annotate the properties of my MainActivity class like that:

@BindView(R.id.input)
EditText input;
@BindView(R.id.validate)
Button validate;
@BindView(R.id.result)
TextView result;

Butter Knife makes our code less verbose and makes us more productive since we don’t have to make several findReferenceById method calls.

Within the onCreate method, we call the bind method of the ButterKnife object in order to let Butter Knife finding the references of the views.

We make the “Validate” button disabled since the dictionary words have not yet been loaded into the Anagram class.

Finally, the loading of the words is started by calling a loadWords method within which the loadWords method of the Anagram class is called in a separate Thread in order not to block the user interface:

Once the words are loaded, the “Validate” button is enabled by calling its setEnabled method within the UI Thread.

We will then bind the validate method to the click on the “Validate” button by annotating the method with Butter Knife’s OnClick annotation.

Within the validate method, we first retrieve the word entered by the user. Then, we display a waiting dialog and we launch the search for anagrams within a separate Thread by calling the static method listWords of the Anagram class.

Once the list of anagrams is returned, we close the waiting dialog and display the results on the screen by calling the displayResult method.

The displayResult method simply iterates through the list of anagrams and displays them with a line break between each within the TextView element.

In the event that the Anagram Finder application does not find any anagrams, the message “NO RESULT” is displayed to the user.

All this gives us the following code through the MainActivity class:


Anagram Finder In Action

We have finished writing the code for the Anagram Finder application. Now we are able to put it in action by typing letters whose anagrams we want to find.

Once the application is launched, I enter the following string: “ITNGRALE”. Then I click on the “Validate” button and I finally get the following anagrams:

The Anagram Finder application returns the following anagrams:

  • INTEGRAL
  • RELATING
  • TRIANGLE

Our application is working properly.


To Go Further

To go further, you could let the user choose words starting with identical letters to discover words that are almost anagrams.

You could also choose specialized data dictionaries for animal names, city names, country names, …

From these different dictionaries, you could create an Android game where the user would have to find names of animals, cities or countries from a word that you would have chosen in the dictionary and whose letters you would have shuffled.

This is beyond the scope of the Anagram Finder application, but you can see that now that you know how to manipulate a word dictionary within an Android application, new possibilities are opening up for you.

Finally, if you want to discover this tutorial in video, you can watch the following YouTube video :

The Startup

Medium's largest active publication, followed by +561K people. Follow to join our community.

Sylvain Saurel

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

The Startup

Medium's largest active publication, followed by +561K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade