0% found this document useful (0 votes)
70 views25 pages

Practical File: Submitted By: Yuganter Sharma Submitted To: Mukesh Sir Semester: VI

The document describes setting up Android Studio and creating a "Hello World" Android application. It discusses: - Installing Android Studio and the Java Development Kit (JDK) - Creating a new Android project in Android Studio called "Hello World" - Developing the app with an activity and layout file to display "Hello World" text on the screen

Uploaded by

Nitin Pathak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
70 views25 pages

Practical File: Submitted By: Yuganter Sharma Submitted To: Mukesh Sir Semester: VI

The document describes setting up Android Studio and creating a "Hello World" Android application. It discusses: - Installing Android Studio and the Java Development Kit (JDK) - Creating a new Android project in Android Studio called "Hello World" - Developing the app with an activity and layout file to display "Hello World" text on the screen

Uploaded by

Nitin Pathak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 25

Practical File

Mobile Application Development Lab


(6CS4-24)

Submitted by : Yuganter Sharma


Submitted to : Mukesh sir
Semester : VI
EXPERIMENT: 1
Aim: To study android studio and installation of android studio. Create “Hello
World” application.

Android Studio is the official Integrated Development Environment (IDE) for Android app
development, based on IntelliJIDEA . On top of IntelliJ's powerful code editor and developer
tools, Android Studio offers even more features that enhance your productivity when building
Android apps, such as:

 A flexible Gradle-based build system

 A fast and feature-rich emulator

 A unified environment where you can develop for all Android devices

 Apply Changes to push code and resource changes to your running app without
restarting your app

 Code templates and GitHub integration to help you build common app features and
import sample code

 Extensive testing tools and frameworks

 Lint tools to catch performance, usability, version compatibility, and other problems

 C++ and NDK support

 Built-in support for Google Cloud Platform, making it easy to integrate Google Cloud
Messaging and App Engine

Installation of Android Studio:-

 If you downloaded an .exe file (recommended), double-click to launch it.


If you downloaded a .zip file, unpack the ZIP, copy the android-studio folder into your Program
Files folder, and then open the android-studio > bin folder and launch studio64.exe (for 64-bit
machines) or studio.exe (for 32-bit machines).

 Follow the setup wizard in Android Studio and install any SDK packages that it
recommends.

Installing the Java Development Kit:-

 JDK Alpha and Beta (1995): Sun Microsystem announced Java in September 23, 1995.
 JDK 1.0 (January 1996): Originally called Oak (named after the oak tree outside James
Gosling's office). Renamed to Java 1 in JDK 1.0.2.
 JDK 1.1 (February 1997): Introduced AWT event model, inner class, JavaBean, JDBC, and
RMI.
 J2SE 1.2 (JDK 1.2) (December 1998): Re-branded as "Java 2" and renamed JDK to J2SE (Java
2 Standard Edition). Also released J2EE (Java 2 Enterprise Edition) and J2ME (Java 2 Micro
Edition). Included JFC (Java Foundation Classes - Swing, Accessibility API, Java 2D, Pluggable
Look & Feel, and Drag & Drop). Also introduced Collection Framework and JIT compiler.
 J2SE 1.3 (JDK 1.3) (May 2000): Introduced Hotspot JVM.
 J2SE 1.4 (JDK 1.4) (February 2002): Introduced assert statement, non-blocking IO (nio),
logging API, image IO, Java webstart, regular expression (regex) support.
 J2SE 5.0 (JDK 5) (September 2004): Officially called 5.0 instead of 1.5 (by dropping the 1.).
Introduced generics, autoboxing/unboxing, annotation, enum, varargs, for-each loop, static
import. See "JDK 5 New Features".
 Java SE 6 (JDK 6) (December 2006): Renamed J2SE to Java SE (Java Platform Standard
Edition). No new language features. See "JDK 6 New Features".
 Java SE 7 (JDK 7) (July 2011): First version after Oracle purchased Sun Microsystem - aslo
called OracleJDK. Introduced Strings in switch statement, Binary integer literals, allowing
underscores in numeric literals, improved type inference for generic instance creation (or
diamond operator <>), Catching multiple exception types and rethrowing exceptions with
improved type checking. See "JDK 7 New Features".
 Java SE 8 (JDK 8) (LTS) (March 2014): Included support for Lambda expressions, default
and static methods in interfaces, improved collection, and JavaScript runtime. Also
integrated JavaFX graphics subsystem. See "JDK 8 New Features".
 Java SE 9 (JDK 9) (September 21, 2017): Introduced modularization of the JDK (module)
under project Jigsaw, the Java Shell (jshell), and more. See "JDK 9 New Features".
 Java SE 10 (18.3) (JDK 10) (March 2018): Introduced var for type inference local variable
(similar to JavaScript). Introduced time-based release versioning with two releases each
year, in March and September, denoted as YY.M. Removed native-header generation
tool javah. See "JDK 10 New Features".
 Java SE 11 (18.9) (LTS) (JDK 11) (September 2018): Extended var to lambda expression.
Standardize HTTP client in java.net.http. Support TLS 1.3. Clean up the JDK and the
installation package (removed JavaFX, JavaEE, CORBA modules, deprecated Nashorn
JavaScript engine). OracleJDK is no longer free for commercial use, but OpenJDK is still free.
See "JDK 11 New Features".
 Java SE 12 (19.3) (JDK 12) (March 2019): Switch Expression (preview). See "JDK 12 New
Features".
 Java SE 13 (19.9) (JDK 13) (September 2019): Switch Expression (preview), Multi-line Text
Block (preview). See "JDK 13 New Features".
 Java SE 14 (20.3) (JDK 14) (March 2020): Records (preview)

(b) Create “Hello World” application


Launch Android Studio if it is not already opened.

In the main Welcome to Android Studio window, click "Start a new Android Studio project.

In the New Project window, give your application an Application Name, such as "Hello World".
Verify the Project location, or choose a different directory for storing your project.

Choose a unique Company Domain.

Apps published to the Google Play Store must have a unique package name. Since domains are
unique, prepending your app's name with your or your company's domain name is going to
result in a unique package name.

If you are not planning to publish your app, you can accept the default example domain. Be
aware that changing the package name of your app later is extra work.

Verify that the default Project location is where you want to store your Hello World app and
other Android Studio projects, or change it to your preferred directory. Click Next.

On the Target Android Devices screen, "Phone and Tablet" should be selected. And you should
ensure that API 15: Android 4.0.3 IceCreamSandwich is set as the Minimum SDK. (Fix this if
necessary.)

At the writing of this book, choosing this API level makes your "Hello World" app compatible
with 97% of Android devices active on the Google Play Store.

These are the settings used by the examples in this book.

Click Next.
If your project requires additional components for your chosen target SDK, Android Studio will
install them automatically. Click Next.

Customize the Activity window. Every app needs at least one activity. An activity represents a
single screen with a user interface and Android Studio provides templates to help you get
started. For the Hello World project, choose the simplest template (as of this writing, the
"Empty Activity" project template is the simplest template) available.

It is a common practice to call your main activity MainActivity. This is not a requirement.

Make sure the Generate Layout file box is checked (if visible).Make sure the Backwards
Compatibility (App Compat) box is checked.

Leave the Layout Name as activity_main. It is customary to name layouts after the activity they
belong to. Accept the defaults and click Finish.

# 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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#2196F3"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

# MainActivity.java
package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


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

EXPERIMENT: 2
Aim: To understand activity, intent, create sample
application with login module. (Check username and
password)
# activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://sche mas.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/Name"
android:layout_width="369dp"
android:layout_height="62dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="113dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="29dp"
android:hint="Name"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/Password"
android:layout_width="372dp"
android:layout_height="55dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="29dp"
android:hint="Password"
app:layout_constraintTop_toBottomOf="@+id/etName" />
<Button
android:id="@+id/button"
android:layout_width="261dp"
android:layout_height="51dp"
android:layout_marginStart="75dp"
android:layout_marginLeft="75dp"
android:layout_marginEnd="75dp"
android:layout_marginRight="75dp"
android:layout_marginBottom="359dp"
android:text="Submit"
app:layout_constraintStart_toStartOf="parent" />
</LinearLayout>

# MainActivity.java
package com.example.login_page;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.jar.Attributes;

public class MainActivity extends AppCompatActivity {


private EditText NAME;
private EditText Password;
private Button LogIn;
String st;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NAME = (EditText) findViewById((R.id.Name));
Password = (EditText) findViewById(R.id.Password);
LogIn = (Button) findViewById(R.id.button);

LogIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

st = NAME.getText().toString();
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("Value",st);
startActivity(intent);
finish();
}
});

}
}
# activity_main2.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/ettext3"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">

<TextView
android:id="@+id/textView"
android:layout_width="168dp"
android:layout_height="47dp"
android:layout_marginTop="96dp"
android:text="Welcome"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

# Main2Activity.java
package com.example.login_page;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

String st;
TextView tv;

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

tv=findViewById(R.id.ettext3);
st=getIntent().getExtras().getString("Value");
tv.setText("Welcome "+st);

}
}
EXPERIMENT: 3
Aim: Design simple GUI application with activity and intents. e.g. Calculator
# 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:background="#ecf0f1"
android:orientation="vertical"
tools:context="com.example.calculator.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="horizontal">
<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/status_bar_item_app_background"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="0.2"
android:orientation="horizontal">
<Button
android:id="@+id/buttonDel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="Del"
android:textSize="20sp" />
<Button
android:id="@+id/buttoneql"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="="
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="1"
android:textSize="20sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="2"
android:textSize="20sp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="3"
android:textSize="20sp" />
<Button
android:id="@+id/buttondiv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="/"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="horizontal">
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="4"
android:textSize="20sp" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="5"
android:textSize="20sp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="6"
android:textSize="20sp" />
<Button
android:id="@+id/buttonsub"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="-"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="horizontal">
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="7"
android:textSize="20sp" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="8"
android:textSize="20sp" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="9"
android:textSize="20sp" />
<Button
android:id="@+id/buttonmul"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="x"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="horizontal">
<Button
android:id="@+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="."
android:textSize="20sp" />
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="0"
android:textSize="20sp" />
<Button
android:id="@+id/Remainder"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="%"
android:textSize="30sp" />
<Button
android:id="@+id/buttonadd"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:text="+"
android:textSize="30sp" />
</LinearLayout>

</LinearLayout>

MainActivity.java
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


double input1 = 0, input2 = 0;
TextView edt1;
boolean Addition, Subtract, Multiplication, Division, mRemainder, decimal;
Button button0, button1, button2, button3, button4, button5, button6, button7,
button8, button9, buttonAdd, buttonSub,buttonMul, buttonDivision,buttonEqual,
buttonDel, buttonDot, Remainder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonDot = (Button) findViewById(R.id.buttonDot);
buttonAdd = (Button) findViewById(R.id.buttonadd);
buttonSub = (Button) findViewById(R.id.buttonsub);
buttonMul = (Button) findViewById(R.id.buttonmul);
buttonDivision = (Button) findViewById(R.id.buttondiv);
Remainder = (Button) findViewById(R.id.Remainder);
buttonDel = (Button) findViewById(R.id.buttonDel);
buttonEqual = (Button) findViewById(R.id.buttoneql);
edt1 = (TextView) findViewById(R.id.display);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
edt1.setText(edt1.getText()+"1");
} });
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
edt1.setText(edt1.getText()+"2");
}});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
edt1.setText(edt1.getText() + "3");
}});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
edt1.setText(edt1.getText() + "4");
}});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
edt1.setText(edt1.getText() + "5");
}});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
edt1.setText(edt1.getText() + "6");
}});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
edt1.setText(edt1.getText() + "7");
}});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
edt1.setText(edt1.getText() + "8");
}});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
edt1.setText(edt1.getText() + "9");
}});
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
edt1.setText(edt1.getText() + "0");
}});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt1.getText().length() != 0) {
input1 = Float.parseFloat(edt1.getText() + "");
Addition = true;
decimal = false;
edt1.setText("+");
}}});
buttonSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt1.getText().length() != 0) {
input1 = Float.parseFloat(edt1.getText() + "");
Subtract = true;
decimal = false;
edt1.setText("-");
}}});
buttonMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt1.getText().length() != 0) {
input1 = Float.parseFloat(edt1.getText() + "");
Multiplication = true;
decimal = false;
edt1.setText("*");
}}});
buttonDivision.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt1.getText().length() != 0) {
input1 = Float.parseFloat(edt1.getText() + "");
Division = true;
decimal = false;
edt1.setText("/");
} }});
Remainder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt1.getText().length() != 0) {
input1 = Float.parseFloat(edt1.getText() + "");
mRemainder = true;
decimal = false;
edt1.setText("%");
}}});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Addition || Subtract || Multiplication || Division || mRemainder)
{
input2 = Float.parseFloat(edt1.getText() + "");
}
if (Addition) {
edt1.setText(input1 + input2 + "");
Addition = false;
}
if (Subtract) {
edt1.setText(input1 - input2 + "");
Subtract = false;
}
if (Multiplication) {
edt1.setText(input1 * input2 + "");
Multiplication = false;
}
if (Division) {
edt1.setText(input1 / input2 + "");
Division = false;
}
if (mRemainder) {
edt1.setText(input1 % input2 + "");
mRemainder = false;}
}});
buttonDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
input1 = 0;
input2 = 0;
}});
buttonDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (decimal) {
//do nothing or you can show the error
} else {
edt1.setText(edt1.getText() + ".");
decimal = true;
}}});}
}
EXPERIMENT: 4
Aim: Create an android app for database creation using SQLite Database.
# activity_main.xml
<?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"
android:layout_margin="30dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter Name" />

<EditText
android:layout_marginTop="5dp"
android:id="@+id/Surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_below="@+id/Name"
android:inputType="textPersonName"
android:hint="Enter Surname"/>
<LinearLayout
android:layout_below="@+id/Surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical">

<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="ADD" />

<Button
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Display" />
</LinearLayout>
</RelativeLayout>
# MainActivity.java
package com.example.sqliteapp;

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.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText Name,Surname;
Button Add,Display;
SqliteHelper mydb;

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

Name = findViewById(R.id.Name);
Surname = findViewById(R.id.Surname);

Add = findViewById(R.id.add);
Display = findViewById(R.id.display);

mydb = new SqliteHelper(this);


AddData();
ViewAll();

public void AddData(){

Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted=
mydb.insertData(Name.getText().toString(),Surname.getText().toString());

if (isInserted==true){

Toast.makeText(getApplicationContext(),"Data
Inserted",Toast.LENGTH_LONG).show();
}
else {

Toast.makeText(getApplicationContext(),"Data not
Inserted",Toast.LENGTH_LONG).show();
}

}
});
}

public void ViewAll(){


Display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Cursor res= mydb.getAllData();


if (res.getCount()==0){

showMessage("Error","Nothing Found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()){

buffer.append("Id:"+res.getString(0)+"\n");
buffer.append("Name:"+res.getString(1)+"\n\n");
buffer.append("Surname:"+res.getString(2)+"\n\n");
}
showMessage("Data",buffer.toString());
}

});

private void showMessage(String title, String message){

AlertDialog.Builder builder = new AlertDialog.Builder(this);


builder.create();
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}

# SqliteHelper
package com.example.sqliteapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class SqliteHelper extends SQLiteOpenHelper {

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


public static final String TABLE_NAME = "student_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public SqliteHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY
AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);


onCreate(db);

public boolean insertData(String name,String surname) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}

public Cursor getAllData() {


SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}

}
EXPERIMENT: 5
Aim: write an application that draws basic graphical primitives in the screen.
# 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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

# MainActivity.java
package com.example.graphics;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Test ts=new Test(this);
setContentView(ts);
}
}

# Test.java
package com.example.graphics;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.view.View;
import androidx.annotation.RequiresApi;

public class Test extends View {

public Test(Context context) {


super(context);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint=new Paint();
paint.setColor(Color.RED);
canvas.drawRect(400,400,500,500,paint);
canvas.drawCircle(100,300,50,paint);
canvas.drawLine(200,100,500,400,paint);
}
}

You might also like