Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need help with this Android App project. The application keeps crashing when running. It is basically an app that calculates the area and perimeter of
Need help with this Android App project. The application keeps crashing when running. It is basically an app that calculates the area and perimeter of a rectangle given the height and width entered by the user in an editText field.
activity_main.xml
MainActivity.java
package com.example.rectanglecalculator; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements TextView.OnEditorActionListener{ private EditText heightField; private EditText widthField; private TextView areaCalculated; private TextView permimeterCalculated; private Button getResults; private SharedPreferences savedValues; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); heightField = (EditText)findViewById(R.id.heightField); widthField = (EditText)findViewById(R.id.widthField); areaCalculated = (TextView)findViewById(R.id.areaCalculated); permimeterCalculated = (TextView)findViewById(R.id.perimeterCalculated); getResults = (Button)findViewById(R.id.getResults); heightField.setOnEditorActionListener(this); widthField.setOnEditorActionListener(this); // create an onLister method here // remember calculate method to pass up getResults.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { calculate(); } }); } public void calculate(){ Float nWidth = Float.parseFloat(widthField.getText().toString()); Float nHeight = Float.parseFloat(heightField.getText().toString()); Float finalArea = nWidth * nHeight; areaCalculated.setText(finalArea.toString()); Float finalPerimeter = (nWidth * 2) + (nHeight *2); permimeterCalculated.setText(finalPerimeter.toString()); } @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED){ calculate(); } return false; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started