How to Create a Morse Code Converter Android App?
Morse code is a way in which one can encode characters and text using dots and dashes. The international Morse codes contain 26 letters of the alphabets and some non-English letters that are Arabic numerals as well as some punctuation. In Morse code, there is no difference between the upper and lower case letters. The transmission of the Morse Code is measured in dot durations. The Morse Code Converter App is an app that is used to convert the given statements to Morse Code. This is done by using Android Studio. The Languages that are used are Java and XML.
So in this article lets create a Morse Code Converter Android App using the Java language. This project also involves the conversion of the Morse Code into relevant Statements. It means both encoding and decoding can be done using this Android app.
Software Tools Required
The software tools required in this project are:
- ANDROID-STUDIO IDE (1.0.2)
- SDK having API level-21(minimum version)
- JAVA 7 and above
- An android smartphone – version 4.2.2(Jelly bean and above) (only for testing the software)
Step 1: Create a New Project
To create a new project in Android Studio please refer How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Create a Round Button
This step is optional (only if you want a black button with round corners). In the project tab, in the left-hand corner of the screen, click on the app folder. Then, click on the res folder. Then, right-click on the drawable folder and select New then select Drawable Resource file. Give this resource file a name. Remember one can use only lower case letters for the name of the resource file. Write the code below to create a resource file for black buttons with round corners. Use this resource file as background in the XML code of buttons to get black buttons with round corners.
In case one wants to use the default button or some other button then skip this optional step and don’t include the resource file in the background of the buttons.
Step 3: Working with activity_main.xml file
- Open the activity_main.xml file and start writing the xml code.
- Create 2 EditTexts in the xml file. One for writing the input and the other one for displaying the output. EditTexts are used because on clicking on the EditTexts the text can be selected and copied which will be very useful in sending the coded/uncoded message some other person using any messaging app.
- Make 3 Buttons: Encode, Decode and Clear.
- The Encode Button will take the input as text from the input EditText and then encode it into morse code and display it in the output EditText.
- The Decode Button will take the morse code as input from the input EditText and then decode it into alphabets and numbers and display it in the output EditText.
- The Clear Button will clear the input and output EditTexts.
- Give id’s to all the EditTexts and Buttons.
The complete xml code is given below:
Step 4: Working with MainActivity.java file
- Open the MainActivity.java file.
- Initialize the variables just under the MainActivity class (Syntax:- Edit Text etinput, etoutput;Button btnEncode, btnDecode, btnclear;)
- Assign the variables under the OnCreate method.
- Make an array and store all the alphabets and digits in it.
- Make another array and store the morse code of each alphahbets and digits in the indices corresponding to it’s alphabets and digits in the first array.
- Use the logic in the code below to complete the morse code converter app.
Below is the complete code for MainActivity.java file.
Output: Run on Emulator
Github link: Click here