Android calculator with button click

Posted by rynwtts on Stack Overflow See other posts from Stack Overflow or by rynwtts
Published on 2012-11-25T09:42:32Z Indexed on 2012/11/25 11:04 UTC
Read the original article Hit count: 369

I am trying to calculate a field named lblAnswer by adding values txtA + txtB. I am fairly new to the android development world and would like to know what is the best way of going about this. I have already added the necessarily edit fields to the GUI. I am now working in the java file to try and create the method. This method has been named doCalc. Here is what I have thus far.

public void doCalc() 
{
    lblAnswer = txtA + txtB;
}

It has been suggested that I add more code here is the full code. Thank you for that suggestion.

Here is the Java File.

      package com.example.wattsprofessional;

     import android.app.Activity;
     import android.os.Bundle;
     import android.view.Menu;

    public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


public void doCalc() 
{
    lblAnswer = txtA + txtB;
    Double.parseDouble(txtA.getText().toString());
    lblAnswer.setText"t
}

and here is the xml file.

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
              tools:context=".MainActivity" >

    <EditText
           android:id="@+id/txtA"
           android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
             android:layout_centerHorizontal="true"
             android:layout_marginTop="40dp"
            android:ems="10"
            android:hint="Write Here"
            android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/txtB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txtA"
    android:layout_below="@+id/txtA"
    android:layout_marginTop="32dp"
    android:ems="10"
    android:hint="Second Here"
    android:inputType="numberDecimal" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/calculate" 
    android:onClick="doCalc"/>

<TextView
    android:id="@+id/lblAnswer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:text="TextView" />

    </RelativeLayout>

© Stack Overflow or respective owner

Related posts about android

Related posts about button