Calculator Coding For Android

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Calculator Coding For Android

Postby Dliswa » Mon Oct 18, 2010 8:40 pm

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText android:layout_height="wrap_content" android:id="@+id/First_Operand" android:layout_width="fill_parent" android:inputType="number"></EditText><LinearLayout
   android:orientation="horizontal"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent"
   >
   
<Button android:layout_height="wrap_content" android:id="@+id/Plus" android:text="+" android:layout_width="70dp"></Button>
<Button android:layout_height="wrap_content" android:text="-" android:id="@+id/Subtract" android:layout_width="70dp"></Button><Button android:layout_height="wrap_content" android:id="@+id/multiply" android:text="*" android:layout_width="70dp"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/divide" android:text="/" android:layout_width="70dp"></Button>

</LinearLayout>

<EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:inputType="number" android:id="@+id/second_Operand"></EditText><Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/answerButton" android:id="@+id/New_calculation"></Button>
<EditText android:layout_height="wrap_content" android:id="@+id/finalAnswer" android:layout_width="fill_parent" android:inputType="number"></EditText>
</LinearLayout>





String.xml File is Given Below

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MyActivity!</string>
    <string name="plus_string">Add</string>
    <string name="app_name">Calculator</string>

<string name="multiply_string">Multiply</string>
<string name="divide_String">Divide</string>
<string name="subtract_string">Subtract</string>

<string name="RadioGroup01">RadioGroup</string>
<string name="answerButton">New Calculation !</string>
<string name="RadioGroup">Radio Groups</string>

</resources>





Java File is Given Below

Code: Select all
package com.google.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MyActivity extends Activity {
    /** Called when the activity is first created. */
    EditText firstOperand;
    EditText secondOperand;
    EditText finalResult;
    OnClickListener radioGroup;
   
    double answer;
    double first_operand;
    double second_operand;
   
    Button addButton;
    Button divideButton;
    Button multiplyButton;
    Button subtractButton;
    Button final_calculation;
   
   
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        firstOperand = (EditText)findViewById(R.id.First_Operand);
        secondOperand = (EditText)findViewById(R.id.second_Operand);
        finalResult = (EditText)findViewById(R.id.finalAnswer);
        firstOperand.setInputType(InputType.TYPE_NULL);
        secondOperand.setInputType(InputType.TYPE_NULL);
        finalResult.setInputType(InputType.TYPE_NULL);
       
        addButton = (Button)findViewById(R.id.Plus);
        addButton.setOnClickListener(new AddbuttonHandler());
       
        divideButton =(Button)findViewById(R.id.divide);
        divideButton.setOnClickListener(new DivideButtonHandler());
       
        multiplyButton = (Button)findViewById(R.id.multiply);
        multiplyButton.setOnClickListener(new MultiplybuttonHandler());
       
        subtractButton = (Button)findViewById(R.id.Subtract);
        subtractButton.setOnClickListener(new SubtractButtonhandler());
       
        final_calculation = (Button)findViewById(R.id.New_calculation);
        final_calculation.setOnClickListener(new ResetAll());
       
       
    }

   private class AddbuttonHandler implements OnClickListener{

      @Override
      public void onClick(View v) {
         // TODO Auto-generated method stub
         first_operand = Double.parseDouble(firstOperand.getText().toString());
           second_operand = Double.parseDouble(secondOperand.getText().toString());
           answer = first_operand + second_operand;
           finalResult.setText(answer + "");
         
         
      }
      
   }
   
   private class MultiplybuttonHandler implements OnClickListener{

      @Override
      public void onClick(View v) {
         // TODO Auto-generated method stub
         first_operand = Double.parseDouble(firstOperand.getText().toString());
           second_operand = Double.parseDouble(secondOperand.getText().toString());
           answer = first_operand * second_operand;
           finalResult.setText(answer + "");
         
      }
      
   }
   
   private class DivideButtonHandler implements OnClickListener{

      @Override
      public void onClick(View v) {
         // TODO Auto-generated method stub
         first_operand = Double.parseDouble(firstOperand.getText().toString());
           second_operand = Double.parseDouble(secondOperand.getText().toString());
           answer = first_operand / second_operand;
           finalResult.setText(answer + "");
         
      }
      
   }
   
   private class SubtractButtonhandler implements OnClickListener{

      @Override
      public void onClick(View v) {
         // TODO Auto-generated method stub
         first_operand = Double.parseDouble(firstOperand.getText().toString());
           second_operand = Double.parseDouble(secondOperand.getText().toString());
           answer = first_operand - second_operand;
           finalResult.setText(answer + "");
         
      }
      
   }
   private class ResetAll implements OnClickListener{

      @Override
      public void onClick(View v) {
         // TODO Auto-generated method stub
         firstOperand.setText("");
         secondOperand.setText("");
         finalResult.setText("");
         
         
      }
      
   }
}



Dliswa
Developer
Developer
 
Posts: 30
Joined: Fri Oct 15, 2010 5:56 am

Top

Re: Calculator Coding For Android

Postby yagneshrevar » Mon Nov 01, 2010 11:04 am

Thanks. It worked.
yagneshrevar
Freshman
Freshman
 
Posts: 3
Joined: Sat Oct 16, 2010 7:35 am

Re: Calculator Coding For Android

Postby andrenievo » Thu Nov 25, 2010 11:45 pm

Wow! Great stuff, this is amazing! Thanks for posting this, it really worked! :D
andrenievo
Freshman
Freshman
 
Posts: 2
Joined: Wed Nov 24, 2010 11:23 pm
Location: Philippines

Re: Calculator Coding For Android

Postby sukhito_kun » Fri Nov 26, 2010 11:56 am

thanx!
i learn a lot from this stuff :D :D :D :D :mrgreen:
sukhito_kun
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Nov 25, 2010 7:13 pm

Re: Calculator Coding For Android

Postby veddna » Fri Apr 29, 2011 11:16 am

Hmmm .. k ..
My doubt may sound very nooby, but need to sort it out .. Am a beginner ..
There are 3 sets of code available in the post ..
1. The java file which gives the details of the logic of the calculator application
2. The main.xml defining the layout of the buttons
3. and The string.xml ..
My doubt is - On what basis do we define the contents of string.xml .. Beccause all the tokens that are used in this file are found in the R.java file .. And R.java is itself an auto-generated file .. What is happening here .. Please do shed some light .. :)
Thanks in advance .. :)
veddna
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 29, 2011 11:09 am

Re: Calculator Coding For Android

Postby sk1289 » Sat Jan 28, 2012 10:09 pm

i paste everything but it doesnt work on an emulator. there is no error.
sk1289
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Jan 28, 2012 10:06 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests