Like you must know, the Morse code is a method of transmitting text informations as a series of on-off tones, lights, or clicks which can be directly understood by a skilled listener or observer without special equipment. It is named for Samuel F. N. Morse, an inventor of the telegraph.
In this tutorial, you are going to discover how to create a Morse code Converter Application for Android. Note that you can enjoy this tutorial in video on YouTube too :
The first step of the tutorial is to create a MorseCode class which will be used to convert an english text to Morse code and to make the reverse operation : converting a Morce code text to english.
We use 2 arrays to define the english and Morse equivalent codes. To make the access easier, we are going to use 2 HashMaps :
- One to store the association between Alpha caracters to Morse
- An other to store the association between Morse codes to Alpha
Then, we create a morseToAlpha method to convert a text in Morse code. We take in parameter a sentence in Morse code. We split the sentences to get each words of the sentence. We iterate on each word and we cut it to get each letter. For each letter, we get the associated alpha text. We create a new sentence with the converted text and we return it.
We create an alphaToMorse method to make the reverse operation : convert a sentence in english to Morse code.
The MorseCode object has the following form :
Now, we can create the User Interface of our Morse code Converter Application. The User Interface will have the following views :
- 1 EditText to enter the sencente to convert
- 2 Buttons to convert text to Alpha and to Morse
- 1 LinearLayout to embed these 2 Buttons
- 1 TextView to display the converted text
The code for the main layout will have the following form :
You can see a preview of our User Interface just below :
Last part of the tutorial is to write the Java code of the Main Activity to connect the User Interface with the methods offered by the MorseCode object.
When a user will click on one of the Buttons, we will get the text to convert. Then, we will apply the expected conversion and we will display the result on the TextView. This is gives us the following code for the MainActivity :
Now, we can try our Morse code Converter Application. You have just to enter a text to convert and to click on the Button of your choice to enjoy the result. In the following example, we have entered the text “Hello from the SSaurel’s Channel” and we have clicked on the Button to convert the text in Morse code :
Don’t hesitate to give me your feedback on this tutorial. You can also discover the final application published on the Google Play Store:
Morse Converter - Apps on Google Play
Convert your texts to Morse and vice-versa with Morse Converter. Share your texts converted in Morse with your friends…
To discover more tutorials on Android Development, don’t hesitate to visit the SSaurel’s blog : https://www.ssaurel.com/blog/