In this example we are going to see that how to perform basic arithmetic operation in android studio.
Example ::
activity_main.xml (Inside res/layout )
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number 1" />
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number 2" />
<Button
android:id="@+id/sum"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Sum"
android:textStyle="bold" />
<Button
android:id="@+id/sub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Substraction"
android:textStyle="bold" />
<Button
android:id="@+id/mul"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Multiplication"
android:textStyle="bold" />
<Button
android:id="@+id/div"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Division"
android:textStyle="bold" />
<Button
android:id="@+id/mod"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Modulus"
android:textStyle="bold" />
<EditText
android:id="@+id/editTextResultHere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:hint="Result here"
android:textStyle="bold" />
</LinearLayout>
Example ::
activity_main.xml (Inside res/layout )
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number 1" />
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number 2" />
<Button
android:id="@+id/sum"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Sum"
android:textStyle="bold" />
<Button
android:id="@+id/sub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Substraction"
android:textStyle="bold" />
<Button
android:id="@+id/mul"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Multiplication"
android:textStyle="bold" />
<Button
android:id="@+id/div"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Division"
android:textStyle="bold" />
<Button
android:id="@+id/mod"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Modulus"
android:textStyle="bold" />
<EditText
android:id="@+id/editTextResultHere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:hint="Result here"
android:textStyle="bold" />
</LinearLayout>
MainActivity.java
package com.example.malik.blog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText num1, num2, result;
Button sum, sub, mul, div, mod;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.num1);
num2 = (EditText) findViewById(R.id.num2);
result = (EditText) findViewById(R.id.editTextResultHere);
sum = (Button) findViewById(R.id.sum);
sub = (Button) findViewById(R.id.sub);
mul = (Button) findViewById(R.id.mul);
div = (Button) findViewById(R.id.div);
mod = (Button) findViewById(R.id.mod);
sum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String NUM1 = num1.getText().toString();
String NUM2 = num2.getText().toString();
int a = Integer.parseInt(NUM1);
int b = Integer.parseInt(NUM2);
int total = a + b;
Toast.makeText(getApplicationContext(),"Sum is " + Integer.toString(total), Toast.LENGTH_LONG).show();
result.setText("Sum is " + Integer.toString(total));
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String NUM1 = num1.getText().toString();
String NUM2 = num2.getText().toString();
int a = Integer.parseInt(NUM1);
int b = Integer.parseInt(NUM2);
int total = a - b;
Toast.makeText(getApplicationContext(), "Sub is " +Integer.toString(total), Toast.LENGTH_LONG).show();
result.setText("Sub is " + Integer.toString(total));
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String NUM1 = num1.getText().toString();
String NUM2 = num2.getText().toString();
int a = Integer.parseInt(NUM1);
int b = Integer.parseInt(NUM2);
int total = a * b;
Toast.makeText(getApplicationContext(),"Mul is " + Integer.toString(total), Toast.LENGTH_LONG).show();
result.setText("Mul is " + Integer.toString(total));
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String NUM1 = num1.getText().toString();
String NUM2 = num2.getText().toString();
int a = Integer.parseInt(NUM1);
int b = Integer.parseInt(NUM2);
int total = a / b;
Toast.makeText(getApplicationContext(), "Division is " +Integer.toString(total), Toast.LENGTH_LONG).show();
result.setText("Division is " + Integer.toString(total));
}
});
mod.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String NUM1 = num1.getText().toString();
String NUM2 = num2.getText().toString();
int a = Integer.parseInt(NUM1);
int b = Integer.parseInt(NUM2);
int total = a % b;
Toast.makeText(getApplicationContext(),"Modulus is " + Integer.toString(total), Toast.LENGTH_LONG).show();
result.setText("Modulus is " + Integer.toString(total));
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.malik.blog">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.malik.blog"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}