You are on page 1of 75

DR. B.R.

AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY, JALANDHAR

NAME – Anshika Singhal


ROLL NO. – 19124013
GROUP – G1
BRANCH – Information Technology
SUBJECT – Mobile Application Technology
SUBMITTED TO – Miss Vanita

1
2
INDEX

S. No. Name of Experiment Date Page No. Remark


1 Installation of J2ME 18/01/2022 04 - 09

2 Working with J2ME Features 25/01/2022 10 - 27

3 Developing application that uses 01/02/2022 28 - 32


GUI components, Font and colors

4 Developing application that 08/02/2022 34 - 41


uses Layout Managers and event
listeners.

5 Develop a native calculator 15/02/2022 42 - 50


application

6 Write an application that draws 22/02/2022 52 - 55


basic graphical primitives on the
screen

7 Develop an application that 08/03/2022 56 - 60


makes use of database.

8 Develop an application that 28/03/2022 62 - 66


makes use of RSS Feed.

9 Implement an application that 02/04/2022 68 - 73


implements multi-
threading

10 Develop a native application that 04/04/2022 74 - 78


uses GPS location information.

3
4
LAB – 1

Steps for installation of Android Studio on Window 11 :

5
Step 1 : To download the Android Studio, visit the official Android Studio website in your web
browser.

Step 2 : Click on the "Download Android Studio" option.

Step 3 : Double click on the downloaded "Android Studio-ide.exe" file after it is


downloaded.

6
Step 4 : "Android Studio Setup" will appear on the screen and click "Next" to
proceed.

Step 5 : Select the components that you want to install and click on the "Next"
button.

7
Step 6 : Now, browse the location where you want to install the Android Studio and
click "Next" to proceed.

Step 7 : Choose a start menu folder for the "Android Studio" shortcut and click the
"Install" button to proceed.

8
Step 8 : After the successful completion of the installation, click on the "Next"
button.

Step 9 : Click on the "Finish" button to proceed.

9
10
LAB - 2
Working with J2ME Features

a. Create a program which creates a menu


b. Event Handling
c. Input Checking

11
import javax.microedition.midlet.*; import

javax.microedition.lcdui.*;

public class MenuEvents extends MIDlet implements CommandListener,


ItemStateListener { public ChoiceGroup ch; public ChoiceGroup ch1;
public Form form; public Form form1; public Display display;

public Command View;

public Command Exit;

public Command Back;

public StringItem options;

public Item item;

public MenuEvents() { display = Display.getDisplay(this);

form = new Form(""); form1 = new Form("Selcted Options are");

ch = new ChoiceGroup("Preferences", Choice.MULTIPLE);

ch.append("cut", null);

ch.append("copy", null);

ch.append("paste", null);

ch.append("delete", null);

ch.setSelectedIndex(1, true);

form.append(ch);

ch1 = new ChoiceGroup("", Choice.EXCLUSIVE);

ch1.append("select all", null);

ch1.append("unselect all", null);

ch1.setSelectedIndex(1, true); form.append(ch1);

View = new Command("View", Command.OK, 1);

12
Exit = new Command("Exit", Command.EXIT, 1); Back

= new Command("Back", Command.BACK, 1);

form.addCommand(View); form.addCommand(Exit);

form1.addCommand(Back);

form.setCommandListener(this);

form1.setCommandListener(this);

form.setItemStateListener(this);

public void startApp()

{ display.setCurrent(form);

public void pauseApp() {

public void destroyApp(boolean unconditional) {

public void commandAction(Command command, Displayable displayable)

{ if (displayable == form) { if (command

== View) { boolean opt[] = new

boolean[ch.size()]; options = new

StringItem("", ""); String values = "";

ch.getSelectedFlags(opt);

options.setText(""); for (int i = 0; i <

opt.length; i++) {

13
if (opt[i]) {

values += ch.getString(i) + "\n";

options.setText(values);

form1.append(options); display.setCurrent(form1);

} else if (command == Exit) {

destroyApp(true);

notifyDestroyed();

} else if (displayable == form1) {

if (command == Back)

{ display.setCurrent(form);

options.setText("");

public void itemStateChanged(Item item) {

if (item == ch1) {

int i = 0; int size = ch.size();

while (i < size) { if

(ch1.getSelectedIndex() == 0)

ch.setSelectedIndex(i, true);

else

14
ch.setSelectedIndex(i, false);

i++;

Output :

15
16
17
c. Input Checking

a) J2me code :
import javax.microedition.lcdui.*; import javax.microedition.midlet.*;

public class EventEx1 extends MIDlet implements CommandListener {

// display manager

Display display = null;

// a menu with items

List menu = null; // main menu

// textbox

TextBox input = null;

// command

static final Command backCommand = new Command("Back",


Command.BACK, 0);

static final Command mainMenuCommand = new Command("Main", Command.SCREEN,


1);

18
static final Command exitCommand = new Command("Exit",

Command.STOP, 2);

String currentMenu = null;

// constructor.
public EventEx1() {

/**

* Start the MIDlet by creating a list of items and associating the * exit
command with it.

*/

public void startApp() throws MIDletStateChangeException { display =


Display.getDisplay(this);

menu = new List("Menu Items", Choice.IMPLICIT);


menu.append("Item1", null); menu.append("Item2", null);
menu.append("Item3", null); menu.append("Item4", null);
menu.addCommand(exitCommand);
menu.setCommandListener(this);

mainMenu();

public void pauseApp() {


display = null; menu =
null; input = null;

public void destroyApp(boolean unconditional) { notifyDestroyed();

// main menu

void mainMenu() {

display.setCurrent(menu); currentMenu =
"Main";

19
/**

* a generic method that will be called when selected any of * the items on
the list.
*/

public void prepare() { input = new TextBox("Enter some text: ", "", 5,
TextField.ANY); input.addCommand(backCommand);
input.setCommandListener(this); input.setString("");
display.setCurrent(input);

/**

* Test item1.
*/

public void testItem1() { prepare();

currentMenu = "item1";

/**

* Test item2.
*/

public void testItem2() { prepare();

currentMenu = "item2";

/**

* Test item3.
*/

public void testItem3() { prepare();

currentMenu = "item3";

/**

* Test item4.
*/

20
public void testItem4() { prepare();

currentMenu = "item4";

/**

* Handle events.
*/

public void commandAction(Command c, Displayable d) { String


label = c.getLabel(); if (label.equals("Exit"))
{ destroyApp(true);

} else if (label.equals("Back")) { if(currentMenu.equals("item1") ||


currentMenu.equals("item2") || currentMenu.equals("item3") ||
currentMenu.equals("item4")) {

// go back to menu
mainMenu();

} else {

List down = (List)display.getCurrent();


switch(down.getSelectedIndex()) { case 0: testItem1();break; case
1: testItem2();break; case 2: testItem3();break; case 3:
testItem4();break;

}}}

OUTPUT :

21
b),c) j2me code:
import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import

javax.microedition.lcdui.Choice; import javax.microedition.lcdui.Command; import

javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import

javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import

javax.microedition.lcdui.List; import javax.microedition.lcdui.TextField; import

javax.microedition.midlet.MIDlet; public class LoginMidlet extends MIDlet implements

CommandListener { private Display display; private TextField userName = new

TextField("LoginID:", "", 10, TextField.ANY);

22
private TextField password = new TextField("Password:", "", 10, TextField.PASSWORD);
private Form form = new Form("Sign in"); private Command cancel = new
Command("Cancel", Command.CANCEL, 2);

private Command login = new Command("Login", Command.OK, 2);

public void startApp() { display =

Display.getDisplay(this);

form.append(userName);

form.append(password);

form.addCommand(cancel);

form.addCommand(login);

form.setCommandListener(this);

display.setCurrent(form);

public void pauseApp() {

public void destroyApp(boolean unconditional) { notifyDestroyed();

public void validateUser(String name, String password) { if

(name.equals("anshika") && password.equals("1234")) {

menu();

} else {

tryAgain();

23
}

public void menu() {

List services = new List("Choose one", Choice.EXCLUSIVE);

services.append("Check Mail", null); services.append("Compose",

null); services.append("Addresses", null);

services.append("Options", null); services.append("Sign Out", null);

display.setCurrent(services);

public void tryAgain() {

Alert error = new Alert("Login Incorrect", "Please try again", null,


AlertType.ERROR); error.setTimeout(Alert.FOREVER);
userName.setString(""); password.setString(""); display.setCurrent(error,
form);

public void commandAction(Command c, Displayable d) {

String label = c.getLabel(); if

(label.equals("Cancel"))

{ destroyApp(true);

} else if (label.equals("Login")) { validateUser(userName.getString(),

password.getString());

}}

24
OUTPUT :

25
CORRECT USERNAME & PASSWORD :

26
INCORRECT USERNAME AND PASSWORD :

27
LAB - 3

Develop an application that uses GUI components,


Font and Colours

28
Android Application that uses GUI components, Font and Colors
Procedure:

Creating a New project:

 Open Android Stdio and then click on File -> New -> New project.

 Then type the Application name as “ex.no.1″ and click Next.

 Then select the Minimum SDK as shown below and click Next.

29
 Then select the Empty Activity and click Next.

 Finally click Finish.

30
 It will take some time to build and load the project.
 After completion it will look as given below.

Designing layout for the Android Application:

 Click on app -> res -> layout -> activity_main.xml.

31
 Now click on Text as shown below.

 Then delete the code which is there and type the code as given below.
Code for Activity_main.xml:

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:id="@+id/textView" android:layout_width="match_parent"


android:layout_height="wrap_content" android:layout_margin="30dp"
android:gravity="center" android:text="Hello World!"
android:textSize="25sp" android:textStyle="bold" />

32
<Button android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
android:textSize="25sp" />

<Button android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change color"
android:textSize="25sp" />

</LinearLayout>

 Now click on Design and your application will look as given below.

 So now the designing part is completed.

Java Coding for the Android Application:

 Click on app -> java -> com.example.exno1 -> MainActivity.

33
 Then delete the code which is there and type the code as given below.
Code for MainActivity.java: package com.example.exno1; import
android.graphics.Color; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.view.View; import
android.widget.Button; import android.widget.TextView;

public class MainActivity extends AppCompatActivity


{ int ch=1;
float font=30;
@Override

protected void onCreate(Bundle savedInstanceState)


{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TextView t= (TextView) findViewById(R.id.textView); Button


b1= (Button) findViewById(R.id.button1); b1.setOnClickListener(new
View.OnClickListener() {

@Override

public void onClick(View v) {


t.setTextSize(font); font =
font + 5; if (font == 50)
font = 30;

}
});

34
Button b2= (Button) findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {

@Override public void


onClick(View v) { switch (ch)
{ case 1:

t.setTextColor(Color.RED);
break;
case 2:

t.setTextColor(Color.GREEN);
break;
case 3:

t.setTextColor(Color.BLUE);
break;
case 4:

t.setTextColor(Color.CYAN);
break;
case 5:

t.setTextColor(Color.YELLOW);
break;
case 6:

t.setTextColor(Color.MAGENTA);
break;
} ch++;
if (ch == 7)
ch = 1;

}
});
}
}
 So now the Coding part is also completed.
 Now run the application to see the output.

Output:

35
36
Result:

Thus a Simple Android Application that uses GUI components, Font and Colors is developed
and executed successfully.

37
LAB - 4
Develop an application that uses Layout
Managers and event listeners.

Aim :

38
Develop an application that uses Layout Managers and event listeners.

Framework : Android Studio

Language : Java

Main_activity.xml file:
<?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:id="@+id/assign"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/tan_background"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/numbers"
android:layout_gravity="center"
android:layout_height="80dp"
android:layout_width="match_parent"
android:textStyle="bold"
android:textSize="30dp"
android:textColor="@color/white"
android:padding="16dp"
android:background="@color/lab1"
android:text="Lab 1" />

<TextView
android:id="@+id/family"
android:layout_gravity="center"
android:layout_height="80dp"
android:layout_width="match_parent"
android:textStyle="bold"
android:textSize="30dp"
android:textColor="@color/white"
android:padding="16dp"
android:background="@color/lab2"
android:text="Lab 2" />

<TextView
android:id="@+id/colors"
android:layout_gravity="center"
android:layout_height="80dp"
android:layout_width="match_parent"
android:textStyle="bold"
android:textSize="30dp"
android:textColor="@color/white"
android:padding="16dp"
android:background="@color/lab3"

39
android:text="Lab 3" />

<TextView
android:id="@+id/phrases"
android:layout_gravity="center"
android:layout_height="80dp"
android:layout_width="match_parent"
android:textStyle="bold"
android:textSize="30dp"
android:textColor="@color/white"
android:padding="16dp"
android:background="@color/lab4"
android:text="Lab 4" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="209dp"
android:gravity="center"
android:orientation="horizontal">

<Button
android:id="@+id/like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="-6dp"
android:padding="10dp"
android:text="👍"
android:textSize="30dp" />

<Button
android:id="@+id/hate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="58dp"
android:layout_toRightOf="@id/like"
android:padding="10dp"
android:text="👎"
android:textSize="30dp" />

</RelativeLayout>

</LinearLayout>

Activity_main.java file
package com.example.mobiledev;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

40
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button likebutton= (Button) findViewById(R.id.like);
Button hatebutton= (Button) findViewById(R.id.hate);
final LinearLayout linearlayout = findViewById(R.id.assign);
likebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
linearlayout.setBackgroundResource(R.color.green);
}
});
hatebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
linearlayout.setBackgroundResource(R.color.red);
}
});
}
}

OUTPUT :

initial screen: OnClick ‘like’ button: OnClick ‘dislike’ button:

41
<!--<?xml version="1.0" encoding="utf-8"?>-->
<!--<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"-->
<!-- android:orientation="vertical"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent">-->

<!-- <Button-->
<!-- android:id="@+id/button6"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:text="Button" />-->

<!-- <Button-->
<!-- android:id="@+id/button4"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:text="Button" />-->

<!-- <Button-->
<!-- android:id="@+id/button5"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:text="Button" />-->

<!-- <Button-->
<!-- android:id="@+id/button7"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:text="Button" />-->

<!-- <Button-->
<!-- android:id="@+id/button8"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:text="Button" />-->

<!-- <Button-->
<!-- android:id="@+id/button9"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:text="Button" />-->

<!-- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"-->


<!-- android:orientation="horizontal"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content">-->

<!-- <Button-->
<!-- android:id="@+id/button9"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->

42
<!-- android:text="Button" />-->
<!-- <Button-->
<!-- android:id="@+id/button9"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:text="Button" />-->

<!-- </LinearLayout>-->

<!-- </LinearLayout>-->

43
LAB - 5
Develop a native calculator application

44
Aim : develop a native calculator application

Framework : Android Studio

Language : Java

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@color/material_dynamic_neutral_variant30"
android:layout_height="match_parent"
android:layout_margin="20dp">

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />

</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">

<Button
android:id="@+id/Add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="30sp"/>

<Button
android:id="@+id/Sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"

45
android:layout_weight="1"
android:text="-"
android:textSize="30sp"/>

<Button
android:id="@+id/Mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="30sp"/>

<Button
android:id="@+id/Div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="30sp"/>

</LinearLayout>

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:textColor="@color/white"
android:text="Answer is"
android:textSize="30sp"
android:gravity="center"/>

</LinearLayout>

Mainactivity.java
import android.os.Bundle;

import android.text.TextUtils;
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 AppCompatActivity implements


OnClickListener
{
//Defining the Views
EditText Num1;
EditText Num2;
Button Add;
Button Sub;
Button Mul;
Button Div;
TextView Result;

@Override
public void onCreate(Bundle savedInstanceState)
{

46
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Referring the Views


Num1 = (EditText) findViewById(R.id.editText1);
Num2 = (EditText) findViewById(R.id.editText2);
Add = (Button) findViewById(R.id.Add);
Sub = (Button) findViewById(R.id.Sub);
Mul = (Button) findViewById(R.id.Mul);
Div = (Button) findViewById(R.id.Div);
Result = (TextView) findViewById(R.id.textView);

// set a listener
Add.setOnClickListener(this);
Sub.setOnClickListener(this);
Mul.setOnClickListener(this);
Div.setOnClickListener(this);
}

@Override
public void onClick (View v)
{

float num1 = 0;
float num2 = 0;
float result = 0;
String oper = "";

// check if the fields are empty


if (TextUtils.isEmpty(Num1.getText().toString()) ||
TextUtils.isEmpty(Num2.getText().toString()))
return;

// read EditText and fill variables with numbers


num1 = Float.parseFloat(Num1.getText().toString());
num2 = Float.parseFloat(Num2.getText().toString());

// defines the button that has been clicked and performs the
corresponding operation
// write operation into oper, we will use it later for output
switch (v.getId())
{
case R.id.Add:
oper = "+";
result = num1 + num2;
break;
case R.id.Sub:
oper = "-";
result = num1 - num2;
break;
case R.id.Mul:
oper = "*";
result = num1 * num2;
break;
case R.id.Div:
oper = "/";
result = num1 / num2;
break;
default:
break;
}

47
// form the output line
Result.setText(num1 + " " + oper + " " + num2 + " = " + result);
}
}

output

48
49
LAB - 6
Write an application that draws basic
graphical primitives on the screen

50
Aim : To develop a Simple Android Application that draws basic Graphical
Primitives on the screen..

Framework : Android Studio

Language : Java

Code for Activity_main.xml:

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

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" /> </RelativeLayout>

Code for MainActivity.java: package com.example.exno4; import


android.app.Activity; 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 android.widget.ImageView; public class MainActivity extends
Activity

51
{

@Override public void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

//Creating a Bitmap

Bitmap bg = Bitmap.createBitmap(720, 1280, Bitmap.Config.ARGB_8888);

//Setting the Bitmap as background for the ImageView ImageView i = (ImageView)


findViewById(R.id.imageView); i.setBackgroundDrawable(new BitmapDrawable(bg));
//Creating the Canvas Object

Canvas canvas = new Canvas(bg);

//Creating the Paint Object and set its color & TextSize Paint paint = new
Paint(); paint.setColor(Color.BLUE); paint.setTextSize(50); //To draw a
Rectangle canvas.drawText("Rectangle", 420, 150, paint);
canvas.drawRect(400, 200, 650, 700, paint);

//To draw a Circle canvas.drawText("Circle", 120, 150, paint);


canvas.drawCircle(200, 350, 150, paint);

//To draw a Square canvas.drawText("Square", 120, 800, paint);


canvas.drawRect(50, 850, 350, 1150, paint);

//To draw a Line canvas.drawText("Line", 480, 800, paint);


canvas.drawLine(520, 850, 520, 1150, paint);

}}

Output:

52
53
LAB – 7
Develop an application that makes use of
database.

54
Aim : Develop an application that makes use of database.

Framework : Android Studio

Language : Java

MainActivity.java
package com.example.useofdatabase;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText e1, e2, e3; // 3 edit text objects DBHelper mydb;
DBHelper mydb;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBHelper(this);

e1 = (EditText) findViewById(R.id.student_name);
e2 = (EditText) findViewById(R.id.student_roll);
e3 = (EditText) findViewById(R.id.branch_name);
}

public void insert(View view) {


mydb.insertData(e1.getText().toString(), e2.getText().toString(),
e3.getText().toString());
Toast.makeText(this, "Data Inserted into DB",
Toast.LENGTH_SHORT).show();
}

public void clear(View view) {


e1.setText("");
e2.setText("");
e3.setText("");
}

public void viewData(View view) {


// Create cursor object
Cursor c = mydb.getAllData();
if (c.getCount() == 0) {
showDialog("Alert", "No Data Found in Database");
} else {
StringBuffer bf = new StringBuffer();
while (c.moveToNext()) {

55
bf.append("Name : " + c.getString(0) + "\n");
bf.append("Roll No : " + c.getString(1) + "\n");
bf.append("Branch : " + c.getString(2) + "\n");
}
showDialog("Data", bf.toString());
}
}

// Function to show dialog


public void showDialog(String title, String msg) {
AlertDialog.Builder bu = new AlertDialog.Builder(this);
bu.setCancelable(true);// Allow to cancel dialog box
bu.setTitle(title);
bu.setMessage(msg);
bu.show();
}
}

DBHelper.java

package com.example.useofdatabase;

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 {

// Our database

public static final String DATABASE_NAME = "student.db";

public DBHelper(Context context){

super(context, DATABASE_NAME, null, 1);

@Override

56
public void onCreate(SQLiteDatabase db) {

// Creating table with sname, rollno, branch

db.execSQL("create table student(sname Text, rollno Text, branch


Text)");

@Override

public void onUpgrade(SQLiteDatabase db, int i, int i1) {

db.execSQL("DROP TABLE IF EXISTS student");

onCreate(db);

// Functions to insert data

public void insertData(String student_name, String student_roll, String


branch_name){

SQLiteDatabase db = this.getWritableDatabase(); // get the link of


database

ContentValues cv = new ContentValues(); // store the values

cv.put("sname", student_name);

cv.put("rollno", student_roll);

cv.put("branch", branch_name);

db.insert("student", null , cv); // insert the values stored in cv


object

// Functions to fetch data

public Cursor getAllData() {

SQLiteDatabase db = this.getWritableDatabase(); // get the link of


database

Cursor res = db.rawQuery("select * from student", null);

return res; // return the data

57
}

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:id="@+id/linearLayout"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TextView

android:id="@+id/textView"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_marginStart="30dp"

android:layout_marginTop="30dp"

android:layout_marginEnd="30dp"

android:text="Student Name"

android:textSize="20sp"

android:textStyle="bold"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

<EditText

58
android:id="@+id/student_name"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_marginStart="30dp"

android:layout_marginTop="80dp"

android:layout_marginEnd="30dp"

android:ems="10"

android:inputType="textPersonName"

android:minHeight="48dp"

android:text="Name"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

<TextView

android:id="@+id/textView2"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_marginStart="30dp"

android:layout_marginTop="24dp"

android:layout_marginEnd="30dp"

android:text="Roll Number"

android:textSize="20sp"

android:textStyle="bold"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/student_name" />

59
<EditText

android:id="@+id/student_roll"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_marginStart="30dp"

android:layout_marginTop="30dp"

android:layout_marginEnd="30dp"

android:layout_marginRight="30dp"

android:ems="10"

android:inputType="textPersonName"

android:minHeight="48dp"

android:text="Roll no."

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView

android:id="@+id/textView3"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_marginStart="30dp"

android:layout_marginTop="30dp"

android:layout_marginEnd="30dp"

android:text="Branch"

android:textSize="20sp"

android:textStyle="bold"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent"

60
app:layout_constraintTop_toBottomOf="@+id/student_roll" />

<EditText

android:id="@+id/branch_name"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_marginStart="30dp"

android:layout_marginTop="30dp"

android:layout_marginEnd="30dp"

android:layout_marginBottom="372dp"

android:autoText="true"

android:ems="10"

android:inputType="textPersonName"

android:minHeight="48dp"

android:text="Branch"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="@+id/textView3" />

<LinearLayout

android:id="@+id/linearLayout3"

android:layout_width="0dp"

android:layout_height="71dp"

android:layout_marginStart="25dp"

android:layout_marginTop="458dp"

android:layout_marginEnd="25dp"

android:orientation="horizontal"

app:layout_constraintEnd_toEndOf="parent"

61
app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent">

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="Save"

android:onClick="insert"/>

<Button

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="Clear"

android:onClick="clear"/>

<Button

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="View"

android:onClick="viewData"/>

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

62
Ouput : -

63
LAB – 8
Develop an application that makes use of
RSS Feed.

64
Aim : Develop an application that makes use of RSS Feed.

Framework : Android Studio

Language : Java

Step 1 : Create a new Empty Activity project in Android Studio

Step 2 : Code for MainActivity.java

65
package com.example.madlab8;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;

import android.content.Intent;

import android.net.Uri;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.InputDevice;

import android.view.View;

import android.widget.AdapterView;

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;

public class MainActivity extends AppCompatActivity {

ListView lvRSS;

ArrayList<String> titles;

ArrayList<String> links;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

lvRSS = (ListView) findViewById(R.id.lvRSS);

titles = new ArrayList<String>();

links = new ArrayList<String>();

66
67
68
return exception;

@Override

protected void onPostExecute(Exception s) {

super.onPostExecute(s);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,


android.R.layout.simple_list_item_1, titles);

lvRSS.setAdapter(adapter);

progressDialog.dismiss();

Step 3 : Code for 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"
>

<ListView

android
:id="@+id/lvRSS"

android
:layout_width
="match_parent"

android
:layout_height
="match_parent"
/>

</LinearLayout
>
Step 4 : Code for AndroidManifest.xml

Giving permission to use internet


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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
category
< android:name="android.intent.category.LAUNCHER"
/>

package="com.example.madlab8"
</
intent-filter
> >

<uses-permission
</
activity
> android:name="android.permission.INTERNET"
></uses-permission>

<application
</application
>

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"
</manifest>

Step 5 : Output
android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true" 70

android:theme="@style/Theme.MADLab8">

<activity
android:name=".MainActivity"

android:exported="true">

<intent-filter>

<action android:name="android.intent.action.MAIN"/>

LAB – 9
Implement an application that implements multithreading

71
72
LAB – 10
Develop a native application that uses GPS location
information.

73
74

You might also like