Sei sulla pagina 1di 14

EXPERIMENT NO: 13

AIM: Install Android SDK (programming environment) and write a program to display
"hello world".

INTRODUCTION TO ANDROID SDK

To develop apps for Android devices, we use a set of tools that are included in the Android
SDK. Once the Android SDK is downloaded and installed, we can access these tools right
from the Eclipse IDE, through the ADT plugin or from the command line. Developing with
Eclipse is the preferred method because it can directly invoke the tools that we need while
developing applications.

INSTALLATION PROCEDURE OF ANDROID SDK

STEP 1: INSTALL THE JDK

Android requires the JDK "Java Development Kit" to compile java programs. Install the
version of the JDK appropriate for OS.

STEP 2: INSTALL IDE


Download a current stable build of Eclipse from the Eclipse Web Site. For the recommended
package to download and install, click the link Eclipse IDE for java EE Developers on the
Eclipse Packages page. There is no installer used to install Eclipse. The process described
below involves un-archiving a directory tree of files and placing it in an appropriate location
on the hard disk.
 Unzip the software into an appropriate location on hard disk.
 Once the unzipped files are located on the filesystem, get started using Eclipse.
 Run Eclipse by running C:\eclipse\eclipse.exe.
 The first time we run Eclipse, we will be prompted to identify a location for our
Eclipse workspace. This is where local copies of our projects will live on our file
system.

STEP 3: INSTALL THE ANDROID SDK


Download the latest module and pop it into the SDK. We can pick and choose which modules
to install.

SOURCE CODE FOR HELLO WORLD

MAINACTIVITY.JAVA
package com.hello_world;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

OUTPUT
This is how output appears on the android mobile device on successful compilation of the
source code.

CONCLUSION
We have successfully displayed hello world on android mobile device.
EXPERIMENT NO: 14

AIM: Write a program to implement a distance converter using buttons.

HARDWARE/SOFTWARE REQUIREMENTS: Android SDK , JDK , Android Handset,


Connecting Cable.

PROCEDURE:
1. Open Android SDK .
2. Start File -> New -> Android Application Project.
3. Write Application name, Project name, Package name
4. Continue clicking next by filling necessary details.
5. Click Finish.
6. Write source code.

SOURCE CODE:

MAINACTIVITY.JAVA

package com.example.experiment2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View v)
{
LinearLayout ll=(LinearLayout)findViewById(R.id.ll);
TextView result=(TextView)findViewById(R.id.result);
EditText et1=(EditText)findViewById(R.id.editText1);

//get value from edit text box and convert into double
double a=Double.parseDouble(String.valueOf(et1.getText()));
RadioButton cb=(RadioButton)findViewById(R.id.km);
RadioButton fb=(RadioButton)findViewById(R.id.ml);

//check which radio button is checked


if(cb.isChecked())
{
//change background colour
ll.setBackgroundColor(Color.YELLOW);
//display conversion
result.setText(m2k(a)+" miles");
//cb.setChecked(false);
fb.setChecked(true);
}
else
{
ll.setBackgroundColor(Color.CYAN);
result.setText(k2m(a)+" km");
//fb.setChecked(false);
cb.setChecked(true);
}
}

private double k2m(double c)


{
return (c*1.609344);
}

private double m2k(double f)


{
return (f*0.621372);
}
}

ACTIVITY CODE

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


<android.support.constraint.ConstraintLayout
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:id="@+id/a3434"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/km"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="km" />
<RadioButton
android:id="@+id/ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="miles" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="Convert"
android:textSize="30sp"
tools:ignore="OnClick" />
</LinearLayout>

</android.support.constraint.ConstraintLayout>

OUTPUT
This is how output appears on the android mobile device on successful compilation of the
source code.
CONCLUSION
We have successfully implemented distance converter on android mobile device.
EXPERIMENT NO: 15

AIM: Write a program to implement a calculator. Display result in different activity.

HARDWARE/SOFTWARE REQUIREMENTS: Android SDK , JDK , Android Handset,


Connecting Cable.

PROCEDURE:
1. Open Android SDK .
2. Start File -> New -> Android Application Project.
3. Write Application name, Project name, Package name
4. Continue clicking next by filling necessary details.
5. Click Finish.
6. Write source code.

SOURCE CODE:

MAIN ACTIVITY.JAVA

package com.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {


Button one, two, three, four, five, six, seven, eight, nine, div, sub, mul, add, zero, cancel,
equal;
EditText display;
int op1, op2;
String optr;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.android_main);
one= (Button)findViewById(R.id.one);
two= (Button)findViewById(R.id.two);
three= (Button)findViewById(R.id.three);
four= (Button)findViewById(R.id.four);
five= (Button)findViewById(R.id.five);
six= (Button)findViewById(R.id.six);
seven= (Button)findViewById(R.id.seven);
eight= (Button)findViewById(R.id.eight);
nine= (Button)findViewById(R.id.nine);
zero= (Button)findViewById(R.id.zero);
add= (Button)findViewById(R.id.add);
sub= (Button)findViewById(R.id.sub);
mul= (Button)findViewById(R.id.mul);
div= (Button)findViewById(R.id.div);
equal= (Button)findViewById(R.id.equal);
cancel=(Button)findViewById(R.id.cancel);
display= (EditText)findViewById(R.id.display);

try{
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
zero.setOnClickListener(this);
add.setOnClickListener(this);
sub.setOnClickListener(this);
mul.setOnClickListener(this);
div.setOnClickListener(this);
equal.setOnClickListener(this);
cancel.setOnClickListener(this);
}
catch (Exception e){}
}

public void operation(){


if (optr.equals("+")){
op2=Integer.parseInt(display.getText().toString());
display.setText("");
op1=op1+op2;
display.setText("Result:"+Integer.toString(op1));
}
else if (optr.equals("-")){
op2=Integer.parseInt(display.getText().toString());
display.setText("");
op1=op1-op2;
display.setText("Result:"+Integer.toString(op1));
}

else if (optr.equals("*")){
op2=Integer.parseInt(display.getText().toString());
display.setText("");
op1=op1*op2;
display.setText("Result:"+Integer.toString(op1));
}
else if (optr.equals("/")){
op2=Integer.parseInt(display.getText().toString());
display.setText("");
op1=op1/op2;
display.setText("Result:"+Integer.toString(op1));
}
}

public void onClick(android.view.View arg0) {


Editable str=display.getText();
switch(arg0.getId()){
case R.id.one:if (op2!=0){
op2=0;
display.setText("");
}
str=str.append(one.getText());
display.setText(str);
break;

case R.id.two:if (op2!=0){


op2=0;
display.setText("");
}
str=str.append(two.getText());
display.setText(str);
break;

case R.id.three:if (op2!=0){


op2=0;
display.setText("");
}
str=str.append(three.getText());
display.setText(str);
break;

case R.id.four:if (op2!=0){


op2=0;
display.setText("");
}
str=str.append(four.getText());
display.setText(str);
break;

case R.id.five:if (op2!=0){


op2=0;
display.setText("");
}
str=str.append(five.getText());
display.setText(str);
break;

case R.id.six:if (op2!=0){


op2=0;
display.setText("");
}
str=str.append(six.getText());
display.setText(str);
break;

case R.id.seven:if (op2!=0){


op2=0;
display.setText("");
}
str=str.append(seven.getText());
display.setText(str);
break;

case R.id.eight:if (op2!=0){


op2=0;
display.setText("");
}
str=str.append(eight.getText());
display.setText(str);
break;

case R.id.nine:if (op2!=0){


op2=0;
display.setText("");
}
str=str.append(nine.getText());
display.setText(str);
break;

case R.id.cancel:
op1=0;
op2=0;
display.setText("");
display.setHint("Perform Operation");
break;

case R.id.add:
optr="+";
if (op1==0){
op1=Integer.parseInt(display.getText().toString());
display.setText("");
}else if (op2!=0){
op2=0;
display.setText("");
}
else{
op2=Integer.parseInt(display.getText().toString());
display.setText("");
op1=op1+op2;
display.setText("Result:" + Integer.toString(op1));
}
break;

case R.id.sub:
optr="-";
if (op1==0){
op1=Integer.parseInt(display.getText().toString());
display.setText("");
}
else if (op2!=0){
op2=0;
display.setText("");
}
else{
op2=Integer.parseInt(display.getText().toString());
display.setText("");
op1=op1-op2;
display.setText("Result:"+Integer.toString(op1));
}
break;

case R.id.mul:
optr="*";
if (op1==0){
op1=Integer.parseInt(display.getText().toString());
display.setText("");
}
else if (op2!=0){
op2=0;
display.setText("");
}
else{
op2=Integer.parseInt(display.getText().toString());
display.setText("");
op1=op1*op2;
display.setText("Result:"+Integer.toString(op1));
}
break;

case R.id.div:
optr="/";
if (op1==0){
op1=Integer.parseInt(display.getText().toString());
display.setText("");
}
else if (op2!=0){
op2=0;
display.setText("");
}
else{
op2=Integer.parseInt(display.getText().toString());
display.setText("");
op1=op1/op2;
display.setText("Result:"+Integer.toString(op1));
}
break;

case R.id.equal:
if(!optr.equals(null)){
if(op2!=0){
if(optr.equals("+")){
display.setText("");
display.setText("Result:"+Integer.toString(op1));
}
else if(optr.equals("-")){
display.setText("");
display.setText("Result:"+Integer.toString(op1));
}

else if(optr.equals("*")){
display.setText("");
display.setText("Result:"+Integer.toString(op1));
}

else if(optr.equals("/")){
display.setText("");
display.setText("Result:"+Integer.toString(op1));
}
}
else{
operation();
}
}
break;
}
}

HOME.JAVA

package com.calculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Home extends Activity {


protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Thread th=new Thread(){
public void run(){
try{
sleep(2000);
}
catch (Exception e){
e.printStackTrace();
}
finally{
onPause();
startActivity (new
Intent("com.calculator.android_main"));
}
}
};
th.start();
}
public void onPause(){
super.onPause();
finish();
}
}

OUTPUT
This is how output appears on the android mobile device on successful compilation of the
source code.

CONCLUSION
We have successfully implemented calculator on android mobile device
INDEX
S.no Name Date Page No Sign

Potrebbero piacerti anche