Mad File
Mad File
PRACTICALS
Experiment
No.
1. To study Android Studio and android studio installation. Create
―Hello World application.
3. Design simple GUI application with activity and intents e.g.
calculator.
7. Write an application that draws basic graphical primitives on the
screen
5. Develop a native application that uses GPS location information
9. Design a gaming application
PROCEDURE:
1. Open Eclipse IDE.
2. Create the project Ex_No_1.
3. Go to package explorer in the left hand side. Select the project Ex_No_1.
4. Go to res folder and select layout. Double click the activity_main.xml file.
5. Now you can see the Graphical layout window.
6. Drag and drop the Textview.
7. Again go to package explorer in the left hand side. Select the project Ex_No_1.
8. Go to src folder. Double click the MainActivity.java file.
9. In java file write the activities done by the application such as, actions of button.
10. Finally run the Android application
PROGRAM:
Acitivity_main.xml:
?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayou
t
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
EXPERIMENT-3
Design simple GUI application with activity and intents e.g. calculator
AIM: Design simple GUI application with activity and intents e.g. calculator
PROCEDURE:
1. Open Eclipse IDE.
2. Create the project Ex_No_3.
3. Go to package explorer in the left hand side. Select the project Ex_No_3.
4. Go to res folder and select layout. Double click the activity_main.xml file.
5. Now you can see the Graphical layout window.
6. Drag and drop the following components:
a. Two EditTexts with hints. Enter the first number and enter the second number
b. Four Buttons with labeled as ADD, SUB, MUL and DIV
7. Again go to package explorer in the left hand side. Select the project Ex_No_3.
8. Go to src folder. Double click the MainActivity.java file.
9. In java file write the activities done by the application such as, actions of button.
10. Finally run the Android application
PROGRAM:
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="com.example.ex_no_3.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Enter the first number"
tools:ignore="TextFields,HardcodedText" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:ems="10"
android:hint="Enter the second number"
tools:ignore="TextFields,HardcodedText" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button3"
android:text="DIV"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText2"
android:text="ADD"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:text="SUB"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2"
android:text="MUL"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
MainActivity.java:
package com.example.ex_no_3;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle; import
android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity
{ int n1,n2;
float num1,num2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText e1=(EditText)findViewById(R.id.editText1);
final EditText e2=(EditText)findViewById(R.id.editText2);
Button b1=(Button)findViewById(R.id.button1); Button
b2=(Button)findViewById(R.id.button2);
Button b3=(Button)findViewById(R.id.button3);
Button b4=(Button)findViewById(R.id.button4);
final TextView t=(TextView)findViewById(R.id.textView1);
b1.setOnClickListener(
new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
n1=Integer.parseInt(e1.getText().toString());
n2=Integer.parseInt(e2.getText().toString());
t.setText(e1.getText().toString()+"+"+e2.getText().toString()+" = "+(n1+n2));
}
});
b2.setOnClickListener(
new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
n1=Integer.parseInt(e1.getText().toString());
n2=Integer.parseInt(e2.getText().toString());
t.setText(e1.getText().toString()+"-"+e2.getText().toString()+"=
"+(n1-n2));
}
});
b3.setOnClickListener(
new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
n1=Integer.parseInt(e1.getText().toString());
n2=Integer.parseInt(e2.getText().toString());
t.setText(e1.getText().toString()+"*"+e2.getText().toString()+" =
"+(n1*n2));
}
});
b4.setOnClickListener(
new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
num1=Float.parseFloat(e1.getText().toString());
num2=Float.parseFloat(e2.getText().toString());
t.setText(e1.getText().toString()+"/"+e2.getText().toString()+" =
"+(num1/num2));
}
});
}
}
OUTPUT
EXPERIMENT-7
AIM: Write an application that draws basic graphical primitives on the screen
PROCEDURE:
1. Open Eclipse IDE.
2. Create the project Ex_no_5.
3. Go to package explorer in the left hand side. Select the project Ex_no_5.
4. Go to res folder and select layout. Double click the activity_main.xml file.
5. Now you can see the Graphical layout window.
6. Drag and drop only one ImageView
7. Again go to package explorer in the left hand side. Select the project Ex_No_5.
8. Go to src folder. Double click the MainActivity.java file.
9. In java file write the activities done by the application such as drawing the graphical primitives.
10. Finally run the android application.
PROGRAMS:
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="com.example.ex_no_5.MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher"
tools:ignore="ContentDescription" />
</RelativeLayout>
MainActivity.java:
package com.example.ex_no_5;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
@SuppressLint("ClickableViewAccessibility")
public class MainActivity extends ActionBarActivity implements OnTouchListener
{ ImageView iv;
Bitmap b;
Canvas c;
Paint p;
float dx=0,dy=0,ux=0,uy=0;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)this.findViewById(R.id.imageView1);
Display d = getWindowManager().getDefaultDisplay();
float dw = d.getWidth();
float dh = d.getHeight();
b = Bitmap.createBitmap((int) dw, (int) dh,Bitmap.Config.ARGB_8888);
c = new Canvas(b);
p = new Paint();
p.setColor(Color.BLUE);
iv.setImageBitmap(b);
iv.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method
stub int action = event.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
dx = event.getX();
dy = event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
ux = event.getX(); uy =
event.getY();
c.drawLine(dx, dy, ux, uy, p);
iv.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
}
OUTPUT:
EXPERIMENT-5
PROCEDURE:
1. Open Eclipse IDE.
2. Create the project Ex_No_7.
3. Go to package explorer in the left hand side. Select the project Ex_No_7.
4. Go to res folder and select layout. Double click the activity_main.xml file.
5. Now you can see the Graphical layout window.
6. Drag and drop the following components:
a. One TextView with text as Current Location
b. Two TextViews without any texts.
7. Again go to package explorer in the left hand side. Select the project Ex_No_7.
8. Go to src folder. Double click the MainActivity.java file.
9. In java file write the activities done by the application such as finding current location and print them.
10. Get the following permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
11. Finally run the android application.
PROGRAMS:
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="com.example.ex_no_7.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="114dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="51dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:text="Current Location"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
</RelativeLayout>
MainActivity.java:
package com.example.ex_no_7;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements
LocationListener{ @Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria c=new Criteria();
String s=lm.getBestProvider(c, false);
if(s!=null && !s.equals(""))
{
Location l=lm.getLastKnownLocation(s);
lm.requestLocationUpdates(s, 20000, 1, this);
if(l!=null)
onLocationChanged(l);
else
Toast.makeText(getApplicationContext(), "Location can't be
retrieved !!!", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(), "Provider not found
!!!", Toast.LENGTH_LONG).show();
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
TextView t1=(TextView)findViewById(R.id.textView1);
t1.setText("Latitude : \n"+arg0.getLatitude());
TextView t2=(TextView)findViewById(R.id.textView2);
t2.setText("Longitude : \n"+arg0.getLongitude());
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2)
{ // TODO Auto-generated method stub
}
}
OUTPUT
EXPERIMENT-9
Styles.xml
<resources>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="@drawable/splash"
tools:context="net.simplifiedcoding.simplegame.MainActivity">
<ImageButton
android:id="@+id/buttonPlay"
android:background="@drawable/playnow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/buttonScore"
android:layout_centerHorizontal="true" />
<ImageButton
android:id="@+id/buttonScore"
android:background="@drawable/highscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
When we tap the Play Now button our Game Activity will start.
Now come inside MainActivity.java and write the following code.
MainActivity.java
package net.simplifiedcoding.simplegame;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
//image button
private ImageButton buttonPlay;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public void onClick(View v) {
GameView.java
//Class constructor
public GameView(Context context) {
super(context);
@Override
public void run() {
while (playing) {
//to update the frame
update();
//to control
control();
}
}
The above class is our GameView class. It is the actual game panel where we will play the game.
The class is implementing Runnable interface. We have a volatile boolean type variable running
that will track whether the game is running or not. After that we have our gameThread, it is the
main game loop. Then we have the constructor to the class. We are not doing anything inside the
constructor right now. Then we have the overriden method run(), here we are running a loop
until the playing variable running is true. Inside the loop we are calling the following methods.
update() -> Here we will update the coordinate of our characters.
draw() -> Here we will draw the characters to the canvas.
control() -> This method will control the frames per seconds drawn. Here we are calling the delay
method of Thread. And this is actually making our frame rate to aroud 60fps.
After these we have two more methods.
pause() -> To pause the game, we are stopping the gameThread here.
resume() -> To resume the game, here we are starting the gameThread.
GameActivity.java
package net.simplifiedcoding.spacefighter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
//declaring gameview
private GameView gameView;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
//adding it to contentview
setContentView(gameView);
}
Player.java
package net.simplifiedcoding.spacefighter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class Player {
//Bitmap to get character from image
private Bitmap bitmap;
//coordinates
private int x;
private int y;
//constructor
public Player(Context context) {
x = 75;
y = 50;
speed = 1;
/*
* These are getters you can generate it autmaticallyl
* right click on editor -> generate -> getters
* */
public Bitmap getBitmap() {
return bitmap;
}
Drawing Player to GameView: To draw the player to our GameView you need to come back to
the GameView.java class and modify it as below.
GameView.java
@Override
public void run() {
while (playing) {
update();
draw();
control();
}
}
Player.java
GameActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
//adding it to contentview
setContentView(gameView);
}
Now to complete adding the boosters come inside GameView.java file and modify the onTouchEvent()
as follows.
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK)
{ case MotionEvent.ACTION_UP:
//stopping the boosting when screen is released
player.stopBoosting();
break;
case MotionEvent.ACTION_DOWN:
//boosting the space jet when screen is pressed
player.setBoosting();
break;
}
return true;
}
Now we will add background stars to make the background looks animating.
package net.simplifiedcoding.spacefighter;
import java.util.Random;
GameView.java
surfaceHolder = getHolder();
paint = new Paint();
@Override
public void run() {
while (playing) {
update();
draw();
control();
}
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK)
{ case MotionEvent.ACTION_UP:
player.stopBoosting();
break;
case MotionEvent.ACTION_DOWN:
player.setBoosting();
break;
}
return true;
}
}
Create a new java class named Enemy and write the following code.
package net.simplifiedcoding.spacefighter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import java.util.Random;
//min and max coordinates to keep the enemy inside the screen
private int maxX;
private int minX;
We need to add the enemies in the GameView now. So come inside GameView.java and modify the
code as follows.
@Override
public void run() {
while (playing) {
update();
draw();
control();
}
}
paint.setColor(Color.WHITE);
canvas.drawBitmap(
player.getBitmap(),
player.getX(),
player.getY(),
paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK)
{ case MotionEvent.ACTION_UP:
player.stopBoosting();
break;
case MotionEvent.ACTION_DOWN:
player.setBoosting();
break;
}
return true;
}
}
Detecting Collision
public class Enemy {
private Bitmap bitmap;
private int x;
private int y;
private int speed = 1;
private int maxX;
private int minX;
//Adding the top, left, bottom and right to the rect object
detectCollision.left = x;
detectCollision.top = y;
detectCollision.right = x + bitmap.getWidth();
detectCollision.bottom = y + bitmap.getHeight();
}
//getters
public Bitmap getBitmap() {
return bitmap;
}
Player.java
y -= speed + GRAVITY;
if (y < minY) {
y = minY;
}
if (y > maxY) {
y = maxY;
}
Now to complete the collision detection, again to inside GameView.java file and modify the update()
method as follows.
private void update() {
player.update();
for (Star s : stars) {
s.update(player.getSpeed());
}
Boom.java
package net.simplifiedcoding.spacefighter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
//bitmap object
private Bitmap bitmap;
//coordinate variables
private int x;
private int y;
//constructor
public Boom(Context context) {
//getting boom image from drawable resource
bitmap = BitmapFactory.decodeResource
(context.getResources(), R.drawable.boom);
//getters
public Bitmap getBitmap() {
return bitmap;
}
Now again come inside GameView.java file and modify the code as follow.
surfaceHolder = getHolder();
paint = new Paint();
@Override
public void run() {
while (playing) {
update();
draw();
control();
}
}
enemies[i].setX(-200);
}
}
}
paint.setColor(Color.WHITE);
canvas.drawBitmap(
player.getBitmap(),
player.getX(),
player.getY(),
paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK)
{ case MotionEvent.ACTION_UP:
player.stopBoosting();
break;
case MotionEvent.ACTION_DOWN:
player.setBoosting();
break;
}
return true;
}
}
Now again execute the application and you will see a blast effect on collision.