AIDE Tutorial - 54 Simple Calculator
Watch the video below Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
Watch the video below Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/edittext1"
android:inputType="number|numberDecimal"
android:layout_width="match_parent" android:layout_height="wrap_content" />
android:id="@+id/edittext1"
android:inputType="number|numberDecimal"
android:layout_width="match_parent" android:layout_height="wrap_content" />
<EditText
android:layout_below="@+id/edittext1"
android:id="@+id/edittext2"
android:inputType="number|numberDecimal"
android:layout_width="match_parent" android:layout_height="wrap_content" />
android:layout_below="@+id/edittext1"
android:id="@+id/edittext2"
android:inputType="number|numberDecimal"
android:layout_width="match_parent" android:layout_height="wrap_content" />
<Button
android:layout_below="@+id/edittext2"
android:id="@+id/add"
android:text="Add"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
android:layout_below="@+id/edittext2"
android:id="@+id/add"
android:text="Add"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button
android:layout_below="@+id/add"
android:id="@+id/subtract"
android:text="Subtract"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
android:layout_below="@+id/add"
android:id="@+id/subtract"
android:text="Subtract"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button
android:layout_below="@+id/subtract"
android:id="@+id/multiply"
android:text="Multiply"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
android:layout_below="@+id/subtract"
android:id="@+id/multiply"
android:text="Multiply"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button
android:layout_below="@+id/multiply"
android:id="@+id/divide"
android:text="Divide"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
android:layout_below="@+id/multiply"
android:id="@+id/divide"
android:text="Divide"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView
android:id="@+id/mainTextView"
android:layout_below="@+id/divide"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
android:id="@+id/mainTextView"
android:layout_below="@+id/divide"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:id="@+id/edittext1" android:inputType="number|numberDecimal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:layout_below="@+id/edittext1" android:id="@+id/edittext2" android:inputType="number|numberDecimal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_below="@+id/edittext2" android:id="@+id/add" android:text="Add" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_below="@+id/add" android:id="@+id/subtract" android:text="Subtract" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_below="@+id/subtract" android:id="@+id/multiply" android:text="Multiply" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_below="@+id/multiply" android:id="@+id/divide" android:text="Divide" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/mainTextView" android:layout_below="@+id/divide" android:text="" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
MainActivity.java
package com.kcgroup.simplecalculator;
package com.kcgroup.simplecalculator;
import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.os.*;
import android.widget.*;
import android.view.*;
public class MainActivity extends Activity
{ private Button btnadd, btnsub, btnmul, btndiv;
private EditText et1,et2;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnadd=(Button)findViewById(R.id.add);
btnsub=(Button)findViewById(R.id.subtract);
btnmul=(Button)findViewById(R.id.multiply);
btndiv=(Button)findViewById(R.id.divide);
et1=(EditText)findViewById(R.id.edittext1);
et2=(EditText)findViewById(R.id.edittext2);
tv=(TextView)findViewById(R.id.mainTextView);
btnadd.setOnClickListener(new View.OnClickListener(){
{ private Button btnadd, btnsub, btnmul, btndiv;
private EditText et1,et2;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnadd=(Button)findViewById(R.id.add);
btnsub=(Button)findViewById(R.id.subtract);
btnmul=(Button)findViewById(R.id.multiply);
btndiv=(Button)findViewById(R.id.divide);
et1=(EditText)findViewById(R.id.edittext1);
et2=(EditText)findViewById(R.id.edittext2);
tv=(TextView)findViewById(R.id.mainTextView);
btnadd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View p1)
{
double a=Double.parseDouble(et1.getText().toString());
double b=Double.parseDouble(et2.getText().toString());
double result=a+b;
tv.setText(String.valueOf(result));
// TODO: Implement this method
} });
btnsub.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
double a=Double.parseDouble(et1.getText().toString());
double b=Double.parseDouble(et2.getText().toString());
double result=a+b;
tv.setText(String.valueOf(result));
// TODO: Implement this method
} });
btnsub.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View p1)
{
double a=Double.parseDouble(et1.getText().toString());
double b=Double.parseDouble(et2.getText().toString());
double result=a-b;
tv.setText(String.valueOf(result));
public void onClick(View p1)
{
double a=Double.parseDouble(et1.getText().toString());
double b=Double.parseDouble(et2.getText().toString());
double result=a-b;
tv.setText(String.valueOf(result));
// TODO: Implement this method
} });
btnmul.setOnClickListener(new View.OnClickListener(){
} });
btnmul.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View p1)
{
double a=Double.parseDouble(et1.getText().toString());
double b=Double.parseDouble(et2.getText().toString());
double result=a*b;
tv.setText(String.valueOf(result));
public void onClick(View p1)
{
double a=Double.parseDouble(et1.getText().toString());
double b=Double.parseDouble(et2.getText().toString());
double result=a*b;
tv.setText(String.valueOf(result));
// TODO: Implement this method
} });
btndiv.setOnClickListener(new View.OnClickListener(){
} });
btndiv.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View p1)
{
double a=Double.parseDouble(et1.getText().toString());
double b=Double.parseDouble(et2.getText().toString());
double result=a/b;
tv.setText(String.valueOf(result)); } });
}}
public void onClick(View p1)
{
double a=Double.parseDouble(et1.getText().toString());
double b=Double.parseDouble(et2.getText().toString());
double result=a/b;
tv.setText(String.valueOf(result)); } });
}}