Android Practical File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

PCTE Group of Institutes,

Ludhiana

Android Programming

PRACTICAL FILE

(BCA 3(A) 6th - SEMESTER 2021-24)

NAME: Saurav
SUBJECT: Android Programming
UNIV.ROLL.NO: 2123397
SUBMITTED TO: Ms. Ishita Malhotra
OFFICIAL E-MAIL ADDRESS: [email protected]
Table of Contents

Sr No Topics Page No

1 Installation of Java, android Framework 1-5


2 Android SDK Manager and all its components 6-8
3 Program based on the overriding in Java 9
4 Program based on constructor Overloading in Java 10 - 11
5 Program based on classes in Java 11 - 12
6 Design an Application (Calculator) based on Edit Text, Text Boxes, and 13 – 16
Button
7 Design an application based on WebView to display an existing 17 - 18
Website/Webpage
8 Design an application based on Auto Complete Text View 19 - 21
9 Design an application for Linking of activities using Intent Class and transfer 22 – 23
your data from
Main Activity to another activity using Bundle Class.

10 Design an application for Database connectivity using SQLite database. Create table 24 – 25
stud_details and perform all possible queries of insertion, deletion, updation on the
click of buttonsusing SQLite in Android Studio.
11 Design a billing application using Check Boxes in Android Studio for a bookstore. 26 – 27
12 Working with Calendar View in Android Studio. 28
13 Design an application using Seek Bar in Android Studio. 29
Task 1: Installation of Java, android Framework
1. Download Java for Windows: -

2. Run the Windows Java installer.


The Java installation wizard allows you to customize the JDK install in five different ways.
You can choose:

 Where to install Java on your Windows computer


 add Java to the Windows PATH
 allow Java to automatically run JAR files.
 set the JAVA_HOME environment variable.
 configure Java Soft registration keys.

1
 Where to install Java on Windows?

2
 Set The Java Path

 Validate Java Home on Windows and Confirm Java Path

3
Installation of Android Framework: -

1. Install Android Studio IDE:

 Verify that environment variable JAVA_HOME is set to the JDK installation directory by
the using command “set JAVA_HOME “.
 Check the system requirements for Android Studio and SDK
(https://developer.android.com/studio#Requirements).
 Go to “Android Studio” https://developer.android.com/studio ⇒ Click “Download Android
Studio for Windows 64-bit.
 Run .exe step >> Launch Android Studio. Continue to the Next Step.

2. Installing The Android SDK:

 Start Android Studio.


 Select >Tools > SDK Manager.
 As you can see default Settings dialog box, click these tabs to install Android
SDK platform packages and developer tools.
 SDK Platforms: Select the latest Android SDK package.
 SDK Tools: Select these Android SDK tools:

4
 Click Apply. Android Studio starts installing your selected packages. After completing the
installation, the Status of the installed packages and tools changes from Not Installed to
Installed.

 Click OK and Finish.

5
Task 2: Android SDK Manager and all its components
Android SDK stands for Android Software Development Kit which is developed by
Google for Android Platform. With the help of Android SDK, we can create android
Apps easily.

Components of Android SDK: -


Android SDK Components play a major role in the Development of Android
applications. Below are the important components:

1. Android SDK Tools:

Android SDK tool is an important component of Android SDK. It consists of a


complete set of development and debugging tools. Below are the SDK developer
tools:

 Android SDK Build tool.

 Android Emulator.

 Android SDK Platform-tools.

 Android SDK Tools.

2. Android SDK Build Tools: -

6
Android SDK build tools are used for building actual binaries of Android App. The main
functions of Android SDK Build tools are built, debug, run and test Android applications.
The latest version of the Android SDK Build tool is 30.0.3. While downloading or updating
Android in our System, one must ensure that its latest version is downloaded in SDK
Components.

3. Android Emulator: -

An Android Emulator is a device that simulates an Android device on your system. Suppose
we want to run our android application that we code. One option is that we will run this on
our Android Mobile by Enabling USB Debugging on our mobile. Another option is using
Android Emulator. In Android Emulator the virtual android device is shown on our system
on which we run the Android application that we code.

In Android Virtual Emulator all functions that are feasible on real Android mobile is works on virtual
Device like:

 phone calls, text messages.


 stimulate different network speeds.
 specify the location of a device.
 access on google play store and lots more.
4. Android SDK Platform-tools: -
Android SDK Platform-tools is helpful when we are working on Project and they will show
the error messages at the same time. It is specifically used for testing. It includes:

7
 Android Debug Bridge (ADB) is a command-line tool that helps to communicate with the
device. It allows us to perform an action such as Installing App and Debugging App etc.
 Fast boot allows you to flash a device with a new system image.
 Systane tools help to collect and inspect timing information. It is very crucial for App
Debugging.

5. SDK Platforms: -

For Each Android Software, one SDK platform is available as shown below:

6. SDK Update Sites: -

In SDK Update Sites, some sites are embedded in it which will check for
Android SDK Updates Tools. In this, one must ensure we don’t unclick the
button below because these are checked by default which will check for updates
if we unclick it then it doesn’t check updates for those.

8
Task 3: Program based on the overriding in Java
package Saurav;
// Superclass
class Vehicle {
void start () {
System.out.println("Vehicle is starting...");
}
}
// Subclass Car
class Car extends Vehicle {
@Override
void start () {
System.out.println("Car is starting...");
}
void drive () {
System.out.println("Car is in motion!");
}
}
public class Method_Overriding {
public static void main (String [] args) {
Vehicle myVehicle = new Vehicle ();
Car myCar = new Car();
// Call the overridden methods
myVehicle.start();
myCar.start();
// Call the specific method of the Car class
myCar.drive();
}
}

9
Task 4: Program based on the Constructor Overloading
in Java
package Saurav;

class Student {
private String name;
private int age;
private String course;

// Constructor with no parameters


Student() {
System.out.println("Default constructor called");
}

// Constructor with name and age parameters


Student(String name, int age) {
this.name = name;
this.age = age;
System.out.println("Parameterized constructor with name and age called");
}

// Constructor with name, age, and course parameters


Student(String name, int age, String course) {
this.name = name;
this.age = age;
this.course = course;
System.out.println("Parameterized constructor with name, age, and course called");
}

// Method to display student details


void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Course: " + course);
System.out.println();
}
}

public class Constructor_Overloading {


public static void main(String[] args) {
// Create instances of Student using different constructors
Student student1 = new Student();
Student student2 = new Student("Saurav", 20);
Student student3 = new Student("Divesh", 22, "Computer Science");

10
// Display student details
student1.displayDetails();
student2.displayDetails();
student3.displayDetails();
}
}

11
Task 5: Program based on the Classes in Java
package Saurav;
import java.util.Scanner;

class students {
int rn;
String nm;
String dept;
Scanner ob = new Scanner(System.in);

public void input() {


System.out.println("Enter Student Name, Roll Number and Department");
this.nm = this.ob.next();
this.rn = this.ob.nextInt();
this.dept = this.ob.next();
}

public void disp() {


System.out.println("\nName is \n" + this.nm + "Roll No is \n" + this.rn + "Department is \n" +
this.dept);
}

}
public class Classes
{
public static void main(String[] args) {
students sd;
sd = new students();
sd.input();
sd.disp();
}
}

12
Task 6: Design an Application (Calculator) based on
Edit Text, Text Boxes, and Button

activity_main.xml File: -
<?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:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextNumber1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="Enter number 1"/>

<EditText
android:id="@+id/editTextNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextNumber1"
android:layout_marginTop="8dp"
android:inputType="numberDecimal"
android:hint="Enter number 2"/>

<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextNumber2"
android:layout_marginTop="16dp"
android:text=""
android:textSize="18sp"/>

<Button
android:id="@+id/buttonAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textViewResult"
android:layout_marginTop="16dp"

13
android:text="Add"/>
<Button
android:id="@+id/buttonSubtract"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/buttonAdd"
android:layout_marginTop="8dp"
android:text="Subtract"/>

<Button
android:id="@+id/buttonMultiply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSubtract"
android:layout_marginTop="8dp"
android:text="Multiply"/>

<Button
android:id="@+id/buttonDivide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/buttonMultiply"
android:layout_marginTop="8dp"
android:text="Divide"/>

</RelativeLayout>

MainActivity.java File: -
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText editTextNumber1, editTextNumber2;


private TextView textViewResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

14
editTextNumber1 = findViewById(R.id.editTextNumber1);
editTextNumber2 = findViewById(R.id.editTextNumber2);
textViewResult = findViewById(R.id.textViewResult);

Button buttonAdd = findViewById(R.id.buttonAdd);


buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateResult('+');
}
});

Button buttonSubtract = findViewById(R.id.buttonSubtract);


buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateResult('-');
}
});

Button buttonMultiply = findViewById(R.id.buttonMultiply);


buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateResult('*');
}
});

Button buttonDivide = findViewById(R.id.buttonDivide);


buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateResult('/');
}
});
}

private void calculateResult(char operator) {


try {
double num1 = Double.parseDouble(editTextNumber1.getText().toString());
double num2 = Double.parseDouble(editTextNumber2.getText().toString());

double result = 0;

switch (operator) {
case '+':

15
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
textViewResult.setText("Cannot divide by zero");
return;
}
break;
}

textViewResult.setText("Result: " + result);


} catch (NumberFormatException e) {
textViewResult.setText("Invalid input. Please enter valid numbers.");
}
}
}

OUTPUT: -

16
Task 7: Design an application based on WebView to
display an existing Website/Webpage
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"

tools:context=".MainActivity">

<WebView

android:id="@+id/webView"

android:layout_width="match_parent"

android:layout_height="match_parent" />

</RelativeLayout>

MainActivity.java File: -
import android.os.Bundle;

import android.webkit.WebSettings;

import android.webkit.WebView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private WebView webView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

17
setContentView(R.layout.activity_main);

// Initialize WebView

webView = findViewById(R.id.webView);

// Enable JavaScript (if needed)

WebSettings webSettings = webView.getSettings();

webSettings.setJavaScriptEnabled(true);

// Load a website

String websiteUrl = "https://www.example.com"; // Replace with the desired website


URL

webView.loadUrl(websiteUrl);

OUTPUT: -

18
Task 8: Design an application based on Auto Complete
Text View
activity_main.xml File: -

<?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"
tools:context=".MainActivity">

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Type a country"
android:completionThreshold="1" />

<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/autoCompleteTextView"
android:layout_marginTop="16dp"
android:text="Submit" />

</RelativeLayout>

MainActivity.java File: -

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

19
public class MainActivity extends AppCompatActivity {

private AutoCompleteTextView autoCompleteTextView;


private Button buttonSubmit;

private String[] countries = {"Afghanistan", "Albania","Bhutan", "Bolivia", "Bosnia and


Herzegovina", "Botswana","China","Cyprus", "Czechia", "Denmark", "Djibouti", "Dominica",
"Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
"Eswatini", "Ethiopia", "Fiji", "Finland", "France", "Gabon", "Gambia", "Georgia", "Germany",
"Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti",
"Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy",
"Jamaica", "Japan” }

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
buttonSubmit = findViewById(R.id.buttonSubmit);

// Create ArrayAdapter with country data and set it to AutoCompleteTextView


ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, countries);
autoCompleteTextView.setAdapter(adapter);

// Set item click listener for AutoCompleteTextView


autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedCountry = parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "Selected: " + selectedCountry,
Toast.LENGTH_SHORT).show();
}
});

// Set click listener for the submit button


buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String enteredText = autoCompleteTextView.getText().toString();
Toast.makeText(MainActivity.this, "Submitted: " + enteredText,
Toast.LENGTH_SHORT).show(); } }); } }

20
OUTPUT: -

21
Task 9: Design an application for Linking of activities using Intent
Class and transfer your data from
MainActivity to another activity using Bundle Class.
Mainactivity.java :

package com.example.intent;
import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;
import android.os.Bundle; import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity { Button send_button;


EditText send_text; EditText send_text2;

@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); send_button
= findViewById(R.id.send_button_id); send_text = findViewById(R.id.send_text_id);
send_text2 = findViewById(R.id.send_text_id2);

send_button.setOnClickListener(v -> { String str1 = send_text.getText().toString();


String str2 = send_text2.getText().toString();
Intent intent = new Intent(getApplicationContext(), first_activity.class);
intent.putExtra("message_key1", str1); intent.putExtra("message_key2", str2);
startActivity(intent);
});
}
}

First_activity.java:

package com.example.intent;
import android.content.Intent; import android.os.Bundle; import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; public class first_activity extends
AppCompatActivity {
TextView receiver_msg;

@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
receiver_msg = findViewById(R.id.received_value_id); Intent intent = getIntent();
String str1 = intent.getStringExtra("message_key1"); String str2 =
intent.getStringExtra("message_key2"); String concatenatedString = str1 + " wants to be a " + str2;
receiver_msg.setText(concatenatedString);

22
OUTPUT:

23
Task 10: Design an application for Database connectivity using SQLite
database. Create table stud_details and perform all possible queries of
insertion, deletion, updation on the click of buttons using SQLite in
Android Studio.
package com.example.database;

import android.annotation.SuppressLint; import android.content.Context;


import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import
android.view.View;
import android.widget.*;

import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import


androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import
androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button
b1, b2, b3 ;
EditText et1,et2,et3,et4;
TextView tv;
String sid,name,age,pn,abc; private SQLiteDatabase obj;

@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); b1=findViewById(R.id.b1);
b2=findViewById(R.id.b2); b3=findViewById(R.id.b3);

et1=findViewById(R.id.et1);
et2=findViewById(R.id.et2); et3=findViewById(R.id.et3); et4=findViewById(R.id.et4);
tv=findViewById(R. id.tv); b1.setOnClickListener(this);
b2. setOnClickListener(this); b3.setOnClickListener(this);

obj = openOrCreateDatabase("stud_details", Context.MODE_PRIVATE, null);


}

@SuppressLint("SetTextI18n") @Override
public void onClick(View v) { if (v == b1) {
//obj.execSQL("CREATE TABLE IF NOT EXISTS studentinfo(sid numeric PRIMARY KEY NOT
NULL, name CHAR,age numeric,phone number);");
et1.setText(" "); et2.setText(" ");
et3.setText(" "); et4.setText(" ");
}
if (v == b2) {
obj.execSQL("CREATE TABLE IF NOT EXISTS studentinfo(sid numeric PRIMARY KEY NOT
NULL, name CHAR,age numeric,phone number);");
sid = et1.getText().toString(); name = et2.getText().toString();
age = et3.getText().toString(); pn
= et4.getText().toString();

24
try {
abc = "INSERT INTO studentinfo VALUES('" + sid + "', '" + name + "','" + age + "', '" + pn +
"');";
obj.execSQL(abc); tv.setText("One record inserted");
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
if (v == b3) {
sid = et1.getText().toString();
try {
abc = "DELETE from studentinfo WHERE bid='" + sid + "'";

obj.execSQL(abc);
tv.setText("record deleted");
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}

}
}

25
Task 11: Design a billing application using CheckBoxes in Android
Studio for a bookstore.
package com.example.training_options;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import android.widget.CheckBox; import


android.widget.CompoundButton; import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements


CompoundButton.OnCheckedChangeListener {
CheckBox c1,c2,c3,c4,c5; TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c1=findViewById(R.id.checkBox); c2=findViewById(R.id.checkBox2);
c3=findViewById(R.id.checkBox3);
c4=findViewById(R.id.checkBox4);
c5=findViewById(R.id.checkBox5); t1=findViewById(R.id.textView3);

c1.setOnCheckedChangeListener(this);
c2.setOnCheckedChangeListener(this); c3.setOnCheckedChangeListener(this);
c4.setOnCheckedChangeListener(this); c5.setOnCheckedChangeListener(this);
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ int bill=0; if(c1.isChecked())
{
bill+=150;
}
if(c2.isChecked())
{
bill+=500;
}
if(c3.isChecked())
{
bill+=150;

26
}
if(c4.isChecked())
{
bill+=200;
}
if(c5.isChecked())
{
bill+=50;
}
String a="Your Total Bill :"+ String.valueOf(bill); t1.setText(a);

}
}

27
Task 12: Working with CalendarView in Android Studio.
package com.example.calendarview; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color; import android.os.Bundle; import android.widget.CalendarView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
CalendarView calendar;
TextView dateView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calendar = findViewById(R.id.calendarView);
dateView = findViewById(R.id.textView);
dateView.setTextColor(Color.RED);
calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int
dayOfMonth)
{
String Date = dayOfMonth + "/" + (month + 1) + "/" +year; dateView.setText("Selected date "+Date);
}
});
}
}

28
Task 13: Design an application using Seek Bar in Android Studio.
package com.example.seek_bar;

import android.os.Bundle; import android.widget.SeekBar;


import android.widget.Toast;

import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import


androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import
androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity { SeekBar seekBar;


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); EdgeToEdge.enable(this);
setContentView(R.layout.activity_main); seekBar=(SeekBar)findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { int
progressChangedValue = 0;

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {


progressChangedValue = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) { Toast.makeText(MainActivity.this, "Seek bar
progress is :" + progressChangedValue,
Toast.LENGTH_SHORT).show();
}});}}

29

You might also like