Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

How would I add a TextWatcher to the following Java calculator app code:

package com.example.tipcalculatorv2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private EditText billEditText;
private TextView totalTextView;
private EditText tipPercentEditText;
private TextView tipTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
billEditText = (EditText) findViewById(R.id.amount_bill);
totalTextView = (TextView) findViewById(R.id.amount_total);
tipPercentEditText = (EditText) findViewById(R.id.amount_tip_percent);
tipTextView = (TextView) findViewById(R.id.amount_tip);
}

public void calculate(View view) {
String billStr=billEditText.getText().toString();
totalTextView.setText(billStr);
String tipPercentStr=tipPercentEditText.getText().toString();
tipTextView.setText(billStr);
try {
float billAmount = Float.parseFloat(billStr);
float tipPercent = Float.parseFloat(tipPercentStr);
float tip = billAmount * tipPercent / 100;
tipTextView.setText(Float.toString(tip));
float total = billAmount + tip;
totalTextView.setText(Float.toString(total));
} catch (NumberFormatException e) {
Context context = getApplicationContext();
CharSequence text = "Please enter numbers for bill and tip";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();

e.printStackTrace();
}


}
}
Expert Solution
Check Mark
Step 1: Providing Introduction

You can include a TextWatcher in your EditText fields to increase the functionality of your calculator app. As the user inputs or adjusts values in the input fields, this will enable you to update calculations in real-time.

Please refer to the following steps for the complete solution to the problem above.

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education