Monday, June 25, 2012


Kutties let us atrt with the basic program in android .. creating a calulator !!

main.xml file

you may not understand what is main.xml .. so v can do onething go to developers.android.com and study the basics .. http://developer.android.com/index.html#



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#33b5e5"
    >

    <EditText
        android:id="@+id/selection"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#33b5e5"
        android:gravity="right"
        android:hint="@string/message"
        android:singleLine="true" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessage7"
            android:text="@string/seven" />

        <Button
            android:id="@+id/button8"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessage8"
            android:text="@string/eight" />

        <Button
            android:id="@+id/button9"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessage9"
            android:text="@string/nine" android:shadowColor="#0f0"/>

        <Button
            android:id="@+id/buttondiv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessagediv"
            android:text="@string/div" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessage4"
            android:text="@string/four" />

        <Button
            android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessage5"
            android:text="@string/five" />

        <Button
            android:id="@+id/button6"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessage6"
            android:text="@string/six" />

        <Button
            android:id="@+id/buttonmul"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessagemul"
            android:text="@string/multi" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessage"
            android:text="@string/one" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessage2"
            android:text="@string/two" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessage3"
            android:text="@string/three" />

        <Button
            android:id="@+id/buttonminus"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessageminus"
            android:text="@string/minus" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button0"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessage0"
            android:text="@string/zero" />

        <Button
            android:id="@+id/button10"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessageplus"
            android:text="@string/plus" />

        <Button
            android:id="@+id/buttonequal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessageequal"
            android:text="@string/equal" />

        <Button
            android:id="@+id/buttonC"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessageC"
            android:text="@string/C" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/buttondelete"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessagedelete"
            android:text="@string/delete" />

        <Button
            android:id="@+id/buttonsqrt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessagesqrt"
            android:text="@string/sqrt" />

        <Button
            android:id="@+id/buttonbyx"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="sendMessagebyx"
            android:text="@string/byx" />
    </LinearLayout>

</LinearLayout>



string.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">CALCULATE</string>
    <string name="app_name">Calculator</string>
    <string name="message">Enter ur Numbers</string>
    <string name="one">1</string>
    <string name="two">2</string>
    <string name="three">3</string>
    <string name="four">4</string>
    <string name="zero">0</string>
    <string name="five">5</string>
    <string name="six">6</string>
    <string name="seven">7</string>
    <string name="eight">8</string>
    <string name="nine">9</string>
    <string name="equal">=</string>
    <string name="C">C</string>
    <string name="byx">1/x</string>
    <string name="sqrt">sqrt</string>
    <string name="delete">delete</string>
    <string name="plus">+</string>
    <string name="div">/</string>
    <string name="multi">*</string>
    <string name="minus">-</string>
  
</resources>


CalculateActivity.java




package com.sangeeth.cal;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CalculateActivity extends Activity {
boolean flag = true;
Double sqrtnum;
String str,s;
Integer temp=0 ;
Integer temp2=0;
Integer tot=0;
char opt;
boolean show = true;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void sendMessage(View view) {
EditText no = (EditText) findViewById(R.id.selection);

if(show == true)
{
no.setText("");
show = false;
}
Button numbut = (Button) findViewById(R.id.button1);
String j = numbut.getText().toString();
no.setText(no.getText() + j);

}

public void sendMessage0(View view) {
EditText no = (EditText) findViewById(R.id.selection);
if(show == true)
{
no.setText("");
show = false;
}
Button numbut = (Button) findViewById(R.id.button0);
String j = numbut.getText().toString();
no.setText(no.getText() + j);

}

public void sendMessage2(View view) {
EditText no = (EditText) findViewById(R.id.selection);
if(show == true)
{
no.setText("");
show = false;
}
Button numbut2 = (Button) findViewById(R.id.button2);
String j2 = numbut2.getText().toString();
no.setText(no.getText() + j2);

}

public void sendMessage3(View view) {
EditText no = (EditText) findViewById(R.id.selection);
if(show == true)
{
no.setText("");
show = false;
}
Button numbut2 = (Button) findViewById(R.id.button3);
String j2 = numbut2.getText().toString();
no.setText(no.getText() + j2);

}

public void sendMessage4(View view) {
EditText no = (EditText) findViewById(R.id.selection);
if(show == true)
{
no.setText("");
show = false;
}
Button numbut2 = (Button) findViewById(R.id.button4);

String j2 = numbut2.getText().toString();
no.setText(no.getText() + j2);
}

public void sendMessage5(View view) {
EditText no = (EditText) findViewById(R.id.selection);
if(show == true)
{
no.setText("");
show = false;
}
Button numbut2 = (Button) findViewById(R.id.button5);
String j2 = numbut2.getText().toString();
no.setText(no.getText() + j2);

}

public void sendMessage6(View view) {
EditText no = (EditText) findViewById(R.id.selection);
if(show == true)
{
no.setText("");
show = false;
}
Button numbut2 = (Button) findViewById(R.id.button6);
String j2 = numbut2.getText().toString();
no.setText(no.getText() + j2);

}

public void sendMessage7(View view) {
EditText no = (EditText) findViewById(R.id.selection);
if(show == true)
{
no.setText("");
show = false;
}
Button numbut2 = (Button) findViewById(R.id.button7);
String j2 = numbut2.getText().toString();
no.setText(no.getText() + j2);

}

public void sendMessage8(View view) {
EditText no = (EditText) findViewById(R.id.selection);
if(show == true)
{
no.setText("");
show = false;
}
Button numbut2 = (Button) findViewById(R.id.button8);
String j2 = numbut2.getText().toString();
no.setText(no.getText() + j2);

}

public void sendMessage9(View view) {
EditText no = (EditText) findViewById(R.id.selection);
if(show == true)
{
no.setText("");
show = false;
}
Button numbut2 = (Button) findViewById(R.id.button9);
String j2 = numbut2.getText().toString();
no.setText(no.getText() + j2);
}

public void sendMessageplus(View view)
{
EditText no = (EditText) findViewById(R.id.selection);

if(flag)
{
temp=Integer.parseInt(no.getText().toString());
flag=false;
show = true;
no.setText("");
opt='+';
}
else
{
   show = true;
temp2=Integer.parseInt(no.getText().toString());
tot=temp+temp2;
no.setText(tot.toString());
flag = true;
opt='+';
}
}
public void sendMessagesqrt(View view)
{
EditText no = (EditText) findViewById(R.id.selection);
temp=Integer.parseInt(no.getText().toString());

sqrtnum= (Double) Math.sqrt(temp);
String str=sqrtnum.toString();
no.setText(str);
flag=true;
show=true;

}
public void sendMessagebyx(View view)
{
EditText no = (EditText) findViewById(R.id.selection);
Double temp9=Double.parseDouble(no.getText().toString());

Double byx=1/temp9;
String ss=byx.toString();
no.setText(ss);

flag=true;
show=true;
}

public void sendMessageminus(View view)
{
EditText no = (EditText) findViewById(R.id.selection);

if(flag)
{
temp=Integer.parseInt(no.getText().toString());
flag=false;
show = true;
no.setText("");
opt='-';
}
else
{
   show = true;
temp2=Integer.parseInt(no.getText().toString());
tot=temp-temp2;
no.setText(tot.toString());
flag = true;
opt='-';
}
}
public void sendMessagemul(View view)
{
EditText no = (EditText) findViewById(R.id.selection);

if(flag)
{
temp=Integer.parseInt(no.getText().toString());
flag=false;
show = true;
no.setText("");
opt='*';
}
else
{
   show = true;
temp2=Integer.parseInt(no.getText().toString());
tot=temp*temp2;
no.setText(tot.toString());
flag = true;
opt='*';
}
}
public void sendMessagediv(View view)
{
EditText no = (EditText) findViewById(R.id.selection);

if(flag)
{
temp=Integer.parseInt(no.getText().toString());
flag=false;
show = true;
no.setText("");
opt='/';
}
else
{
   show = true;
temp2=Integer.parseInt(no.getText().toString());
tot=temp/temp2;
no.setText(tot.toString());
flag = true;
opt='/';
}
}

public void sendMessageC(View view)
{
EditText no = (EditText) findViewById(R.id.selection);
no.setText("");
flag=true;
show=true;
}
public void sendMessagedelete(View view)
{
EditText no = (EditText) findViewById(R.id.selection);
str=no.getText().toString();
s = str.substring(0, str.length() - 1);
no.setText(s);

}

public void sendMessageequal(View view)
{
EditText no = (EditText) findViewById(R.id.selection);
temp2=Integer.parseInt(no.getText().toString());

switch (opt)
{
case '+':
tot=temp+temp2;
show = true;
flag=true;
break;
case '-':
tot=temp-temp2;
flag=true;
show = true;
break;
case '*':
tot=temp*temp2;
flag=true;
show = true;
break;
case '/':
tot=temp/temp2;
show = true;
flag=true;
break;
}
no.setText(tot.toString());
}
}