In: Computer Science
Your task is to build an Android application that satisfies the following requirements:
Has two activities: MainActivity that implements the main functionality of the application and AboutActivity that shows your full name as per college record and student id
The AboutActivity should be available through a menu
A back button should appear in the ActionBar of the AboutActivity that takes user back
to the MainActivity. It should be done through configuration!
MainActivity should implement the following functionality
o read the following input from the user: § number of hours worked
§ hourly rate
o calculate the pay using the following formula
§ if no of hours is less or equal than 40 then pay=no_of_hours*hourly_rate
§ else, pay=(no_of_hours-40)*hourly_rate*1.5 + 40*hourly_rate o calculate the tax using the following formula
§ tax=pay*0.18
o present following data to the user
§ pay
§ overtime pay § total pay
§ tax
Hi there! You first need to create a new project and then add the required files. I have attached screenshots for steps and added comments for explanation in code. I have also attached screenshots of the app. Please upvote if it helped.
Steps to create new Project
1. Open Android Studio:
2. Click on Start a new Android Studio Project:
3.Create a new project on Android Studio(with an Empty Activity and package name as com.project.taxcalculator and Application Name of your choice):
Creation of the required files
Since we need menu to navigate to AboutActivity I will first show you how to create a menu xml file.
1. Make sure that you are in the Project type structure as shown in the following picture and then navigate to app->src->res and right click on res and then follow New->Android Resource File(If you cannot see the panel on the left press Alt+1)
2. Then in the following pop-up, select the resource type as menu and file name as menu.xml and click on OK.
3. Copy paste the following contents to app->res->menu->menu.xml created.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<!--XML file for creating a menu with a single item-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/about_me"
android:title="About me" />
</menu>
Copy paste the code in the following respective files:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--A linearlayout is used to display elements in a linear fashion, either horizontally or vertically-->
<!--the horizontal or vertical fashion can be specified using the orientation attribute of linearlayout-->
<!--Created a vertical linearlayout of horizontal linearlayouts-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--A layout_weight attribute gives us control about the space occupied by any element inside a linearlayout-->
<!--layout_weight:1 helps us evenly distribute the space between the children of a linearlayout -->
<!--since width is controlled by weight we specify width as 0px-->
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Number of hours worked: " />
<EditText
android:id="@+id/num_hours_edit_text"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hourly rate: " />
<EditText
android:id="@+id/hourly_rate_edit_text"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<!--This button will be used for calculating and displaying tax and pay-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="calculateTaxAndPay"
android:text="Calculate Tax and Pay" />
<!--All the following elements are used for displaying output-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pay: " />
<TextView
android:id="@+id/pay_text_view"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Overtime pay: " />
<TextView
android:id="@+id/overtime_pay_text_view"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Total pay: " />
<TextView
android:id="@+id/total_pay_text_view"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Tax: " />
<TextView
android:id="@+id/tax_text_view"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.project.taxcalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//creating EditTexts and TextViews as global variables so that they can be accessible inside the onClick method calculateTaxAndPay
EditText noOfHoursEditText;
EditText hourlyRateEditText;
TextView payTextView, overtimePayTextView, totalPayTextView, taxTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// we need to get the EditTexts and TextViews we defined in XML into our Java file in order to take input and display output to the user
// we do it by specifying an Id in XML file and referencing the Id to fetch the elements in Java code
// these EditTexts are used for taking input from the user
noOfHoursEditText = findViewById(R.id.num_hours_edit_text);
hourlyRateEditText = findViewById(R.id.hourly_rate_edit_text);
// these TextViews are used for displaying output to the user
payTextView = findViewById(R.id.pay_text_view);
overtimePayTextView = findViewById(R.id.overtime_pay_text_view);
totalPayTextView = findViewById(R.id.total_pay_text_view);
taxTextView = findViewById(R.id.tax_text_view);
}
// as we have specified this method in the onClick attribute of our button, this method will be called if the user clicks on the button
public void calculateTaxAndPay(View view) {
try {
// after the button is clicked we fetch the data entered in the EditTexts using getText and toString methods
// we then convert the string into double type using parseDouble method
double noOfHoursWorked = Double.parseDouble(noOfHoursEditText.getText().toString());
double hourlyRate = Double.parseDouble(hourlyRateEditText.getText().toString());
// initializing methods for displaying in the output
double pay = 0;
double overtimePay = 0;
double totalPay = 0;
double tax = 0;
// calculating the output using the given formulas
if (noOfHoursWorked <= 40) {
pay = noOfHoursWorked * hourlyRate;
overtimePay = 0;
totalPay = pay;
} else {
pay = 40 * hourlyRate;
overtimePay = (noOfHoursWorked - 40) * hourlyRate;
totalPay = pay + overtimePay;
}
tax = totalPay * 0.18;
// displaying the output to the user by using setText method on TextViews
// also used String format method in order to show only decimal places for double values
payTextView.setText(String.format("%.2f", pay));
overtimePayTextView.setText(String.format("%.2f", overtimePay));
totalPayTextView.setText(String.format("%.2f", totalPay));
taxTextView.setText(String.format("%.2f", tax));
} catch (NumberFormatException nfe) {
// If the user does not give valid input a toast message will be displayed
Toast.makeText(this, "Invalid input!", Toast.LENGTH_SHORT).show();
}
}
// this method is used to inflate(render) the menu from the XML file
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
// this method is used to implement functionality when any of the menu items are clicked
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.about_me:
// startActivity method is used to navigate from one activity to another
startActivity(new Intent(MainActivity.this, AboutActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Then create a new activity by right clicking on java->New->Activity->Empty Activity from the right pane as shown in the picture:
Then type in AboutActivity under Activity Name and click Finish
activity_about.xml(here in the TextView you need to type in your name and student id) For example Developed by Andrew\nStudent Id: 12345
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--You can change your name and Student Id below-->
<!--layout_centerInParent attribute will center the textview for us-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Developed by x\nStudent Id: 123" />
</RelativeLayout>
AboutActivity.java
package com.project.taxcalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class AboutActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
}
}
AndroidManifest.xml
You need to add parentActivityName attribute for AboutActivity in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.taxcalculator">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--parentActivityName attribute is used to implement the back functionality using configuration as mentioned in the question-->
<activity
android:name=".AboutActivity"
android:parentActivityName=".MainActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity screenshot
AboutActivity Screenshot(You can add your details by going on activity_about.xml)
Dont forget to upvote!