How to perform Arithmetic Operations in Android

In this tutorial how to perform simple arithmetic operations in Android is shown.

Code:

activity_main.xml

<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”
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=”.MainActivity” >

<EditText
android:id=”@+id/no1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”44dp”
android:ems=”10″
android:hint=”Please enter a number” >

<requestFocus />
</EditText>

<EditText
android:id=”@+id/no2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/no1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”31dp”
android:ems=”10″
android:hint=”Please enter a number” />

<Button
android:id=”@+id/add”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_centerVertical=”true”
android:layout_marginLeft=”16dp”
android:onClick=”onAdd”
android:text=”+” />

<Button
android:id=”@+id/sub”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerVertical=”true”
android:layout_toRightOf=”@+id/add”
android:onClick=”onSub”
android:text=”-” />

<TextView
android:id=”@+id/ans”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”65dp”
android:text=”TextView” />

<Button
android:id=”@+id/div”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerVertical=”true”
android:layout_toLeftOf=”@+id/mul”
android:onClick=”onDiv”
android:text=”/” />

<Button
android:id=”@+id/mul”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentRight=”true”
android:layout_centerVertical=”true”
android:layout_marginRight=”21dp”
android:onClick=”onMul”
android:text=”*” />

<Button
android:id=”@+id/reset”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/sub”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”30dp”
android:onClick=”onReset”
android:text=”Reset” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {

EditText no1,no2;
TextView ans;
int n1,n2,a;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
no1=(EditText)findViewById(R.id.no1);
no2=(EditText)findViewById(R.id.no2);
ans=(TextView)findViewById(R.id.ans);
}

public void onAdd(View v)
{
n1=Integer.parseInt(no1.getText().toString());
n2=Integer.parseInt(no2.getText().toString());
a=n1+n2;
ans.setText(toString().valueOf(a));
}

public void onSub(View v)
{
n1=Integer.parseInt(no1.getText().toString());
n2=Integer.parseInt(no2.getText().toString());
a=n1-n2;
ans.setText(toString().valueOf(a));
}

public void onDiv(View v)
{
n1=Integer.parseInt(no1.getText().toString());
n2=Integer.parseInt(no2.getText().toString());
a=n1/n2;
ans.setText(toString().valueOf(a));
}

public void onMul(View v)
{
n1=Integer.parseInt(no1.getText().toString());
n2=Integer.parseInt(no2.getText().toString());
a=n1*n2;
ans.setText(toString().valueOf(a));
}

public void onReset(View v)
{
ans.setText(” “);
no1.setText(” “);
no2.setText(” “);
}

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

}

 

Add a Comment

Your email address will not be published. Required fields are marked *