Create a Morse code Converter Application for Android

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.

Image for post
Image for post

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 :

Image for post
Image for post

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 :

Image for post
Image for post

Don’t hesitate to give me your feedback on this tutorial. You can also discover the final application published on the Google Play Store:

To discover more tutorials on Android Development, don’t hesitate to visit the SSaurel’s blog : https://www.ssaurel.com/blog/

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store