0% found this document useful (0 votes)
14 views

Android Practical

The document discusses various Android development environments including Android Studio, Eclipse, IntelliJ IDEA, NetBeans, Visual Studio with Xamarin, and Cordova. It provides details on their key features, supported languages, system requirements and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Android Practical

The document discusses various Android development environments including Android Studio, Eclipse, IntelliJ IDEA, NetBeans, Visual Studio with Xamarin, and Cordova. It provides details on their key features, supported languages, system requirements and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Shivalik College Of Engineering

SESSION: 2023-24

Practical file of

Android Application Development

(Subject Code – 055004)

Submitted to: Submitted by:

Meenu Rana Alok Verma

CSE Department Roll No. 21084050001


S.No. Experiment/ Topic Date Signature /
Remarks

1. Study of installation and


configuration of Android
Development framework

2. Study of Various Android


Development Environment
(IDE)

3. Design an Application in
Android Environment
representing a Simple
Calculator

4. Develop an application for


connecting to the internet
and sending email.

5. Develop an application for


connecting to the internet
and sending email.

6. Design a simple to-do list


application using SQLite.
Practical 1
Study of installation and configuration of Android Development framework

Android Studio is the official IDE (Integrated Development Environment) for Android app development
and it is based on JetBrains’ IntelliJ IDEA software.

System Requirements
● Microsoft Windows 7/8/10 (32-bit or 64-bit)
● 4 GB RAM minimum, 8 GB RAM recommended (plus 1 GB for the Android Emulator)
● 2 GB of available disk space minimum, 4 GB recommended (500 MB for IDE plus 1.5 GB for
Android SDK and emulator system image)
● 1280 x 800 minimum screen resolution

Installation Guide
Step1: Search Android Studio on web browser.
Step 2: Click on the Download Android Studio Button.

Click on the “I have read and agree with the above terms and conditions” checkbox followed by the
download button.

Click on the Save file button in the appeared prompt box and the file will start downloading.

Step 3: After the downloading has finished, open the file from downloads and run it. It will prompt the
following dialog box.

Click on next. In the next prompt, it’ll ask for a path for installation. Choose a path and hit next.

Step 4: It will start the installation


Click on next.

Step 5: Once “Finish” is clicked, it will ask whether the previous settings need to be imported [if the
android studio had been installed earlier], or not. It is better to choose the ‘Don’t import Settings option’.

Click the OK button.

Step 6: This will start the Android Studio.

Meanwhile, it will be finding the available SDK components.

Step 7: After it has found the SDK components, it will redirect to the Welcome dialog box.

Click on Next.

Choose Standard and click on Next. Now choose the theme, whether the Light theme or the Dark one.
The light one is called the IntelliJ theme whereas the dark theme is called Dracula. Choose as required.
Click on the Next button.

Step 8: Now it is time to download the SDK components.

Click on Finish. Components begin to download let it complete.

The Android Studio has been successfully configured.


Step 9: Click on Start a new Android Studio project to build a new app.
Practical- 2
Study of Various Android Development Environment (IDE)

1. Android Studio : Android Studio is Google’s official IDE for Android, which is based on Intellij
IDEA with features custom-designed for Android.

Developer/Owner: Google
Key features:
● Instant run
● Intelligent code editor
● Android Emulator
● Flexible Gradle-based build system
● Android device optimization
● Integration with GitHub, Subversion and other team tools
● Code templates
● Sample apps

Programming languages supported:Java, C, C++, Kotlin

Target operating systems: Android

Runs on: Windows, macOS, Linux

System Requirements:
● Windows 7 or later, macOS 10.10 or later, or 64-bit Linux with GNOME or KDE desktop and GNU
C Library
● 3 GB RAM plus 1 GB for Android Emulator (8 GB recommended)
● 2 GB disk space (4 GB recommended)
● 1280 x 800 or better screen resolution
First production release: December 2014

Most recent update/stable release:Version 2.3.3.0 (June 8, 2017)

2. Eclipse: This open source IDE is the second most popular in the world. Eclipse began as a Java
IDE but has since grown to support many different programming languages. For several years, a
version of Eclipse with an Android plug-in (ADT) was the recommended development
environment for the Android platform. Although Google has ceased support for ADT, many mobile
developers continue to use Eclipse to create Android apps.

Developer/Owner: The Eclipse Foundation

Key features:
● Support for multiple languages and operating systems
● Extensive customization abilities
● Large marketplace of plug-ins
● Integration with Git, Maven and many other popular development tools
● Large community and user base

Programming languages supported: Java, C, C++, C#, Cobol, JavaScript, Perl, PHP, Python, R, Ruby
and many others.
Target operating systems: Any, including Windows, Linux, macOS and Android

Runs on: Any system that supports Java, including Windows, Linux, macOS

System Requirements:

● 32- or 64-bit Windows, 64-bit macOS, or 32- or 64-bit Linux


● Java 8 JRE/JDK

First production release: November 2001

Most recent update/stable release: Version 4.7 codenamed Oxygen (June 8, 2017)

3. IntelliJ IDEA : It has numerous features for Android development and provides the basis for
Android Studio. Comes in both a free, open source Community edition and a paid Ultimate
edition.

Developer/Owner: JetBrains

Key features:
● Smart code completion
● Data flow analysis
● Language injection
● Cross-language refactorings
● Ergonomic, editor-centric environment with numerous shortcuts
● Inline debugging
● Version control
● Integrated build tools
● Integrated testing

Programming languages supported: Java, Scala, Groovy, Kotlin, JavaScript, TypeScript, SQL

Target operating systems: Any operating system that supports Java

Runs on: Windows, Linux, macOS

System Requirements:

● Windows XP or later, macOS 10.5 or higher (only 64-bit), or a Linux distribution with the KDE,
Gnome or Unity DE desktop (64-bit recommended)
● JDK
● 1 GB RAM (2GB recommended)
● 2.5 GB hard disk space
● 1024 x 768 minimum resolution

First production release: 2001

Most recent update/stable release:Version 2017.2 (July 2017)


4. Netbeans: NetBeans was bought by Sun Microsystems in 1999 and then by Oracle in 2010. Like
Eclipse, NetBeans is known primarily as a Java IDE, but it also supports many other languages. It
is the official IDE for Java 8 and boasts a large community of users. The latest version offers
improved support for Node.js, Maven, PHP and C/C++.

Developer/Owner: Oracle

Key features:
● Smart code editing
● Easy, efficient project management
● Rapid user interface development
● Integrated debugger
● Support for multiple languages and operating systems
● Large library of plugins

Programming languages supported:Java, C/C++, XML, HTML, PHP, JavaScript and many others
Target operating systems:Cross-platform
Runs on:Windows, macOS, Linux, Solaris

System Requirements:
● JDK 8
● Windows Vista or later, Windows Server 2008 R2 or later, macOS 10.8.3 or later, or one of the
supported versions of Linux
● 128 MB RAM
● 124 MB hard drive space

First production release: 1997


Most recent update/stable release: Version 8.2 (October 2016)

5. Visual Studio( with Xamarin)

For mobile development, Visual Studio integrates with Xamarin, enabling cross-platform native
development in C#. It also supports Apache Cordova for those who prefer to write Android apps in HTML
and JavaScript.

Developer/Owner: Microsoft

Key features:
● Support for multiple languages and platforms
● CodeLens
● Peek Definition
● Refactoring
● Dependency graphs and code maps
● Advanced debugging and diagnostics
● Integrated testing
● Cross-platform mobile development support
● Integrated PowerPoint storyboarding
● Code review
● Programming languages supported:Dozens of languages, including C#, Visual Basic, C++,
JavaScript, Python, HTML, Go, PHP and others
Target operating systems: Cross-platform
Runs on: Windows, macOS, Linux

Programming languages supported: Dozens of languages, including C#, Visual Basic, C++,
JavaScript, Python, HTML, Go, PHP and others

System Requirements:
● Windows 7 or later, Windows Server 2012 R 2 or later, or macOS 10.12 or later
● 1.8 GHz or faster processor (dual- or multi-core recommended)
● 2 GB RAM (4 GB recommended)
● 1 GB hard drive (more required for some features)
● Video card with 1280 x 720 resolution support (or better)

First production release: Visual Studio 97 (1997)


Most recent update/stable release: Visual Studio 2017 (March 2017)

6. Cordova : First developed by a company called Nitobi, Cordova was originally known as
PhoneGap. It is a cross-platform mobile development framework that allows developers to write
apps in HTML, CSS and JavaScript and then deploy them to any mobile platform.

Developer/Owner: Apache Software Foundation

Key features:

● Target multiple platforms with one code base


● Allows developers to create mobile apps using Web technologies
● Support for offline scenarios
● Access to native device APIs
● App templates
● Programming languages supported: HTML, CSS, JavaScript

Target operating systems: Cross-platform, including iOS, Android, Windows, macOS, Ubuntu
Runs on: Windows, macOS, Linux

System Requirements:
● Android Studio
● JDK 8 or later
● Windows 7 or later, macOS 10.10 or later, or 64-bit Linux with GNOME or KDE desktop and GNU
C Library
● 3 GB RAM plus 1 GB for Android Emulator (8 GB recommended)
● 2 GB disk space (4 GB recommended)
● 1280 x 800 or better screen resolution

First production release: 2011


Most recent update/stable release:Version 7.0.1 (May 2017)
Practical-3
Design an Application in Android Environment representing a Simple Calculator

1) Firstly, Create an Application by Name “calciApplication”


2) Go to xml code of design change the layout to “RelativeLayout”
3) Add TextView component & change the following properties:
• Size: 36sp
• Text: Simple Calci
• Center-Align
4) Add PlainText(EditText) component & change the following properties in XML Code:
• Text: “”
• Hint: “Enter the Number 1”
• id: “@+id/editText1”
5) Add PlainText(EditText) component & change the following properties in XML Code:
• Text: “”
• Hint: “Enter the Number 2 ”
• id: “@+id/editText2”
6) Add TextView component to display result & change the following properties:
• Size: 40dp
• Text: “0”
• Center-Align
• id: “@+id/textView1”
7) Add 4 Buttons & rename the four buttons “Add”, “Sub”,”Mul” and “div”
with following addition:
• Onclick: “doAdd”(Add Button)
• Onclick: “doSub”(Sub Button)
• Onclick: “doMul”(Mul Button)
• Onclick: “doDiv”(Div Button)

XML-CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="209dp"
android:layout_height="60dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="108dp"
android:layout_marginRight="108dp"
android:layout_marginBottom="530dp"
android:text="Simple Calci"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="115dp"
android:layout_marginRight="115dp"
android:layout_marginBottom="364dp"
android:ems="10"
android:hint="Enter the Number 2"
android:inputType="textPersonName"
android:text=""
android:textColorHighlight="#FFFFFF" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="110dp"
android:layout_marginRight="110dp"
android:layout_marginBottom="439dp"
android:ems="10"
android:hint="Enter the Number 1"
android:inputType="textPersonName"
android:text=""
android:textColorHighlight="#FFFFFF" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="260dp"
android:layout_marginRight="260dp"
android:layout_marginBottom="175dp"
android:text="ADD"
android:textStyle="bold"
android:onClick="add"
app:backgroundTint="#E8F381" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="266dp"
android:layout_marginRight="266dp"
android:layout_marginBottom="61dp"
android:text="MUL"
android:onClick="mul"
app:backgroundTint="#A1FAA4" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="108dp"
android:layout_marginRight="108dp"
android:layout_marginBottom="63dp"
android:text="DIV"
android:onClick="div"
app:backgroundTint="#E6C28C" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="105dp"
android:layout_marginRight="105dp"
android:layout_marginBottom="182dp"
android:text="SUB"
android:onClick="sub"
app:backgroundTint="#ECA9A9" />
<TextView
android:id="@+id/tv1"
android:layout_width="86dp"
android:layout_height="61dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="202dp"
android:layout_marginRight="202dp"
android:layout_marginBottom="274dp"
android:text="0"
android:textSize="36sp" />
</RelativeLayout>

JAVA-CODE
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText e1,e2;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 =(EditText)findViewById(R.id.editText1);
e2 = (EditText)findViewById(R.id.editText2);
tv= (TextView)findViewById(R.id.tv1);
}
public void add(View v){
int a1=Integer.parseInt(e1.getText().toString());
int a2= Integer.parseInt(e2.getText().toString());
int result=a1+a2;
tv.setText(""+result);
}
public void sub(View v){
int a1=Integer.parseInt(e1.getText().toString());
int a2= Integer.parseInt(e2.getText().toString());
int result=a1-a2;
tv.setText(""+result);
}
public void mul(View v){
int a1=Integer.parseInt(e1.getText().toString());
int a2= Integer.parseInt(e2.getText().toString());
int result=a1*a2;
tv.setText(""+result);
}
public void div(View v){
float a1=Integer.parseInt(e1.getText().toString());
float a2= Integer.parseInt(e2.getText().toString());
float result=a1/a2;
tv.setText(""+result);
}

}
Practical - 4

Develop an application for connecting to the internet and sending email.

1.Create a New Android Project:


• Click New in the toolbar.
• In the window that appears, open the Android folder, select Android Application Project,
and click next.
• Provide the application name and the project name and then finally give the desired
package name.
• Choose a launcher icon for your application and then select Blank Activity and then click
Next
• Provide the desired Activity name for your project and then click Finish.
2. Create a New AVD (Android Virtual Device):
• click Android Virtual Device Manager from the toolbar.
• In the Android Virtual Device Manager panel, click New.
• Fill in the details for the AVD. Give it a name, a platform target, an SD card size, and
a skin (HVGA is default).
• Click Create AVD and Select the new AVD from the Android Virtual Device
Manager and click Start.
3. Design the graphical layout.
4. Run the application.
5. When the application starts alarm sound will be invoked.
6. Stop alarm button is clicked to stop the alarm.
7. Close the Android project.

PROGRAM CODE:
MainActivity.java
package com.example.admin.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivityextends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn = (Button) findViewById(R.id.sendbttn);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendEmail();
}
});
}
protected void sendEmail() {
Log.i("Send email", "");
String[] TO = {
"[email protected]"
};
String[] CC = {
"[email protected]"
};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There is no email client installed.",
Toast.LENGTH_SHORT).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.admin.myapplication.MainActivity">
<EditTextandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" /> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText" /> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText3"
android:layout_below="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_alignEnd="@+id/editText2" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND MAIL"
android:id="@+id/sendbttn"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/editText3"
android:layout_alignStart="@+id/editText3" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipient"
android:id="@+id/textView"
android:layout_alignBottom="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="subject"
android:id="@+id/textView2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message Body"
android:id="@+id/textView3"
android:layout_alignBottom="@+id/editText3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

OUTPUT:
Practical - 5

Develop an application for connecting to the internet and sending email.


PROGRAM CODE
MainActivity.java
package com.example.gui;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
importandroid.widget.TextView;
publicclassMainActivityextends Activity {
float font = 24;
inti = 1;
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
finalTextView t1 = (TextView)findViewById(R.id.textView1);
Button b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View view) {
t1.setTextSize(font);
font = font+4;
if(font==40)
font = 20;
}
});
Button b2 = (Button)findViewById(R.id.button2);
b2.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View view) {
switch(i)
{
case 1:
t1.setTextColor(Color.parseColor("#0000FF"));
break;
case 2:
t1.setTextColor(Color.parseColor("#00FF00"));
break;
case 3:
t1.setTextColor(Color.parseColor("#FF0000"));
break;
case 4:
t1.setTextColor(Color.parseColor("#800000"));
break;
}
i++;
if(i==5)
i = 1;
}
});
}
}

activity_main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="WELCOME"
android:layout_margin="20sp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:gravity="center"
android:text="Change Font Size" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20sp"
android:text="Change Color" />
</LinearLayout>
Practical - 6

Design a simple to-do list application using SQLite.

ALGORITHM:
1. Create a New Android Project:
• Click New in the toolbar.
• In the window that appears, open the Android folder, select Android Application
Project, and click next.
• Provide the application name and the project name and then finally give the
desired package name.
• Choose a launcher icon for your application and then select Blank Activity and
then click Next
• Provide the desired Activity name for your project and then click Finish.
2. Create a New AVD (Android Virtual Device):
• click Android Virtual Device Manager from the toolbar.
• In the Android Virtual Device Manager panel, click New.
• Fill in the details for the AVD. Give it a name, a platform target, an SD card size,
and a skin (HVGA is default).
• Click Create AVD and Select the new AVD from the Android Virtual Device
Manager and click Start.
3. Design the graphical layout.
4. Run the application.
5. Perform the database operation.(Insert/delete/view/update)
6. Close the Android project.

PROGRAM CODE
MainActivity.java
package com.example.admin.myapplication;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivityextends Activity implements OnClickListener
{
EditTextRollno,Name,Marks;
Button Insert,Delete,Update,View,ViewAll;
SQLiteDatabasedb;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Rollno=(EditText)findViewById(R.id.Rollno);
Name=(EditText)findViewById(R.id.Name);
Marks=(EditText)findViewById(R.id.Marks);
Insert=(Button)findViewById(R.id.Insert);
Delete=(Button)findViewById(R.id.Delete);
Update=(Button)findViewById(R.id.Update);
View=(Button)findViewById(R.id.View);
ViewAll=(Button)findViewById(R.id.ViewAll);
Insert.setOnClickListener(this);
Delete.setOnClickListener(this);
Update.setOnClickListener(this);
View.setOnClickListener(this);
ViewAll.setOnClickListener(this);
// Creating database and table
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS
student(rollnoVARCHAR,nameVARCHAR,marks VARCHAR);");
}
public void onClick(View view)
{
// Inserting a record to the Student table
if(view==Insert)
{
// Checking for empty fields
if(Rollno.getText().toString().trim().length()==0||
Name.getText().toString().trim().length()==0||
Marks.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student VALUES('"+Rollno.getText()+"','"+Name.getText()+
"','"+Marks.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
// Deleting a record from the Student table
if(view==Delete)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE rollno='"+Rollno.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Updating a record in the Student table
if(view==Update)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst()) {
db.execSQL("UPDATE student SET name='" + Name.getText() + "',marks='" +
Marks.getText() +
"' WHERE rollno='"+Rollno.getText()+"'");
showMessage("Success", "Record Modified");
}
else {
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Display a record from the Student table
if(view==View)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
Name.setText(c.getString(1));
Marks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
// Displaying all the records
if(view==ViewAll)
{
Cursor c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno: "+c.getString(0)+"\n");
buffer.append("Name: "+c.getString(1)+"\n");
buffer.append("Marks: "+c.getString(2)+"\n\n");
}
showMessage("Student Details", buffer.toString());
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
Rollno.setText("");
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="20dp"
android:text="Student Details"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="110dp"
android:text="Enter Rollno:"
android:textSize="20sp" /> <EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="160dp"
android:text="Enter Name:"
android:textSize="20sp" /> <EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="150dp"
android:inputType="text"
android:textSize="20sp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="210dp"
android:text="Enter Marks:"
android:textSize="20sp" /> <EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="200dp"
android:inputType="number"
android:textSize="20sp" /> <Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="300dp"
android:text="Insert"
android:textSize="30dp" /> <Button
android:id="@+id/Delete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="300dp"
android:text="Delete"
android:textSize="30dp" /> <Button
android:id="@+id/Update"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="400dp"
android:text="Update"
android:textSize="30dp" /> <Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="400dp"
android:text="View"
android:textSize="30dp" /> <Button
android:id="@+id/ViewAll"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="500dp"
android:text="View All"
android:textSize="30dp" />
</AbsoluteLayout>

You might also like