Open In App

How to Use getCurrencyInstance Method in Android?

Last Updated : 30 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In Android, when we’ve to format the numbers of any TextView, we use “NumberFormat” class that is the base class of all formatting methods. So, different formatting methods are used for the different types of formatting. In this way, when we’ve to specify the numbers for a particular locale, getInstance method is used. For ex: getNumberInstance() is used to find an integer number format, getPercentInstance() is used for showing the number is percentage, like 0.53 to 53% and getCurrencyInstance to get currency format. About which we’re going to discuss here.

What we are going to build in this article? 

We’re going to build an application in which we get some number as input and show it with the currency symbol using getcurrencyInstance method. Components of application.

  1. EditText to take input
  2. TextView to suggest
  3. Button, when clicked shows the result
  4. Another TextView to show result

Let’s see the coding implementation below.

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 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"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200"
    android:orientation="vertical"
    tools:context=".MainActivity">
      
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:autofillHints="@string/enter_an_amount"
        android:hint="@string/enter_an_amount"
        android:textColor="@color/black"
        android:textSize="30sp"
        android:textStyle="bold" />
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:text="@string/amount"
        android:textColor="@color/black"
        android:textSize="30sp"
        android:textStyle="bold" />
  
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:onClick="showValue"
        android:text="@string/show_value" />
  
    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:textColor="@color/black"
        android:textSize="30sp"
        android:textStyle="bold" />
  
</androidx.appcompat.widget.LinearLayoutCompat>


Step 3: Working with the strings.xml file

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

XML




<resources>
    <string name="app_name">Geeksforgeeks Currency adder</string>
    <string name="amount">Amount you entered with currency sign is</string>
    <string name="show_value">Show Value</string>
    <string name="enter_an_amount">Enter an amount</string>
</resources>


Step 4: Working with the MainActivity.java 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.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
  
    fun showValue(view: View) {
        // variable editText to get the id of editText
        val editText: EditText = findViewById(R.id.editText)
        // we've id in editText variable, convert it into Int
        val value: String = editText.text.toString()
        // get the currency of local system
        val currency: Currency = Currency.getInstance(Locale.getDefault())
        // store the currency sign in symbol variable
        val symbol: String = currency.symbol
        val message = "$symbol$value"
        // now again find the id of output textView
        val textView: TextView = findViewById(R.id.output)
        // set the message value in it with sign
        // first way
        textView.text = message
        // second way
        // message.also { textView.text = it }
        // following line tells us how we can add any specific currency
        // val newCurrency: Currency = Currency.getInstance("EUR")
        // and everything will be same
    }
  
}


And here’s what we get.

Output:

Output

Application output window



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

Similar Reads