Open In App

How to Create a Morse Code Converter Android App?

Last Updated : 26 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 alphabet 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. So in this article lets create a Morse Code Converter Android App using Java/Kotlin 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.

Steps to Implement Morse Code Convertor

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.

Step 2: Working with activity_main.xml file

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.

activity_main.xml:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:padding="25dp"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/inputEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
        android:hint="Enter the text to be encoded"
        android:gravity="start" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/encode"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Encode"
            android:textColor="#fff"
            android:textSize="16sp"/>

        <Button
            android:id="@+id/clear"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:text="Clear"
            android:textSize="16sp" />

        <Button
            android:id="@+id/decode"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Decode"
            android:textColor="#fff"
            android:textSize="16sp" />
    </LinearLayout>

    <EditText
        android:id="@+id/outputEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
        android:layout_marginTop="10dp"
        android:hint="Output"
        android:gravity="start" />

</LinearLayout>


Design UI:

morse-code-app


Step 4: Working with MainActivity file

In this file we will be fetching all UI elements using findViewById(). Then, create a map that corresponds every letter and number to their morse codes. Now, create a reverse map which maps all the morse codes to their corresponding letters and numbers.

Create three functions for encode, decode and clearing all fields. In the encode and decode function we will mapping the user input letter by letter and display the output to their corresponding morse codes and vice versa.

MainActivity File:

MainActivity.java
package org.geeksforgeeks.demo;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private EditText inputEditText, outputEditText;
    private Button encodeButton, decodeButton, clearButton;

    // Map letters and numbers to Morse code
    private final Map<Character, String> morseMap = new HashMap<>();
    private final Map<String, Character> reverseMorseMap = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        inputEditText = findViewById(R.id.inputEditText);
        outputEditText = findViewById(R.id.outputEditText);
        encodeButton = findViewById(R.id.encode);
        decodeButton = findViewById(R.id.decode);
        clearButton = findViewById(R.id.clear);

        initializeMorseCodeMaps();

        encodeButton.setOnClickListener(v -> encodeText());
        decodeButton.setOnClickListener(v -> decodeText());
        clearButton.setOnClickListener(v -> clearText());
    }

    private void initializeMorseCodeMaps() {
        // Populate Morse Code Map
        String[] letters = {
                ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
                "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
                "..-", "...-", ".--", "-..-", "-.--", "--.."
        };
        String[] numbers = {
                "-----", ".----", "..---", "...--", "....-", ".....",
                "-....", "--...", "---..", "----."
        };

        // Add Letters
        for (int i = 0; i < 26; i++) {
            char letter = (char) ('A' + i);
            morseMap.put(letter, letters[i]);
            reverseMorseMap.put(letters[i], letter);
        }

        // Add Numbers
        for (int i = 0; i < 10; i++) {
            char number = (char) ('0' + i);
            morseMap.put(number, numbers[i]);
            reverseMorseMap.put(numbers[i], number);
        }

        // Add Space as "/"
        morseMap.put(' ', "/");
        reverseMorseMap.put("/", ' ');
    }

    // Convert input text to Morse code
    private void encodeText() {
        String inputText = inputEditText.getText().toString().toUpperCase();
        StringBuilder encodedText = new StringBuilder();

        for (char c : inputText.toCharArray()) {
            if (morseMap.containsKey(c)) {
                encodedText.append(morseMap.get(c)).append(" ");
            }
        }

        outputEditText.setText(encodedText.toString().trim());
    }

    // Convert Morse code back to normal text
    private void decodeText() {
        String inputText = inputEditText.getText().toString().trim();
        StringBuilder decodedText = new StringBuilder();

        for (String morse : inputText.split(" ")) {
            if (reverseMorseMap.containsKey(morse)) {
                decodedText.append(reverseMorseMap.get(morse));
            }
        }

        outputEditText.setText(decodedText.toString());
    }

    // Clear both text fields
    private void clearText() {
        inputEditText.setText("");
        outputEditText.setText("");
    }
}
MainActivity.kt
package org.geeksforgeeks.demo

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private lateinit var inputEditText: EditText
    private lateinit var outputEditText: EditText
    private lateinit var encodeButton: Button
    private lateinit var decodeButton: Button
    private lateinit var clearButton: Button

    // map letters and numbers to morse codes
    private val map = mapOf(
        'A' to ".-",    'B' to "-...",  'C' to "-.-.",  'D' to "-..",   'E' to ".",
        'F' to "..-.",  'G' to "--.",   'H' to "....",  'I' to "..",    'J' to ".---",
        'K' to "-.-",   'L' to ".-..",  'M' to "--",    'N' to "-.",    'O' to "---",
        'P' to ".--.",  'Q' to "--.-",  'R' to ".-.",   'S' to "...",   'T' to "-",
        'U' to "..-",   'V' to "...-",  'W' to ".--",   'X' to "-..-",  'Y' to "-.--",
        'Z' to "--..",  '0' to "-----", '1' to ".----", '2' to "..---", '3' to "...--",
        '4' to "....-", '5' to ".....", '6' to "-....", '7' to "--...", '8' to "---..",
        '9' to "----.", ' ' to "/"
    )

    // reverse map for decoding
    private val reverseMap = map.entries.associate { (key, value) ->
        value to key
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        inputEditText = findViewById(R.id.inputEditText)
        outputEditText = findViewById(R.id.outputEditText)
        encodeButton = findViewById(R.id.encode)
        decodeButton = findViewById(R.id.decode)
        clearButton = findViewById(R.id.clear)

        encodeButton.setOnClickListener {
            encodeText()
        }
        decodeButton.setOnClickListener {
            decodeText()
        }
        clearButton.setOnClickListener {
            clearText()
        }
    }

    // converts the input text into Morse code
    private fun encodeText() {
    
        // get user input and convert to uppercase for consistency
        val inputText = inputEditText.text.toString().uppercase()

        // map letter by letter and convert to morse code
        val encodedText = inputText.mapNotNull {
            map[it]
        }.joinToString(" ")

        // display output
        outputEditText.setText(encodedText)
    }

    // decode morse code to normal text
    private fun decodeText() {
    
        // get user input and remove extra spaces
        val inputText = inputEditText.text.toString().trim()

        // split Morse code by spaces (each Morse letter is separated by a space)
        val decodedText = inputText.split(" ")
            
            // convert Morse code back to letters
            .mapNotNull {
                reverseMap[it]
            }.joinToString("")

        // display output
        outputEditText.setText(decodedText)
    }

    // clear all fields
    private fun clearText() {
        inputEditText.text.clear()
        outputEditText.text.clear()
    }
}

Refer to the following repo to get the entire code: Morse_Code_Convertor_Android

Output:



Next Article
Practice Tags :

Similar Reads