Complete Calculator in android

The xml code

<?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”
android:paddingLeft=”10sp”
android:paddingRight=”10sp”
android:background=”@android:color/white”>
<EditText
android:layout_width=”match_parent”
android:layout_height=”80sp”
android:inputType=”textPersonName”
android:layout_marginTop=”10sp”
android:id=”@+id/t”/>

<ScrollView
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:id=”@+id/scrollView”>

<TableLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/ButtonsTable”
android:layout_marginTop=”20sp”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”>
<TableRow
android:layout_width=”wrap_content”
android:layout_height=”fill_parent”
android:gravity=”center”>

<Button
android:text=”1″
android:id=”@+id/b1″
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_width=”100sp”
android:layout_height=”wrap_content”
android:gravity=”center”/>
<Button
android:text=”2″
android:id=”@+id/b2″
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_width=”100sp”
android:layout_height=”wrap_content”
android:gravity=”center”/>

<Button
android:text=”3″
android:id=”@+id/b3″
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_width=”100sp”
android:layout_height=”wrap_content”
android:gravity=”center”/>
</TableRow>

<TableRow
android:layout_width=”wrap_content”
android:layout_height=”fill_parent”
android:layout_marginTop=”10sp”
android:gravity=”center”>

<Button
android:text=”4″
android:id=”@+id/b4″
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_height=”wrap_content”
android:gravity=”center”/>
<Button
android:text=”5″
android:id=”@+id/b5″
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”center”/>

<Button
android:text=”6″
android:id=”@+id/b6″
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”center”/>
</TableRow>
<TableRow
android:layout_width=”wrap_content”
android:layout_height=”fill_parent”
android:layout_marginTop=”10sp”
android:gravity=”center”>

<Button
android:text=”7″
android:id=”@+id/b7″
android:textStyle=”bold”
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:layout_height=”wrap_content”
android:gravity=”center”/>
<Button
android:text=”8″
android:id=”@+id/b8″
android:textStyle=”bold”
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:layout_height=”wrap_content”
android:gravity=”center”/>

<Button
android:text=”9″
android:id=”@+id/b9″
android:textStyle=”bold”
android:textSize=”30sp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”center”/>
</TableRow>
<TableRow
android:layout_width=”wrap_content”
android:layout_height=”fill_parent”
android:layout_marginTop=”10sp”
android:gravity=”center”>

<Button
android:text=”0″
android:id=”@+id/b0″
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_height=”wrap_content”
android:gravity=”center”/>

<Button
android:text=”/”
android:id=”@+id/bdiv”
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_height=”wrap_content”
android:gravity=”center”/>

<Button
android:text=”*”
android:id=”@+id/bmul”
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_height=”wrap_content”
android:gravity=”center”/>
</TableRow>
<TableRow
android:layout_width=”wrap_content”
android:layout_height=”fill_parent”
android:layout_marginTop=”10sp”
android:gravity=”center”>

<Button
android:text=”-”
android:id=”@+id/bsub”
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_height=”wrap_content”
android:gravity=”center”/>

<Button
android:text=”+”
android:id=”@+id/badd”
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_height=”wrap_content”
android:gravity=”center”/>

<Button
android:text=”.”
android:id=”@+id/bdec”
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_height=”wrap_content”
android:gravity=”center”/>
</TableRow>
<TableRow
android:layout_width=”wrap_content”
android:layout_height=”fill_parent”
android:layout_marginTop=”10sp”
android:gravity=”center”>

<Button
android:text=”=”
android:id=”@+id/beq”
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_height=”wrap_content”
android:gravity=”center”/>

<Button
android:text=”del”
android:id=”@+id/bdel”
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_height=”wrap_content”
android:gravity=”center”/>

<Button
android:text=”clr”
android:id=”@+id/bclr”
android:layout_width=”wrap_content”
android:textSize=”30sp”
android:textStyle=”bold”
android:layout_height=”wrap_content”
android:gravity=”center”/>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>

The java code

package com.example.CalculatorApp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyActivity extends Activity implements View.OnClickListener {

private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private Button b0;
private Button bdiv;
private Button bmul;
private Button bsub;
private Button badd;
private Button bdec;
private Button beq;
private Button bdel;
private Button bclr;
private EditText t;
static double a = 0, b = 0, result = 0;
static int operator = 0;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t=(EditText)findViewById(R.id.t);
t.setEnabled(false);
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);
b3=(Button)findViewById(R.id.b3);
b4=(Button)findViewById(R.id.b4);
b5=(Button)findViewById(R.id.b5);
b6=(Button)findViewById(R.id.b6);
b7=(Button)findViewById(R.id.b7);
b8=(Button)findViewById(R.id.b8);
b9=(Button)findViewById(R.id.b9);
b0=(Button)findViewById(R.id.b0);
bdiv=(Button)findViewById(R.id.bdiv);
bmul=(Button)findViewById(R.id.bmul);
bsub=(Button)findViewById(R.id.bsub);
badd=(Button)findViewById(R.id.badd);
bdec=(Button)findViewById(R.id.bdec);
beq=(Button)findViewById(R.id.beq);
bdel=(Button)findViewById(R.id.bdel);
bclr=(Button)findViewById(R.id.bclr);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b0.setOnClickListener(this);
bdiv.setOnClickListener(this);
bmul.setOnClickListener(this);
bsub.setOnClickListener(this);
badd.setOnClickListener(this);
bdec.setOnClickListener(this);
beq.setOnClickListener(this);
bdel.setOnClickListener(this);
bclr.setOnClickListener(this);
}

public void onClick(View v){
if(v.getId()==R.id.b1){
t.setText(t.getText().toString().concat(“1”));
}
if (v.getId()==R.id.b2) {
t.setText(t.getText().toString().concat(“2”));
}
if (v.getId()==R.id.b3) {
t.setText(t.getText().toString().concat(“3”));
}
if (v.getId()==R.id.b4) {
t.setText(t.getText().toString().concat(“4”));
}
if (v.getId()==R.id.b5) {
t.setText(t.getText().toString().concat(“5”));
}
if (v.getId()==R.id.b6) {
t.setText(t.getText().toString().concat(“6”));
}
if (v.getId()==R.id.b7) {
t.setText(t.getText().toString().concat(“7”));
}
if (v.getId()==R.id. b8) {
t.setText(t.getText().toString().concat(“8”));
}
if (v.getId()==R.id.b9) {
t.setText(t.getText().toString().concat(“9”));
}
if (v.getId()==R.id.b0) {
t.setText(t.getText().toString().concat(“0”));
}
if (v.getId()==R.id.bdec) {
t.setText(t.getText().toString().concat(“.”));
}
if (v.getId()==R.id.badd) {
a = Double.parseDouble(t.getText().toString());
operator = 1;
t.setText(“”);
}
if (v.getId()==R.id.bsub) {
a = Double.parseDouble(t.getText().toString());
operator = 2;
t.setText(“”);
}
if (v.getId()==R.id.bmul) {
a = Double.parseDouble(t.getText().toString());
operator = 3;
t.setText(“”);
}
if (v.getId()==R.id.bdiv) {
a = Double.parseDouble(t.getText().toString());
operator = 4;
t.setText(“”);
}
if (v.getId()==R.id.beq) {
b = Double.parseDouble(t.getText().toString());
switch (operator) {
case 1:
result = a + b;
break;

case 2:
result = a – b;
break;

case 3:
result = a * b;
break;

case 4:
result = a / b;
break;

default:
result = 0;
}
t.setText(“” + result);
}
if (v.getId()==R.id.bclr) {
t.setText(“”);
}
if (v.getId()==R.id.bdel) {
String s = t.getText().toString();
t.setText(“”);
for (int i = 0; i < s.length() – 1; i++) {
t.setText(s.charAt(i) + “”);
}
}
}
}cal

Display initial Launch Screen(Activity) in Android

Most of Android Apps display Launch Screen, when Application is being launched, for few Seconds.
To cause delay Thread.sleep() is used with 5000 milliseconds, in below code.
Below is an example of the same.

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

public class LoginPage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
}
}

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;

import java.util.Timer;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class MyActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launch_activity);
Thread timer= new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
try {
Intent i = new Intent(getBaseContext(), NewActivity.class);
startActivity(i);
finish();
}catch(Exception e){
e.printStackTrace();
}
}
}
};
timer.start();
}
}

You may also like to read: