1

I was wondering why my output on my Calculator app is giving me this message?

message

It is probably something simple that I always seem to look over. I thought it might be the output having the wrong EditTexts or parsing things I don't need to. As well as limiting the number to two decimal places, I can find a fix but I thought I'd ask anyways. Any hints would be appreciated, still pretty new to android programming.

    package com.filej.calculatorapp;
    import android.support.v4.media.MediaBrowserServiceCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    public class CalculatorApp extends AppCompatActivity` {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator_app);

        EditText heightEdit = (EditText) findViewById(R.id.heightEdit);
        EditText widthEdit = (EditText) findViewById(R.id.widthEdit);
        TextView area = (TextView) findViewById(R.id.areaTextView);
        TextView perimeter = (TextView) findViewById(R.id.perimeterTextView);

        Button calculate = (Button) findViewById(R.id.calculateButton);

        View view = findViewById(R.id.calculateButton);
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText height = (EditText) findViewById(R.id.heightEdit);
                EditText width = (EditText) findViewById(R.id.widthEdit);
                calculateArea(height.getText().toString(), width.getText().toString());
                calculatePerimeter(height.getText().toString(), width.getText().toString());

            }
        });


    }

    private void calculateArea(String cHeight, String cWidth)
    {
        TextView area = (TextView) findViewById(R.id.areaTextView);
        int area1 = Integer.parseInt(Height)* Integer.parseInt(Width);
        area.setText(String.valueOf(area));
    }
    private void calculatePerimeter (String dHeight, String dWidth)
    {
        TextView perimeter = (TextView) findViewById(R.id.perimeterTextView);
        int perimeter1 = (Integer.parseInt(Height) * 2) + (Integer.parseInt(Width) * 2);
        perimeter.setText(String.valueOf(perimeter));
    }
    }

This is my XML

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_calculator_app"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.filej.calculatorapp.CalculatorApp">

    <TextView
        android:text="Width"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="33dp"
        android:id="@+id/textView2" />

    <TextView
        android:text="Height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:id="@+id/textView"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:text="Area"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="81dp"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="41dp"
        android:id="@+id/textView4"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="Perimeter" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number|numberDecimal"
        android:ems="10"
        android:id="@+id/widthEdit"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:singleLine="false"
        android:text="Enter Width" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number|numberDecimal"
        android:ems="10"
        android:id="@+id/heightEdit"
        android:text="Enter Height"
        android:layout_alignBaseline="@+id/textView"
        android:layout_alignBottom="@+id/textView"
        android:layout_alignLeft="@+id/widthEdit"
        android:layout_alignStart="@+id/widthEdit" />

    <TextView
        android:text="Total Area"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignLeft="@+id/heightEdit"
        android:layout_alignStart="@+id/heightEdit"
        android:id="@+id/areaTextView"
        android:layout_alignRight="@+id/heightEdit"
        android:layout_alignEnd="@+id/heightEdit" />

    <TextView
        android:text="Total Perimeter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/perimeterTextView"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignLeft="@+id/areaTextView"
        android:layout_alignStart="@+id/areaTextView"
        android:layout_alignRight="@+id/areaTextView"
        android:layout_alignEnd="@+id/areaTextView" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_toRightOf="@+id/textView2"
        android:layout_toEndOf="@+id/textView2"
        android:layout_marginTop="52dp"
        android:id="@+id/calculateButton" />

</RelativeLayout>
u32i64
  • 2,273
  • 3
  • 21
  • 35
jake
  • 35
  • 7

3 Answers3

3

Shouldn't your listener be on your calculate button instead of that View? Something like this

calculate = (Button) findViewById(R.id.calculateButton);
            calculate.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText height = (EditText) findViewById(R.id.heightEdit);
                    EditText width = (EditText) findViewById(R.id.widthEdit);
                    calculateArea(height.getText().toString(), width.getText().toString());
                    calculatePerimeter(height.getText().toString(), width.getText().toString());

                }
            });

and also you're setting the textview with wrong value and parsing wrong variable, try:

private void calculateArea(String cHeight, String cWidth)
{
    TextView area = (TextView) findViewById(R.id.areaTextView);
    int area1 = Integer.parseInt(cHeight)* Integer.parseInt(cWidth);
    area.setText(String.valueOf(area1));
}
private void calculatePerimeter (String dHeight, String dWidth)
{
    TextView perimeter = (TextView) findViewById(R.id.perimeterTextView);
    int perimeter1 = (Integer.parseInt(dHeight) * 2) + (Integer.parseInt(dWidth) * 2);
    perimeter.setText(String.valueOf(perimeter1));
}
Utsav Shrestha
  • 440
  • 2
  • 11
1

Your line such as area.setText(String.valueOf(area)); is setting the text to the name of the object.

Steve Smith
  • 2,008
  • 2
  • 14
  • 21
1

You are setting wrong text for edit text

use

  area.setText(String.valueOf(area1));

and

 perimeter.setText(String.valueOf(perimeter1));

instead of

  area.setText(String.valueOf(area));
  perimeter.setText(String.valueOf(perimeter));

in your case area and perimeter are views and area1 and perimeter1 are calculated values

Manohar Reddy
  • 15,894
  • 7
  • 77
  • 109
  • Thanks so much! I knew it was something so simple. To set the fields to allow decimals, I would have to change Int to double? – jake Feb 14 '17 at 09:52
  • Last question, how do I format it to round to 2 decimal places if it's a double? Since i know how to do String.format ("%s, object") is one way of doing it in the string form. I was wondering if there was a way to do it with a double? – jake Feb 14 '17 at 10:04
  • have a look at http://stackoverflow.com/questions/11701399/round-up-to-2-decimal-places-in-java – Manohar Reddy Feb 14 '17 at 10:05