Open In App

How to Build a Roman Numeral Convertor in Android Studio?

Last Updated : 08 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Roman Numeral converter is an app through which we can convert a decimal number to its corresponding roman number or a roman number to its corresponding decimal number in the range of 1 to 3999. The user will enter a decimal number and on clicking the convert to roman numeral button, the entered number will convert into its corresponding roman numeral and if the user enters a roman number, on clicking convert to decimal, the entered number will convert into its corresponding decimal number. In this article, we will be Roman Numeral convertors in Android Studio using Kotlin and XML.

Prerequisite:

Step by Step Implementation

Step 1: Create a New Project  

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the build.gradle(Module) File

You need to apply the plugin kotlin-android-extensions in app build.gradle module like this

plugins {

   id 'com.android.application'

   id 'kotlin-android'

   id 'kotlin-android-extensions'

}

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="Roman Numeral Convertor"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="Decimal to Roman Numeral"
        android:textSize="20sp"
        android:textStyle="bold" />
 
    <EditText
        android:id="@+id/decimal_et"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"/>
 
    <Button
        android:id="@+id/convert_to_roman_numeral"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:backgroundTint="#A1F6EE"
        android:text="Convert to roman numeral" />
 
    <TextView
        android:id="@+id/roman_numeral_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="Roman to Decimal Number"
        android:textSize="20sp"
        android:textStyle="bold" />
 
    <EditText
        android:id="@+id/roman_et"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:capitalize="characters" />
 
    <Button
        android:id="@+id/convert_to_decimal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:backgroundTint="#A1F6EE"
        android:text="Convert to decimal " />
 
    <TextView
        android:id="@+id/decimal_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp" />
   
</LinearLayout>


 

 

After writing this much code our UI looks like this:

 

 

Step 4: Working with the MainActivity.kt file

 

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail. 

 

Kotlin




import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // Decimal to Roman convertor
        convert_to_roman_numeral.setOnClickListener {
 
            // check if decimal_et.text is empty or not.
            if (decimal_et.text.isNotEmpty()) {
 
                val number = decimal_et.text.toString().toInt()
 
                if (number <= 3999) {
                    // int_to_Roman function convert a decimal
                    // number to its corresponding Roman number.
                    val roman = decimal_to_Roman(number)
 
                    roman_numeral_tv.text = roman.toString()
 
                } else {
                    Toast.makeText(this, "please enter a number in range between 1 to 3999",Toast.LENGTH_SHORT).show()
                }
            } else {
                Toast.makeText(this, "please enter a number ", Toast.LENGTH_SHORT).show()
            }
 
        }
 
 
        // Roman to Decimal convertor.
        convert_to_decimal.setOnClickListener {
            // check if roman_et.text is empty or not.
            if (roman_et.text.isNotEmpty()) {
                val roman = roman_et.text.toString()
                // RomanToDecimal function convert a Roman number
                // to its corresponding decimal number.
                decimal_tv.text = RomanToDecimal(roman).toString()
            } else {
                Toast.makeText(this, "please enter a Roman Numeral ", Toast.LENGTH_SHORT).show()
            }
        }
    }
 
    private fun decimal_to_Roman(num: Int): Any {
 
        val m = arrayOf("", "M", "MM", "MMM")
        val c = arrayOf(
            "", "C", "CC", "CCC", "CD", "D",
            "DC", "DCC", "DCCC", "CM"
        )
        val x = arrayOf(
            "", "X", "XX", "XXX", "XL", "L",
            "LX", "LXX", "LXXX", "XC"
        )
        val i = arrayOf(
            "", "I", "II", "III", "IV", "V",
            "VI", "VII", "VIII", "IX"
        )
 
        // Converting to roman
        val thousands = m[num / 1000]
        val hundreds = c[num % 1000 / 100]
        val tens = x[num % 100 / 10]
        val ones = i[num % 10]
 
        return thousands + hundreds + tens + ones
    }
 
    fun value(r: Char): Int {
        if (r == 'I') return 1
        if (r == 'V') return 5
        if (r == 'X') return 10
        if (r == 'L') return 50
        if (r == 'C') return 100
        if (r == 'D') return 500
        return if (r == 'M') 1000 else -1
    }
 
    // Finds decimal value of a
    // given roman numeral
    fun RomanToDecimal(str: String): Int {
        // Initialize result
        var res = 0
        var i = 0
        while (i < str.length) {
 
            // Getting value of symbol s[i]
            val s1 = value(str[i])
 
            // Getting value of symbol s[i+1]
            if (i + 1 < str.length) {
                val s2 = value(str[i + 1])
 
                // Comparing both values
                if (s1 >= s2) {
                    // Value of current symbol
                    // is greater or equalto
                    // the next symbol
                    res = res + s1
                } else {
                    // Value of current symbol is
                    // less than the next symbol
                    res = res + s2 - s1
                    i++
                }
            } else {
                res = res + s1
            }
            i++
        }
        return res
    }
}


 

 

Output:

 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads