Simple & Compound Interest Calculator Android App with Menu Options
In Android, if you want to show an ActionBar that displays the app title and menu options on top of the screen, you need to set the appropriate theme for your activity. You can either set this theme globally for your entire app or apply it only to a specific activity using your AndroidManifest.xml
file.
The recommended theme to show an ActionBar is:
@style/Theme.AppCompat.Light.DarkActionBar
Below, we’ll set up everything step-by-step to create an app for calculating Simple Interest and Compound Interest with a clean UI and menu options.
Set the ActionBar theme for only CalculationActivity
with a custom title.
<activity
android:name=".CalculationActivity"
android:exported="true"
android:label="Interest Calculator"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/simple_interest"
android:title="Simple Interest" />
<item
android:id="@+id/compound_interest"
android:title="Compound Interest" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="12dp" />
<stroke android:width="1dp" android:color="#CCCCCC" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#E0F7FA" />
<corners android:radius="12dp" />
<stroke android:width="1dp" android:color="#00ACC1" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0F4F8"
android:gravity="center"
android:orientation="vertical"
android:padding="24dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="24dp"
android:text="Interest Calculator"
android:textColor="#333333"
android:textSize="26sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editPrincipal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:background="@drawable/rounded_edittext"
android:hint="Principal Amount"
android:inputType="numberDecimal"
android:padding="14dp" />
<EditText
android:id="@+id/editRate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:background="@drawable/rounded_edittext"
android:hint="Rate of Interest"
android:inputType="numberDecimal"
android:padding="14dp" />
<EditText
android:id="@+id/editYears"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:background="@drawable/rounded_edittext"
android:hint="Number of Years"
android:inputType="numberDecimal"
android:padding="14dp" />
<TextView
android:id="@+id/textResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_result"
android:gravity="center"
android:padding="18dp"
android:text="Result will appear here"
android:textColor="#222222"
android:textSize="18sp" />
</LinearLayout>
package com.tecvipul.layoutmenucolor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class CalculationActivity extends AppCompatActivity {
EditText editPrincipal, editRate, editYears;
TextView textResult, title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculation);
editPrincipal = findViewById(R.id.editPrincipal);
editRate = findViewById(R.id.editRate);
editYears = findViewById(R.id.editYears);
textResult = findViewById(R.id.textResult);
title = findViewById(R.id.title);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.calculation_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
double principal, rate, year;
try {
principal = Double.parseDouble(editPrincipal.getText().toString());
rate = Double.parseDouble(editRate.getText().toString());
year = Double.parseDouble(editYears.getText().toString());
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Please enter all values correctly!", Toast.LENGTH_SHORT).show();
return false;
}
switch (item.getItemId()) {
case R.id.simple_interest:
double simpleInterest = (principal * rate * year) / 100;
textResult.setText(String.format("Simple Interest = %.2f", simpleInterest));
title.setText("Simple Interest");
return true;
case R.id.compound_interest:
double compoundInterest = principal * Math.pow((1 + rate / 100), year) - principal;
textResult.setText(String.format("Compound Interest = %.2f", compoundInterest));
title.setText("Compound Interest");
return true;
}
return super.onOptionsItemSelected(item);
}
}
✅ Conclusion
This completes your Simple & Compound Interest Calculator Android app with stylish UI and menu-driven options. I hope this helps you in building a practical and visually appealing project!
If you found this helpful, do share and comment below! 😊
0 Comments
If you have any doubts, Please let me know