You are on page 1of 67

EX NO: 1 DEVELOP AN APPLICATION USING GUI COMPONENTS

DATE : USING FONTS AND COLORS

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/purple_200"
android:text="MY APPLICATION"
android:textSize="30dp"
android:textAlignment="center"
android:textColor="@color/black"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-1dp" />
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="@color/black"
android:ems="10"
android:inputType="textPersonName"
android:text=""
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="77dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="147dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="@color/black"

311120205059 VIGNESH.G
android:layout_weight="1"
android:text="FONT INCREASE" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="@color/black"
android:layout_weight="1"
android:text="FONT DECREASE" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="218dp">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="@color/black"
android:layout_weight="1"
android:text="RED" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="@color/black"
android:layout_weight="1"
android:text="GREEN" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="@color/black"
android:layout_weight="1"
android:text="BLUE" />
</LinearLayout>
<LinearLayout

311120205059 VIGNESH.G
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="-2dp"
tools:layout_editor_absoluteY="321dp">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="30dp"
android:textAlignment="center"
android:textColor="@color/black"
android:text="" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Main Activity.java
package com.example.exp1;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView t;
Button b,b2,b3,b4,b5;
EditText e;
String a;
Integer c=30;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=findViewById(R.id.textView2);
b=findViewById(R.id.button);
b2=findViewById(R.id.button2);
b3=findViewById(R.id.button3);
b4=findViewById(R.id.button4);
b5=findViewById(R.id.button5);
e=findViewById(R.id.editTextTextPersonName2);
a=e.getText().toString();
b.setOnClickListener(new View.OnClickListener() {
@Override

311120205059 VIGNESH.G
public void onClick(View v) {
t.setText(a);
c=c+5;
t.setTextSize(c);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText(a);
c=c-5;
t.setTextSize(c);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText(a);
t.setTextColor(Color.RED);
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText(a);
t.setTextColor(Color.GREEN);
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText(a);
t.setTextColor(Color.BLUE);
}
});
}
}

311120205059 VIGNESH.G
OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 2 DEVELOP AN APPLICATION THAT USES LAYOUT
DATE : MANAGERS AND EVENT LISTENERS

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="INFO"
android:textSize="40dp"
android:textColor="@color/black"
android:textAlignment="center" />
<TextView
android:id="@+id/textView3"
android:layout_width="150dp"
android:layout_height="50dp"
android:textSize="30dp"
android:textColor="@color/black"
android:text="NAME" />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:inputType="textPersonName"
android:textSize="30dp"
android:text="" />
<TextView
android:id="@+id/textView5"
android:layout_width="150dp"
android:layout_height="50dp"
android:textSize="30dp"
android:textColor="@color/black"
android:text="MOBILE" />
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:inputType="textPersonName"

311120205059 VIGNESH.G
android:textSize="30dp"
android:text="" />
<TextView
android:id="@+id/textView6"
android:layout_width="150dp"
android:layout_height="50dp"
android:textSize="30dp"
android:textColor="@color/black"
android:text="D.O.B" />
<EditText
android:id="@+id/editTextTextPersonName3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:inputType="textPersonName"
android:textSize="30dp"
android:text="" />
<TextView
android:id="@+id/textView4"
android:layout_width="150dp"
android:layout_height="50dp"
android:textSize="30dp"
android:textColor="@color/black"
android:text="REG NO" />
<EditText
android:id="@+id/editTextTextPersonName4"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:inputType="textPersonName"
android:textSize="30dp"
android:text="" />
<Button
android:id="@+id/button"
android:layout_width="409dp"
android:layout_height="60dp"
android:textSize="30dp"
android:textColor="@color/black"
android:text="Button" />
</LinearLayout>

Main Activity.java
package com.example.myapplication4;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

311120205059 VIGNESH.G
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button b1;
TextView t1,t2,t3,t4;
EditText e1,e2,e3,e4;
String name,reg,dob,mob;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.button);
t1=findViewById(R.id.textView);
t2=findViewById(R.id.textView3);
t3=findViewById(R.id.textView4);
t4=findViewById(R.id.textView5);
e1=findViewById(R.id.editTextTextPersonName);
e2=findViewById(R.id.editTextTextPersonName2);
e3=findViewById(R.id.editTextTextPersonName3);
e4=findViewById(R.id.editTextTextPersonName4);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name=e1.getText().toString();
mob=e2.getText().toString();
dob=e3.getText().toString();
reg=e4.getText().toString();
Intent i=new Intent(MainActivity.this,MainActivity2.class);
i.putExtra("Name",name);
i.putExtra("Mob",mob);
i.putExtra("DOB",dob);
i.putExtra("Reg",reg);
startActivity(i);
}
});
}
}

Activity Main2.xml
<?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"

311120205059 VIGNESH.G
android:orientation="vertical"
tools:context=".MainActivity2">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="INFO"
android:textSize="40dp"
android:textColor="@color/black"
android:textAlignment="center" />
<TextView
android:id="@+id/textView7"
android:layout_width="150dp"
android:layout_height="50dp"
android:textSize="30dp"
android:textColor="@color/black"
android:text="Name" />
<TextView
android:id="@+id/textView11"
android:layout_width="261dp"
android:layout_height="50dp"
android:text=""
android:textSize="30dp"
android:textColor="@color/black"
android:textAlignment="center" />
<TextView
android:id="@+id/textView8"
android:layout_width="150dp"
android:layout_height="50dp"
android:textSize="30dp"
android:textColor="@color/black"
android:text="Mobile" />
<TextView
android:id="@+id/textView12"
android:layout_width="261dp"
android:layout_height="50dp"
android:text=""
android:textSize="30dp"
android:textColor="@color/black"
android:textAlignment="center" />
<TextView
android:id="@+id/textView9"
android:layout_width="150dp"
android:layout_height="50dp"
android:textSize="30dp"
android:textColor="@color/black"
android:text="Reg No" />

311120205059 VIGNESH.G
<TextView
android:id="@+id/textView13"
android:layout_width="261dp"
android:layout_height="50dp"
android:text=""
android:textSize="30dp"
android:textColor="@color/black"
android:textAlignment="center" />
<TextView
android:id="@+id/textView10"
android:layout_width="150dp"
android:layout_height="50dp"
android:textSize="30dp"
android:textColor="@color/black"
android:text="D.O.B" />
<TextView
android:id="@+id/textView14"
android:layout_width="261dp"
android:layout_height="50dp"
android:textSize="30dp"
android:textColor="@color/black"
android:text=""
android:textAlignment="center" />
</LinearLayout>

Main Activity2.java
package com.example.myapplication4;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
TextView t1,t2,t3,t4;
String n,m,r,d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
t1=findViewById(R.id.textView11);
t2=findViewById(R.id.textView12);
t3=findViewById(R.id.textView13);
t4=findViewById(R.id.textView14);
n=getIntent().getStringExtra("Name");
m=getIntent().getStringExtra("Mob");
r=getIntent().getStringExtra("Reg");
d=getIntent().getStringExtra("DOB");
t1.setText(n);
t2.setText(m);

311120205059 VIGNESH.G
t3.setText(r);
t4.setText(d);
}
}

OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 3 WRITE AN APPLICATION THAT DRAWS BASIC GRAPHICAL
DATE : PRIMITIVES ON SCREEN

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?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:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="431dp"
tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>

Main Activity.java
package com.example.exp3;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bg=Bitmap.createBitmap(720,1280,Bitmap.Config.ARGB_8888);
ImageView i=(ImageView) findViewById(R.id.imageView);
i.setBackgroundDrawable(new BitmapDrawable(bg));
Canvas canvas=new Canvas(bg);
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(60);
canvas.drawText("Rectangle",420,150,paint);
canvas.drawRect(400,200,650,700,paint);
canvas.drawText("Circle",120,150,paint);
311120205059 VIGNESH.G
canvas.drawCircle(200,350,150,paint);
canvas.drawText("Square",120,800,paint);
canvas.drawRect(50,850,350,1150,paint);
canvas.drawText("Line",480,800,paint);
canvas.drawLine(520,850,520,1150,paint);
}
}

OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 5 DEVELOP AN APPLICATION THAT MAKES USE OF
DATE : NOTIFICATION MANAGER

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?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:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Notification" />
</LinearLayout>

Main Activity.java
package com.example.exp5;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button notify;
EditText e;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

311120205059 VIGNESH.G
notify = (Button) findViewById(R.id.button);
e = (EditText) findViewById(R.id.editTextTextPersonName);
notify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Notification noti = new
Notification.Builder(MainActivity.this).setContentTitle("New
Message").setContentText(e.getText().toString()).setSmallIcon(R.mipmap.ic_launcher).setCo
ntentIntent(pending).build();
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(0, noti);
}
});
}
}

OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 4 DEVELOP AN APPLICATION THAT MAKES USE OF
DATE : DATABASE

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?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:orientation="vertical"
android:background="@color/cardview_light_background"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="DATABASE"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="25dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/textView2"
android:layout_width="150dp"
android:layout_height="match_parent"
android:text="NAME"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="15dp" />
<EditText
android:id="@+id/editTextTextPersonName4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:textSize="20dp"
android:inputType="textPersonName"
android:textAlignment="center"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
311120205059 VIGNESH.G
android:orientation="horizontal">
<TextView
android:id="@+id/textView7"
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="15dp"
android:text="REG NO" />
<EditText
android:id="@+id/editTextTextPersonName5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20dp"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView8"
android:layout_width="125dp"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="15dp"
android:layout_weight="1"
android:text="D.O.B" />
<EditText
android:id="@+id/editTextTextPersonName6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center"
android:textSize="20dp"
android:textColor="@color/black"
android:ems="10"
android:inputType="textPersonName" />

311120205059 VIGNESH.G
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView9"
android:layout_width="125dp"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="15dp"
android:layout_weight="1"
android:text="DEPT" />
<EditText
android:id="@+id/editTextTextPersonName7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center"
android:textColor="@color/black"
android:ems="10"
android:textSize="20dp"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView10"
android:layout_width="125dp"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="15dp"
android:layout_weight="1"
android:text="MARKS" />
<EditText
android:id="@+id/editTextTextPersonName8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="@color/black"

311120205059 VIGNESH.G
android:layout_weight="1"
android:ems="10"
android:textSize="20dp"
android:inputType="textPersonName" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="SAVE"
android:textAlignment="center"
android:textSize="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="DATA RETRIEVAL"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="25dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView12"
android:layout_width="125dp"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="15dp"
android:layout_weight="1"
android:text="NAME" />
<EditText
android:id="@+id/editTextTextPersonName9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="@color/black"
android:layout_weight="1"

311120205059 VIGNESH.G
android:textSize="20dp"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="RETRIEVE"
android:textAlignment="center"
android:textSize="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView14"
android:layout_width="13dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="MARKS"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="15dp" />
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20dp"
android:layout_weight="1"
android:text="" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

Main Activity.java
package com.example.exp5;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

311120205059 VIGNESH.G
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText name,reg,dob,dept,marks,name1;
TextView marks1;
String n,r,d,D,m;
Button b1,b2;
DBHelper dbh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.editTextTextPersonName4);
reg = findViewById(R.id.editTextTextPersonName5);
dob = findViewById(R.id.editTextTextPersonName6);
dept = findViewById(R.id.editTextTextPersonName7);
marks = findViewById(R.id.editTextTextPersonName8);
marks1=findViewById(R.id.textView15);
b1=findViewById(R.id.button);
b2=findViewById(R.id.button3);
name1=findViewById(R.id.editTextTextPersonName9);
dbh = new DBHelper(MainActivity.this);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
n=name.getText().toString();
r=reg.getText().toString();
d=dob.getText().toString();
D=dept.getText().toString();
m=marks.getText().toString();
dbh.savedat(n,r,d,D,m);
Toast.makeText(MainActivity.this,"Saved!",Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String r1 = name1.getText().toString();
String r2 = dbh.getmarks(r1);
marks1.setText(r2);
}
});
}
}

311120205059 VIGNESH.G
DBHelper.java
package com.example.exp5;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper{
SQLiteDatabase sqLiteDatabase;
public DBHelper(Context context) {
super(context, "Details.db", null, 1); // db name, version number
sqLiteDatabase=getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "create table DB (Name text ,Reg text,DOB text,Dept text,Marks text)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long savedat(String n,String r,String d,String D,String m) {
ContentValues contentValues = new ContentValues();
contentValues.put("Name",n);
contentValues.put("Reg",r);
contentValues.put("DOB",d);
contentValues.put("Dept",D);
contentValues.put("Marks",m);// sqlite db
long j = sqLiteDatabase.insert("DB",null,contentValues);
return j;
}
@SuppressLint("Range")
public String getmarks(String name) {
String s;
Cursor c = sqLiteDatabase.query("DB",null,"Name=?",new
String[]{name},null,null,null);
if(c.getCount()<1) {
return "not exist";
}
c.moveToFirst();
s = c.getString(c.getColumnIndex("Marks"));
return s;
}}

311120205059 VIGNESH.G
OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 6 IMPLEMENT AN APPLICATION THAT USES
DATE : MULTITHREADING

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?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:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="317dp"
tools:srcCompat="@tools:sample/avatars" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2" />
</LinearLayout>

Main Activity.java
package com.example.exp6;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity
{
ImageView img;
Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
311120205059 VIGNESH.G
setContentView(R.layout.activity_main);
bt1 = findViewById(R.id.button);
bt2= findViewById(R.id.button2);
img =findViewById(R.id.imageView);
bt1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
img.post(new Runnable()
{
@Override
public void run()
{
img.setImageResource(R.drawable.download);
}
});
}
}).start();
}
});
bt2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
img.post(new Runnable()
{
@Override
public void run()
{
img.setImageResource(R.drawable.download2);
}
});

311120205059 VIGNESH.G
}
}).start();
}
});
}
}

OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 7 IMPLEMENT AN APPLICATION THAT WRITES
DATE : DATA TO THE SD CARD

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?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:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""
tools:ignore="SpeakableTextPresentCheck" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read data" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Write data" />

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear" />
</LinearLayout>

Main Activity.java
package com.example.ex8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;

311120205059 VIGNESH.G
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText e1;
Button r,c,w;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText) findViewById(R.id.editTextTextPersonName);
r=(Button) findViewById(R.id.button);
w=(Button) findViewById(R.id.button2);
c=(Button) findViewById(R.id.button3);
r.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message;
String buf = "";
try
{
File f = new File("/sdcard/myfile.txt");
FileInputStream fin = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
while ((message = br.readLine()) != null)
{
buf += message;
}
e1.setText(buf);
br.close();
fin.close();
Toast.makeText(getBaseContext(),"Data Received from
SDCARD",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
});
w.setOnClickListener(new View.OnClickListener()
{

311120205059 VIGNESH.G
@Override
public void onClick(View v)
{
String message=e1.getText().toString();
try
{
File f=new File("/sdcard/myfile.txt");
f.createNewFile();
FileOutputStream fout=new FileOutputStream(f);
fout.write(message.getBytes());
fout.close();
Toast.makeText(getBaseContext(),"Data Written in
SDCARD",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{

Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}
});
c.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
e1.setText("");
}
});
}
}

311120205059 VIGNESH.G
OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 8 IMPLEMENT AN APPLICATION THAT CREATES AN
DATE : ALERT UPON RECEIVING A MESSAGE

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</LinearLayout>

Main Activity.java
package com.example.ex9;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button closeButton;
AlertDialog.Builder builder;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
closeButton = (Button) findViewById(R.id.button);
builder = new AlertDialog.Builder(this);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override

311120205059 VIGNESH.G
public void onClick(View v) {
//Setting message manually and performing action on button click
builder.setMessage("Do you want to close this application
?").setCancelable(false).setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
Toast.makeText(getApplicationContext(),"you choose yes action for
alertbox", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
Toast.makeText(getApplicationContext(),"you choose no action for
alertbox", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.setTitle("AlertDialogExample");
alert.show();
}
});
}
}

OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 9 WRITE A MOBILE APPLICATION THAT MAKES
DATE : USE OF RSS FEED

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?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" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Main Activity.java
package com.example.exp92;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ListActivity
{
List headlines;
List links;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
new MyAsyncTask().execute();
}
class MyAsyncTask extends AsyncTask<Object,Void,ArrayAdapter>

311120205059 VIGNESH.G
{
@Override
protected ArrayAdapter doInBackground(Object[] params)
{
headlines = new ArrayList();
links = new ArrayList();
try
{
URL url = new URL("https://codingconnect.net/feed");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title"))
{
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
}
else if (xpp.getName().equalsIgnoreCase("link"))
{
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}
else if(eventType==XmlPullParser.END_TAG &&
xpp.getName().equalsIgnoreCase("item"))
{
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
}

311120205059 VIGNESH.G
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ArrayAdapter adapter)
{
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,
headlines);
setListAdapter(adapter);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
Uri uri = Uri.parse((links.get(position)).toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
public InputStream getInputStream(URL url)
{
try
{
return url.openConnection().getInputStream();
}
catch (IOException e)
{
return null;
}
}
}

311120205059 VIGNESH.G
OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 10 DEVELOP AND MOBILE APPLICATION TO SEND
DATE : EMAIL

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical" >
<EditText
android:id="@+id/txtTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"/>
<EditText
android:id="@+id/txtSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
<EditText
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Message"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Send"
android:id="@+id/btnSend"/>
</LinearLayout>

Main Activity.java
package com.example.exno11;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

311120205059 VIGNESH.G
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText eTo;
private EditText eSubject;
private EditText eMsg;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eTo = (EditText)findViewById(R.id.txtTo);
eSubject = (EditText)findViewById(R.id.txtSub);
eMsg = (EditText)findViewById(R.id.txtMsg);
btn = (Button)findViewById(R.id.btnSend);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, new String[]{eTo.getText().toString()});
it.putExtra(Intent.EXTRA_SUBJECT,eSubject.getText().toString());
it.putExtra(Intent.EXTRA_TEXT,eMsg.getText());
it.setType("message/rfc822");
startActivity(Intent.createChooser(it,"Choose Mail App"));
}
});
}
}

Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.exno11" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name="com.example.exno11.MainActivity"
android:label="@string/app_name">

311120205059 VIGNESH.G
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="message/rfc822"/>
</intent-filter>
</activity>
</application>
</manifest>

OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 11 DEVELOP A NATIVE APPLICATION THAT USES GPS
DATE : LOCATION INFORMATION

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Activity Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="139dp"
tools:layout_editor_absoluteY="88dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Your GPS Location" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Latitude" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" android:text=""
android:textSize="25dp"
android:hint="Press button below" />
<TextView
android:id="@+id/textView3" android:layout_width="match_parent"
android:layout_height="wrap_content" android:textSize="20dp"
android:text="Longitude" />
<EditText
android:id="@+id/editText2" android:layout_width="match_parent"
311120205059 VIGNESH.G
android:layout_height="wrap_content" android:ems="10"
android:inputType="textPersonName" android:text=""
android:textSize="25dp"
android:hint="Press button below" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check Location" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Main Activity.java
package com.example.exp10;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity 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("")) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=

311120205059 VIGNESH.G
PackageManager.PERMISSION_GRANTED) {
return;
}
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) {
EditText e1 = (EditText) findViewById(R.id.editText);
EditText e2 = (EditText) findViewById(R.id.editText2);
Button b1 = (Button) findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b1.setText("Check Location Once Again");
e1.setText("" + arg0.getLatitude());
e2.setText("" + arg0.getLongitude());
}
});
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}

311120205059 VIGNESH.G
OUTPUT:

RESULT:

311120205059 VIGNESH.G
EX NO: 12 DEVELOP A MOBILE APPLICATION FOR SIMPLE
DATE : NEEDS(MINI PROJECT)

SNAKE GAME

AIM:

PROCEDURE:

311120205059 VIGNESH.G
PROGRAM:
Snake Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.android.snake.SnakeView
android:id="@+id/snake"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tileSize="24"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text"
android:text="@string/snake_layout_text_text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#ff8888ff"
android:textSize="24sp"/>
</RelativeLayout>
</FrameLayout>

Snake.java
package com.example.android.snake;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class Snake extends Activity {
private SnakeView mSnakeView;
private static String ICICLE_KEY = "snake-view";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.snake_layout);
mSnakeView = (SnakeView) findViewById(R.id.snake);
mSnakeView.setTextView((TextView) findViewById(R.id.text));
311120205059 VIGNESH.G
if (savedInstanceState == null) {
mSnakeView.setMode(SnakeView.READY);
} else {
Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
if (map != null) {
mSnakeView.restoreState(map);
} else {
mSnakeView.setMode(SnakeView.PAUSE);
}
}
}
@Override
protected void onPause() {
super.onPause();
mSnakeView.setMode(SnakeView.PAUSE);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
}
}

SnakeView.java
package com.example.android.snake;
import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
public class SnakeView extends TileView {
private static final String TAG = "SnakeView";
private int mMode = READY;
public static final int PAUSE = 0;
public static final int READY = 1;
public static final int RUNNING = 2;
public static final int LOSE = 3;
private int mDirection = NORTH;

311120205059 VIGNESH.G
private int mNextDirection = NORTH;
private static final int NORTH = 1;
private static final int SOUTH = 2;
private static final int EAST = 3;
private static final int WEST = 4;
private static final int RED_STAR = 1;
private static final int YELLOW_STAR = 2;
private static final int GREEN_STAR = 3;
private long mScore = 0;
private long mMoveDelay = 600;
private long mLastMove;
private TextView mStatusText;
private ArrayList<Coordinate> mSnakeTrail = new ArrayList<Coordinate>();
private ArrayList<Coordinate> mAppleList = new ArrayList<Coordinate>();
private static final Random RNG = new Random();
private RefreshHandler mRedrawHandler = new RefreshHandler();
class RefreshHandler extends Handler {
@Override
public void handleMessage(Message msg) {
SnakeView.this.update();
SnakeView.this.invalidate();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
public SnakeView(Context context, AttributeSet attrs) {
super(context, attrs);
initSnakeView();
}
public SnakeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initSnakeView();
}
private void initSnakeView() {
setFocusable(true);
Resources r = this.getContext().getResources();
resetTiles(4);
loadTile(RED_STAR, r.getDrawable(R.drawable.redstar));
loadTile(YELLOW_STAR, r.getDrawable(R.drawable.yellowstar));
loadTile(GREEN_STAR, r.getDrawable(R.drawable.greenstar));
}

311120205059 VIGNESH.G
private void initNewGame() {
mSnakeTrail.clear();
mAppleList.clear();
mSnakeTrail.add(new Coordinate(7, 7));
mSnakeTrail.add(new Coordinate(6, 7));
mSnakeTrail.add(new Coordinate(5, 7));
mSnakeTrail.add(new Coordinate(4, 7));
mSnakeTrail.add(new Coordinate(3, 7));
mSnakeTrail.add(new Coordinate(2, 7));
mNextDirection = NORTH;
addRandomApple();
addRandomApple();
mMoveDelay = 600;
mScore = 0;
}
private int[] coordArrayListToArray(ArrayList<Coordinate> cvec) {
int count = cvec.size();
int[] rawArray = new int[count * 2];
for (int index = 0; index < count; index++) {
Coordinate c = cvec.get(index);
rawArray[2 * index] = c.x;
rawArray[2 * index + 1] = c.y;
}
return rawArray;
}
public Bundle saveState() {
Bundle map = new Bundle();
map.putIntArray("mAppleList", coordArrayListToArray(mAppleList));
map.putInt("mDirection", Integer.valueOf(mDirection));
map.putInt("mNextDirection", Integer.valueOf(mNextDirection));
map.putLong("mMoveDelay", Long.valueOf(mMoveDelay));
map.putLong("mScore", Long.valueOf(mScore));
map.putIntArray("mSnakeTrail", coordArrayListToArray(mSnakeTrail));
return map;
}
private ArrayList<Coordinate> coordArrayToArrayList(int[] rawArray) {
ArrayList<Coordinate> coordArrayList = new ArrayList<Coordinate>();
int coordCount = rawArray.length;
for (int index = 0; index < coordCount; index += 2) {
Coordinate c = new Coordinate(rawArray[index], rawArray[index + 1]);
coordArrayList.add(c);
}
return coordArrayList;
}

311120205059 VIGNESH.G
public void restoreState(Bundle icicle) {
setMode(PAUSE);
mAppleList = coordArrayToArrayList(icicle.getIntArray("mAppleList"));
mDirection = icicle.getInt("mDirection");
mNextDirection = icicle.getInt("mNextDirection");
mMoveDelay = icicle.getLong("mMoveDelay");
mScore = icicle.getLong("mScore");
mSnakeTrail = coordArrayToArrayList(icicle.getIntArray("mSnakeTrail"));
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
if (mMode == READY | mMode == LOSE) {
initNewGame();
setMode(RUNNING);
update();
return (true);
}
if (mMode == PAUSE) {
setMode(RUNNING);
update();
return (true);
}
if (mDirection != SOUTH) {
mNextDirection = NORTH;
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
if (mDirection != NORTH) {
mNextDirection = SOUTH;
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
if (mDirection != EAST) {
mNextDirection = WEST;
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
if (mDirection != WEST) {
mNextDirection = EAST;
}

311120205059 VIGNESH.G
return (true);
}
return super.onKeyDown(keyCode, msg);
}
public void setTextView(TextView newView) {
mStatusText = newView;
}
public void setMode(int newMode) {
int oldMode = mMode;
mMode = newMode;
if (newMode == RUNNING & oldMode != RUNNING) {
mStatusText.setVisibility(View.INVISIBLE);
update();
return;
}
Resources res = getContext().getResources();
CharSequence str = "";
if (newMode == PAUSE) {
str = res.getText(R.string.mode_pause);
}
if (newMode == READY) {
str = res.getText(R.string.mode_ready);
}
if (newMode == LOSE) {
str = res.getString(R.string.mode_lose_prefix) + mScore
+ res.getString(R.string.mode_lose_suffix);
}
mStatusText.setText(str);
mStatusText.setVisibility(View.VISIBLE);
}
private void addRandomApple() {
Coordinate newCoord = null;
boolean found = false;
while (!found) {
int newX = 1 + RNG.nextInt(mXTileCount - 2);
int newY = 1 + RNG.nextInt(mYTileCount - 2);
newCoord = new Coordinate(newX, newY);
boolean collision = false;
int snakelength = mSnakeTrail.size();
for (int index = 0; index < snakelength; index++) {
if (mSnakeTrail.get(index).equals(newCoord)) {
collision = true;
}
}

311120205059 VIGNESH.G
found = !collision;
}
if (newCoord == null) {
Log.e(TAG, "Somehow ended up with a null newCoord!");
}
mAppleList.add(newCoord);
}
public void update() {
if (mMode == RUNNING) {
long now = System.currentTimeMillis();
if (now - mLastMove > mMoveDelay) {
clearTiles();
updateWalls();
updateSnake();
updateApples();
mLastMove = now;
}
mRedrawHandler.sleep(mMoveDelay);
}
}
private void updateWalls() {
for (int x = 0; x < mXTileCount; x++) {
setTile(GREEN_STAR, x, 0);
setTile(GREEN_STAR, x, mYTileCount - 1);
}
for (int y = 1; y < mYTileCount - 1; y++) {
setTile(GREEN_STAR, 0, y);
setTile(GREEN_STAR, mXTileCount - 1, y);
}
}
private void updateApples() {
for (Coordinate c : mAppleList) {
setTile(YELLOW_STAR, c.x, c.y);
}
}
private void updateSnake() {
boolean growSnake = false;
Coordinate head = mSnakeTrail.get(0);
Coordinate newHead = new Coordinate(1, 1);
mDirection = mNextDirection;
switch (mDirection) {
case EAST: {
newHead = new Coordinate(head.x + 1, head.y);
break;

311120205059 VIGNESH.G
}
case WEST: {
newHead = new Coordinate(head.x - 1, head.y);
break;
}
case NORTH: {
newHead = new Coordinate(head.x, head.y - 1);
break;
}
case SOUTH: {
newHead = new Coordinate(head.x, head.y + 1);
break;
}
}
if ((newHead.x < 1) || (newHead.y < 1) || (newHead.x > mXTileCount - 2)
|| (newHead.y > mYTileCount - 2)) {
setMode(LOSE);
return;
}
int snakelength = mSnakeTrail.size();
for (int snakeindex = 0; snakeindex < snakelength; snakeindex++) {
Coordinate c = mSnakeTrail.get(snakeindex);
if (c.equals(newHead)) {
setMode(LOSE);
return;
}
}
int applecount = mAppleList.size();
for (int appleindex = 0; appleindex < applecount; appleindex++) {
Coordinate c = mAppleList.get(appleindex);
if (c.equals(newHead)) {
mAppleList.remove(c);
addRandomApple();
mScore++;
mMoveDelay *= 0.9;
growSnake = true;
}
}
mSnakeTrail.add(0, newHead);
if (!growSnake) {
mSnakeTrail.remove(mSnakeTrail.size() - 1);
}
int index = 0;
for (Coordinate c : mSnakeTrail) {

311120205059 VIGNESH.G
if (index == 0) {
setTile(YELLOW_STAR, c.x, c.y);
} else {
setTile(RED_STAR, c.x, c.y);
}
index++;
}
}
private class Coordinate {
public int x;
public int y;
public Coordinate(int newX, int newY) {
x = newX;
y = newY;
}
public boolean equals(Coordinate other) {
if (x == other.x && y == other.y) {
return true;
}
return false;
}
@Override
public String toString() {
return "Coordinate: [" + x + "," + y + "]";
}
}
}

TileView.java
package com.example.android.snake;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
public class TileView extends View {
protected static int mTileSize;
protected static int mXTileCount;
protected static int mYTileCount;
private static int mXOffset;
private static int mYOffset;

311120205059 VIGNESH.G
private Bitmap[] mTileArray;
private int[][] mTileGrid;
private final Paint mPaint = new Paint();
public TileView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
a.recycle();
}
public TileView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
a.recycle();
}
public void resetTiles(int tilecount) {
mTileArray = new Bitmap[tilecount];
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mXTileCount = (int) Math.floor(w / mTileSize);
mYTileCount = (int) Math.floor(h / mTileSize);
mXOffset = ((w - (mTileSize * mXTileCount)) / 2);
mYOffset = ((h - (mTileSize * mYTileCount)) / 2);
mTileGrid = new int[mXTileCount][mYTileCount];
clearTiles();
}
public void loadTile(int key, Drawable tile) {
Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tile.setBounds(0, 0, mTileSize, mTileSize);
tile.draw(canvas);
mTileArray[key] = bitmap;
}
public void clearTiles() {
for (int x = 0; x < mXTileCount; x++) {
for (int y = 0; y < mYTileCount; y++) {
setTile(0, x, y);
}
}
}
public void setTile(int tileindex, int x, int y) {
mTileGrid[x][y] = tileindex;

311120205059 VIGNESH.G
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int x = 0; x < mXTileCount; x += 1) {
for (int y = 0; y < mYTileCount; y += 1) {
if (mTileGrid[x][y] > 0) {
canvas.drawBitmap(mTileArray[mTileGrid[x][y]],
mXOffset + x * mTileSize,
mYOffset + y * mTileSize,
mPaint);
}
}
}
}
}

OUTPUT:

311120205059 VIGNESH.G
RESULT:

311120205059 VIGNESH.G

You might also like