SimpleInterest calculator in Android is developed to calculate the simple Interest in Android apps.
Android Studio Chipmunk is used to develop this project.
Simple interest formula is p*r*t/100
activity_main.xml
Created a TextView to show messages at the top.
Create a Number (decimal) field for the Principal amount and rate.
Create a Number field for Time.
Create a button and set its text to Calculate.
Create a TextView to show simple interest.
Code layout is as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F4E7E7" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="331dp" android:layout_height="42dp" android:layout_marginTop="40dp" android:background="#3F51B5" android:backgroundTint="#3F51B5" android:text="Simple Intrest Calculator" android:textAlignment="center" android:textColor="#F4F4F7" android:textSize="24sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="214dp" android:layout_height="65dp" android:layout_marginTop="284dp" android:text="Calculate" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.492" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="52dp" android:text="TextView" android:textSize="24sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button" /> <EditText android:id="@+id/editTextNumberDecimal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="112dp" android:ems="10" android:hint="Principal Amount" android:inputType="numberDecimal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.492" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editTextNumberDecimal2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="168dp" android:ems="10" android:hint="Rate" android:inputType="numberDecimal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editTextNumber4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="224dp" android:ems="10" android:hint="Time (in months)" android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
MainActivity.java
create Textboxes and get its values using findViewById()
.
After getting values in the specified variable set the onClickListenert button.
the onclick listener will call calculateSimpleInterest()
this method calculates the simple interest.
result.setAlpha(0.0f)
is used to hide the result TextView at the start of the program.
At the method calculateSimpleIntrest() we set result.setAlpha(1.0f)
to show the result TextView.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package com.ebhor.simpleinterest; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.text.DecimalFormat; public class MainActivity extends AppCompatActivity { EditText principalEditText; EditText rateEditText; EditText timeEditText; Button calculateSI; TextView result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); principalEditText = (EditText) findViewById(R.id.editTextNumberDecimal); rateEditText = (EditText) findViewById(R.id.editTextNumberDecimal2); timeEditText = (EditText) findViewById(R.id.editTextNumber4); calculateSI = (Button) findViewById(R.id.button); result = (TextView) findViewById(R.id.textView2); result.setAlpha(0.0f); calculateSI.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { calculateSimpleInterest(); } }); } public void calculateSimpleInterest() { Double principal = Double.parseDouble(principalEditText.getText().toString()); Double rate = Double.parseDouble(rateEditText.getText().toString()); Double time = Double.parseDouble(timeEditText.getText().toString()); Double simpleInterest = principal * rate * time / 100; result.setText("Simple Interest: " + simpleInterest.toString()); result.setAlpha(1.0f); } } |
Adding Validation in form
Here we have added saripaar validation library.
To add this library open build.gradle and under dependencies add implementation 'com.mobsandgeeks:android-saripaar:2.0.3'
or any updated version of library.
MainActivity must implement Validator.ValidationListener
and override two methods public void onValidationFailed(List errors)
and public void onValidationSucceeded()
.
Working code is shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | package com.ebhor.simpleinterest; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.mobsandgeeks.saripaar.ValidationError; import com.mobsandgeeks.saripaar.Validator; import com.mobsandgeeks.saripaar.annotation.NotEmpty; import java.text.DecimalFormat; import java.util.List; public class MainActivity extends AppCompatActivity implements Validator.ValidationListener { @NotEmpty(message = "Principal Amount Required") EditText principalEditText; @NotEmpty(message = "Rate Required") EditText rateEditText; @NotEmpty(message = "Time Required") EditText timeEditText; Button calculateSI; TextView result; Validator validator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); principalEditText = (EditText) findViewById(R.id.editTextNumberDecimal); rateEditText = (EditText) findViewById(R.id.editTextNumberDecimal2); timeEditText = (EditText) findViewById(R.id.editTextNumber4); calculateSI = (Button) findViewById(R.id.button); result = (TextView) findViewById(R.id.textView2); result.setAlpha(0.0f); validator = new Validator(this); validator.setValidationListener(this); calculateSI.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { { validator.validate(); } } }); } @Override public void onValidationSucceeded() { calculateSimpleInterest(); } @Override public void onValidationFailed(List<ValidationError> errors) { for (ValidationError error : errors) { View view = error.getView(); String message = error.getCollatedErrorMessage(this); // Display error messages ;) if (view instanceof EditText) { ((EditText) view).setError(message); } else { Toast.makeText(this, message, Toast.LENGTH_LONG).show(); } } } public void calculateSimpleInterest() { Double principal = Double.parseDouble(principalEditText.getText().toString()); Double rate = Double.parseDouble(rateEditText.getText().toString()); Double time = Double.parseDouble(timeEditText.getText().toString()); Double simpleInterest = principal * rate * time / 100; result.setText("Simple Interest: " + simpleInterest.toString()); result.setAlpha(1.0f); } } |
So here we have implemented Simple Interest Calculator in Android using validation.