In: Computer Science
Allow the user to enter the number of people in the party. Calculate and display the amount owed by each person if the bill were to be split evenly among the party members.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mdc.tippcalcula"> <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"> <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>
-------------------------------
<?xml version="1.0" encoding="utf-8"?> <GridLayout 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:columnCount="2" android:useDefaultMargins="true" tools:context=".MainActivity"> <EditText android:id="@+id/amountEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="" android:digits="0123456789" android:inputType="number" android:layout_column="0" android:layout_columnSpan="2" android:maxLength="6"/> <TextView android:id="@+id/amountTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="0" android:layout_column="0" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" android:background="@color/amount_background" android:elevation="@dimen/elevation" android:hint="@string/enter_amount" android:padding="@dimen/textview_padding" /> <TextView android:id="@+id/percentTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|end" android:text="@string/tip_percentage" /> <SeekBar android:id="@+id/percentSeekBar" android:layout_width="wrap_content" android:layout_height="@dimen/seekbar_height" android:layout_gravity="fill_horizontal" android:max="30" android:progress="15"/> <TextView android:id="@+id/TipLabelTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:text="@string/tip" /> <TextView android:id="@+id/tipTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="fill_horizontal" android:background="@color/result_background" android:elevation="@dimen/elevation" android:gravity="center" android:padding="@dimen/textview_padding" /> <TextView android:id="@+id/totalLabelTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:text="@string/total" /> <TextView android:id="@+id/totalTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="fill_horizontal" android:background="@color/result_background" android:elevation="@dimen/elevation" android:gravity="center" android:padding="@dimen/textview_padding" /> <EditText android:id="@+id/numberOfPeopleEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="fill_horizontal" android:background="@color/result_background" android:elevation="@dimen/elevation" android:gravity="center" android:padding="@dimen/textview_padding" android:ems="10" android:hint="Enter Number Of People" android:digits="012345678" android:inputType="number" android:layout_columnSpan="2"/> <TextView android:id="@+id/eachPersonLabelTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:text="@string/each" /> <TextView android:id="@+id/eachPersonTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="fill_horizontal" android:background="@color/result_background" android:elevation="@dimen/elevation" android:gravity="center" android:padding="@dimen/textview_padding" /> </GridLayout>
----------
package com.mdc.tippcalcula; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import java.text.NumberFormat; public class MainActivity extends AppCompatActivity { private static final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); private static final NumberFormat percentFormat = NumberFormat.getCurrencyInstance(); private double billAmount = 0.0; private double percent = 0.15; private double numPeople = 0; private TextView percentTextView; private TextView tipTextView; private TextView amountTextView; private TextView totalTextView; private TextView eachPersonTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); amountTextView = (TextView) findViewById (R.id.amountTextView); percentTextView = (TextView) findViewById (R.id.percentTextView); tipTextView = (TextView) findViewById(R.id.tipTextView); totalTextView = (TextView) findViewById(R.id.totalTextView); eachPersonTextView = (TextView) findViewById(R.id.eachPersonTextView); tipTextView.setText(currencyFormat.format(0)); totalTextView.setText(currencyFormat.format(0)); eachPersonTextView.setText(currencyFormat.format(0)); EditText amountEditText = (EditText) findViewById(R.id.amountEditText); amountEditText.addTextChangedListener(amountEditTextWatcher); SeekBar percentSeekBar = (SeekBar) findViewById(R.id.percentSeekBar); percentSeekBar.setOnSeekBarChangeListener(seekBarListener); } private void calculate(){ percentTextView.setText(percentFormat.format(percent)); double tip = billAmount * percent; double total = billAmount + tip; tipTextView.setText(currencyFormat.format(tip)); totalTextView.setText((currencyFormat.format(total))); } private final OnSeekBarChangeListener seekBarListener = new OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { percent = progress / 100.0; calculate(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }; private final TextWatcher amountEditTextWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { try { billAmount = Double.parseDouble(charSequence.toString()) / 100.0; amountTextView.setText(currencyFormat.format(billAmount)); } catch (NumberFormatException e){ amountTextView.setText(""); billAmount = 0.0; } } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { } }; }
------
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="textview_padding">12dp</dimen> <dimen name="elevation">4dp</dimen> <dimen name="seekbar_height">40dp</dimen> </resources>
---
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#6200EE</color> <color name="colorPrimaryDark">#3700B3</color> <color name="colorAccent">#03DAC5</color> <color name="amount_background">#BBDEFB</color> <color name="result_background">#ffE0B2</color> </resources>
----
<resources> <string name="app_name">Tip Calculator</string> <string name="enter_amount"> Enter Amount</string> <string name="tip_percentage">15%</string> <string name="tip">Tip</string> <string name="total">Total</string> <string name="number">No. Of People</string> <string name="each">Each Person</string> </resources>
***Please upvote/thumbsup if you liked the answer***
Screenshot of the modified Android code:-
Output:-
Modified Android Code to copy:-
package com.example.myapp; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import java.text.NumberFormat; public class MainActivity extends AppCompatActivity { private static final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); private static final NumberFormat percentFormat = NumberFormat.getCurrencyInstance(); private double billAmount = 0.0; private double percent = 0.15; private double numPeople = 0; private TextView percentTextView; private TextView tipTextView; private TextView amountTextView; private TextView totalTextView; private TextView eachPersonTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); amountTextView = (TextView) findViewById (R.id.amountTextView); percentTextView = (TextView) findViewById (R.id.percentTextView); tipTextView = (TextView) findViewById(R.id.tipTextView); totalTextView = (TextView) findViewById(R.id.totalTextView); eachPersonTextView = (TextView) findViewById(R.id.eachPersonTextView); tipTextView.setText(currencyFormat.format(0)); totalTextView.setText(currencyFormat.format(0)); eachPersonTextView.setText(currencyFormat.format(0)); EditText amountEditText = (EditText) findViewById(R.id.amountEditText); amountEditText.addTextChangedListener(amountEditTextWatcher); EditText numberofPeopleEditText = (EditText) findViewById(R.id.numberOfPeopleEditText); numberofPeopleEditText.addTextChangedListener(numberOfPeopleEditTextWatcher); SeekBar percentSeekBar = (SeekBar) findViewById(R.id.percentSeekBar); percentSeekBar.setOnSeekBarChangeListener(seekBarListener); } private void calculate(){ percentTextView.setText(percentFormat.format(percent)); double tip = billAmount * percent; double total = billAmount + tip; double eachPersonAmt = total/numPeople; tipTextView.setText(currencyFormat.format(tip)); totalTextView.setText((currencyFormat.format(total))); eachPersonTextView.setText(currencyFormat.format(eachPersonAmt)); } private final OnSeekBarChangeListener seekBarListener = new OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { percent = progress / 100.0; calculate(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }; private final TextWatcher amountEditTextWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { try { billAmount = Double.parseDouble(charSequence.toString()) / 100.0; amountTextView.setText(currencyFormat.format(billAmount)); } catch (NumberFormatException e){ amountTextView.setText(""); billAmount = 0.0; } } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { } }; private final TextWatcher numberOfPeopleEditTextWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { try { numPeople = Integer.parseInt(charSequence.toString()); } catch (NumberFormatException e){ numPeople = 0; } } @Override public void afterTextChanged(Editable editable) { } }; }