MAD Lab
MAD Lab
1
[Link] Study and installation of Android Studio
Date:
Aim:
Click on the "I have read and agree with the above terms and conditions"
checkbox followed by the download button.
2
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, and once it is completed, it will be like the
image shown below.
3
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'.
4
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.
5
Click on the Next button.
6
The Android Studio has been successfully configured. Now it's time to launch and
build apps. Click on the Finish button to launch it.
Step 9: Click on Start a new Android Studio project to build a new app.
Result:
7
Ex. No: 02 Develop an application that uses GUI components, Font and Colors
Date:
Aim:
To develop a Simple Android Application that uses GUI components, Font and
Colors.
Procedure:
• Open Android Studio and then click on File -> New -> New project.
8
Designing layout for the Android Application:
• Now click on Design and your application will look as given below.
10
Code for [Link]:
package [Link].exno2
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity :AppCompatActivity() {
var ch = 1
var font = 30f
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val t = findViewById<TextView>([Link])
val b1 = findViewById<Button>([Link].button1)
[Link] {
[Link]= font
font += 5
if (font == 50f) font = 30f
}
val b2 = findViewById<Button>([Link].button2)
[Link] {
when (ch) {
1 ->[Link]([Link])
2 ->[Link]([Link])
3 ->[Link]([Link])
4 ->[Link]([Link])
5 ->[Link]([Link])
6 ->[Link]([Link])
}
ch++
if (ch == 7) ch = 1
11
}
}
}
Output:
Result:
Thus a Simple Android Application that uses Layout Managers and Event
Listeners is developed and executed successfully.
12
Ex. No. 03 Develop an application that uses Layout Managers and Event Listeners
Date:
Aim:
To develop a Simple Android Application that uses Layout Managers and Event
Listeners.
Procedure:
• Open Android Studio and then click on File -> New -> New project.
13
• Type the Activity Name asSecondActivity and click Finish button.
• Thus Second Activity For the application is created.
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
14
android:ems="10"
android:inputType="textPersonName"
android:hint="Name" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="Registration Number" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Submit" />
</RelativeLayout>
15
Designing Layout for Second Activity:
16
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
</LinearLayout>
• Now click on Design and your activity will look as given below.
• So now the designing part of Second Activity is also completed.
package [Link].exno3;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
//Defining the Views
EditText e1,e2;
Button bt;
Spinner s;
//Data for populating in Spinner
String [] dept_array={"CSE","ECE","IT","Mech","Civil"};
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
17
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Referring the Views
e1= (EditText) findViewById([Link]);
e2= (EditText) findViewById([Link].editText2);
bt= (Button) findViewById([Link]);
s= (Spinner) findViewById([Link]);
//Creating Adapter for Spinner for adapting the data from array to Spinner
ArrayAdapter adapter= new
ArrayAdapter([Link],[Link].simple_spinner_item,dept_array)
;
[Link](adapter);
//Creating Listener for Button
[Link](new [Link]() {
@Override
public void onClick(View v) {
//Getting the Values from Views(Edittext& Spinner)
name=[Link]().toString();
reg=[Link]().toString();
dept=[Link]().toString();
//Intent For Navigating to Second Activity
Intent i = new Intent([Link],[Link]);
//For Passing the Values to Second Activity
[Link]("name_key", name);
[Link]("reg_key",reg);
[Link]("dept_key", dept);
startActivity(i);
}
});
}
}
18
Java Coding for Second Activity:
package [Link].exno3;
import [Link];
import [Link];
import [Link];
import [Link];
public class SecondActivity extends AppCompatActivity {
TextView t1,t2,t3;
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
//Getting the Values from First Activity using the Intent received
name=[Link]("name_key");
reg=[Link]("reg_key");
dept=[Link]("dept_key");
19
//Setting the Values to Intent
[Link](name);
[Link](reg);
[Link](dept);
}
}
Output:
Result:
Thus a Simple Android Application that uses Layout Managers and Event
Listeners is developed and executed successfully.
20
Ex No. 04 Write an application that draws Basic Graphical Primitives on the
Date: screen
Aim:
Procedure:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno4″ and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
21
android:layout_height="match_parent"
android:id="@+id/imageView" />
</RelativeLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
package [Link].exno4
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
valbg = [Link](720, 1280, [Link].ARGB_8888)
vali = findViewById<ImageView>([Link])
[Link]= BitmapDrawable(resources, bg)
val canvas = Canvas(bg)
val paint = Paint()
[Link]= [Link]
[Link]= 50f
[Link]("Rectangle", 420f, 150f, paint)
22
[Link](400f, 200f, 650f, 700f, paint)
[Link]("Circle", 120f, 150f, paint)
[Link](200f, 350f, 150f, paint)
[Link]("Square", 120f, 800f, paint)
[Link](50f, 850f, 350f, 1150f, paint)
[Link]("Line", 480f, 800f, paint)
[Link](520f, 850f, 520f, 1150f, paint) }
}
Output:
Result:
23
Ex. No.05
Develop an application that makes use of database
Date:
Aim:
Procedure:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno5″ and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
24
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" />
25
<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"
26
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>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
27
Code for [Link]:
package [Link].exno5;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends 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)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
Rollno=(EditText)findViewById([Link]);
Name=(EditText)findViewById([Link]);
Marks=(EditText)findViewById([Link]);
Insert=(Button)findViewById([Link]);
Delete=(Button)findViewById([Link]);
Update=(Button)findViewById([Link]);
View=(Button)findViewById([Link]);
ViewAll=(Button)findViewById([Link]);
[Link](this);
28
[Link](this);
[Link](this);
[Link](this);
[Link](this);
// Creating database and table
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
[Link]("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([Link]().toString().trim().length()==0||
[Link]().toString().trim().length()==0||
[Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
[Link]("INSERT INTO student
VALUES('"+[Link]()+"','"+[Link]()+
"','"+[Link]()+"');");
showMessage("Success", "Record added");
clearText();
}
// Deleting a record from the Student table
if(view==Delete)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
29
{
showMessage("Error","Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]())
{
[Link]("DELETE FROM student WHERE rollno='"+[Link]()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error","InvalidRollno");
}
clearText();
}
// Updating a record in the Student table
if(view==Update)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
{
showMessage("Error","Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]()) {
[Link]("UPDATE student SET name='" + [Link]() + "',marks='" +
[Link]() +
"' WHERE rollno='"+[Link]()+"'");
showMessage("Success","Record Modified");
30
}
else {
showMessage("Error","InvalidRollno");
}
clearText();
}
// Display a record from the Student table
if(view==View)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
{
showMessage("Error","Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]())
{
[Link]([Link](1));
[Link]([Link](2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
// Displaying all the records
if(view==ViewAll)
{
Cursor c=[Link]("SELECT * FROM student", null);
if([Link]()==0)
31
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while([Link]())
{
[Link]("Rollno: "+[Link](0)+"\n");
[Link]("Name: "+[Link](1)+"\n");
[Link]("Marks:"+[Link](2)+"\n\n");
}
showMessage("Student Details", [Link]());
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
[Link](true);
[Link](title);
[Link](message);
[Link]();
}
public void clearText()
{
[Link]("");
[Link]("");
[Link]("");
[Link](); }
}
32
Output:
33
Result:
Aim:
Procedure:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno6″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
36
Code for [Link]:
package [Link].exno6;
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
37
// Permission is granted. Continue the action or workflow in your
// app.
} else {
// Explain to the user that the feature is unavailable because the
// features requires a permission that the user has denied. At the
// same time, respect the user's decision. Don't link to system
// settings in an effort to convince the user to change their
// decision.
}
}
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
valbtnSimpleNotification: Button = findViewById([Link])
valbtnNotificationIcon: Button = findViewById([Link])
valbtnNotificationImage: Button = findViewById([Link])
valbtnNotificationWithGroupConvo: Button =
findViewById([Link])
valbtnNotificationSemantic: Button = findViewById([Link])
charSequence = [Link]
[Link](this)
[Link](this)
[Link](this)
[Link](this)
[Link](this)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as
NotificationManager
if ([Link].SDK_INT>= Build.VERSION_CODES.O) {
val name: CharSequence = "My Notification"
38
[Link]= description
[Link](channel)
builder = [Link](this@MainActivity, [Link])
.setSmallIcon([Link].ic_launcher)
} else {
builder = [Link](this@MainActivity, "1")
.setSmallIcon([Link].ic_launcher)
}
}
override fun onClick(v: View?) {
when (v?.id) {
[Link]->simpleNotification()
[Link]->notificationWithIcon()
[Link]->notificationWithImage()
[Link]->notificationWithGroupConvo()
[Link]->notificationSemantic()
}
}
private fun simpleNotification() {
valjd = [Link]().setName("JournalDev ").setImportant(true).build()
val style = [Link](jd)
.addMessage("Check me out", Date().time, jd)
[Link](style)
askForNotificationPermission()
[Link](1, [Link]())
}
private fun notificationWithIcon() {
valanupam = [Link]()
.setName("Anupam")
.setIcon([Link](this,
[Link].ic_launcher_background))
.setImportant(true).build()
val style = [Link](anupam)
39
.addMessage("Check out my latest article!", Date().time, anupam)
[Link](style)
askForNotificationPermission()
[Link](2, [Link]())
}
private fun notificationWithImage() {
val bot = [Link]()
.setName("Bot")
.setBot(true)
.setImportant(true)
.build()
valuri = [Link]("[Link]://[Link].exno5/drawable/" +
[Link].ic_launcher_background)
val message = [Link]("Check out my latest
article!", Date().time, bot)
[Link]("image/*", uri)
val style = [Link](bot)
.addMessage(message)
.setGroupConversation(true)
[Link](style)
askForNotificationPermission()
[Link](3, [Link]())
}
private fun notificationWithGroupConvo() {
valjd = [Link]()
.setName("JournalDev").build()
valanupam = [Link]()
.setName("Anupam")
.setIcon([Link](this,
[Link].ic_launcher_foreground))
.setImportant(true).build()
val bot = [Link]()
.setName("Bot").setBot(true).build()
40
valuri = [Link]("[Link]://[Link].exno5/drawable/" +
[Link].ic_launcher_background)
val message = [Link]("", Date().time, bot)
[Link]("image/*", uri)
val style = [Link](bot)
.addMessage("Hi. How are you?", Date().time, anupam)
.addMessage(message)
.addMessage("Does this image look good?", Date().time, bot)
.addMessage("Looks good!", Date().time, jd)
.setGroupConversation(true)
.setConversationTitle("Sample Conversation")
[Link](style)
askForNotificationPermission()
[Link](4, [Link]())
}
private fun notificationSemantic() {
valjd = [Link]()
.setName("JournalDev")
.build()
valanupam = [Link]()
.setName("Anupam")
.setIcon([Link](this,
[Link].ic_launcher_background))
.setImportant(true)
.build()
val bot = [Link]()
.setName("Bot")
.setBot(true)
.build()
valuri = [Link]("[Link]://[Link].exno5/drawable/" +
[Link].ic_launcher_background)
val intent = Intent(this, MainActivity::[Link])
[Link]("hi", "Notifications were read")
41
valpendingIntent = [Link](this, 0, intent,
PendingIntent.FLAG_IMMUTABLE)
val message = [Link]("", Date().time, bot)
[Link]("image/*", uri)
valreplyAction =
[Link](
[Link].ic_launcher_background, "MARK READ", pendingIntent
)
.setSemanticAction([Link].SEMANTIC_ACTION_MARK_AS_RE
AD)
.build()
valseparateBuilder = [Link](this, "1")
.setSmallIcon([Link].ic_launcher)
.addAction(replyAction)
val style = [Link](bot)
.addMessage("Hi. How are you?", Date().time, anupam)
.addMessage(message)
.addMessage("Does this image look good?", Date().time, bot)
.addMessage("Looks good!", Date().time, jd)
.setGroupConversation(true)
.setConversationTitle("Sample Conversation")
[Link](style)
askForNotificationPermission()
[Link](5,[Link]())
}
override fun onResume() {
[Link]()
if (intent?.extras!= null) {
val value = [Link]("hi")
else {
// Directly ask for the permission
[Link]([Link].POST_NOTIFICATIONS)
}
}
}
}
43
Output:
Result:
44
Ex. No. 07 Implement an application that uses Multi-threading
Date:
Aim:
Procedure:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno7″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
45
android:layout_height="250dp"
android:layout_margin="50dp"
android:layout_gravity="center" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load Image 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load image 2" />
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
package [Link].exno7;
import [Link]
import [Link]
import [Link]
46
import [Link]
import [Link]
import [Link]
imageView = findViewById([Link])
button = findViewById([Link])
button2 = findViewById([Link].button2)
[Link] {
Thread {
try {
valurl = URL("[Link]
7236918_1280.jpg")
val bitmap =
[Link]([Link]().getInputStream())
[Link] {
[Link](bitmap)
}
} catch (e: Exception) {
[Link]()
}
}.start()
}
47
[Link] {
Thread {
try {
valurl = URL("[Link]
map-template_762986-[Link]?semt=ais_rp_50_assets&w=740&q=80")
val bitmap =
[Link]([Link]().getInputStream())
[Link] {
[Link](bitmap)
}
} catch (e: Exception) {
[Link]()
}
}.start()
}
}
}
Note:
• Before running the application, copy the images given below and paste it in
“app -> res -> drawable” by pressing “right click mouse button on
drawable” and selecting the “Paste” option.
48
Output:
Result:
49
Ex. No. 08 Develop a native application that uses GPS location information
Date:
Aim:
Procedure:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno8″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
50
android:text = "getlocation"/>
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Exno7">
<activity
android:name=".MainActivity"
51
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
package [Link].exno8;
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
52
try {
if ([Link](this, mPermission) !=
PackageManager.PERMISSION_GRANTED) {
[Link](this, arrayOf(mPermission),
REQUEST_CODE_PERMISSION)
}
} catch (e: Exception) {
[Link]()
}
btnShowLocation = findViewById([Link])
[Link] {
gps = GPSTracker(this@MainActivity)
if (gps!!.canGetLocation()) {
val latitude = gps!!.getLatitude()
val longitude = gps!!.getLongitude()
[Link](
applicationContext,
"Your Location is - \nLat: $latitude\nLong: $longitude",
Toast.LENGTH_LONG
).show()
} else {
gps!!.showSettingsAlert()
}
}
}
override fun onDestroy() {
[Link]()
gps?.stopUsingGPS() }
}
53
Code for [Link]:
package [Link].exno7;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@SuppressLint("MissingPermission")
54
public void getLocation() {
try {
locationManager = (LocationManager)
[Link](LOCATION_SERVICE);
if (locationManager != null) {
// getting GPS status
booleanisGPSEnabled =
[Link](LocationManager.GPS_PROVIDER);
// getting network status
booleanisNetworkEnabled =
[Link](LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled || isNetworkEnabled) {
[Link] = true;
// First get location from Network Provider
if (isNetworkEnabled) {
[Link](
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
location =
[Link](LocationManager.NETWORK_PROVIDER)
;
if (location != null) {
latitude = [Link]();
longitude = [Link]();
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled&& location == null) {
[Link](
LocationManager.GPS_PROVIDER,
55
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
location =
[Link](LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = [Link]();
longitude = [Link]();
}
}
}
}
} catch (Exception e) {
Log.e("GPSTracker", "Error getting location", e);
}
}
public booleancanGetLocation() {
return [Link];
}
Output:
Result:
Aim:
Procedure:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno9″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<EditText
59
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30dp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Write Data"
android:textSize="30dp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Read data"
android:textSize="30dp" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Clear"
android:textSize="30dp" />
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
60
Adding permissions in Manifest for the Android Application:
61
• So now the Permissions are added in the Manifest.
package [Link].exno9;
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
62
val message = [Link]()
try {
val f = File(getExternalFilesDir(null), "[Link]")
valfout = FileOutputStream(f)
[Link]([Link]())
[Link]()
[Link](baseContext, "Data Written in External Storage",
Toast.LENGTH_LONG).show()
} catch (e: Exception) {
[Link](baseContext, [Link], Toast.LENGTH_LONG).show()
}
}
[Link] {
var message: String?
valbuf = StringBuilder()
try {
val f = File(getExternalFilesDir(null), "[Link]")
val fin = FileInputStream(f)
val br = BufferedReader(InputStreamReader(fin))
while ([Link]().also { message = it } != null) {
[Link](message)
}
[Link]([Link]())
[Link]()
[Link]()
[Link](baseContext, "Data Received from External Storage",
Toast.LENGTH_LONG).show()
} catch (e: Exception) {
[Link](baseContext, [Link], Toast.LENGTH_LONG).show()
}
}
[Link] {
[Link]("") }
63
}
}
Output:
Result:
Thus Android Application that writes data to the SD Card is developed and
executed successfully.
64
Ex. No.10 Implement an application that creates an alert upon receiving a
Date: message
Aim:
Procedure:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno10″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<TextView
android:layout_width="wrap_content"
65
android:layout_height="wrap_content"
android:text="Message"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:layout_gravity="center"
android:text="Notify"
android:textSize="30sp"/>
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
package [Link].exno10;
import [Link]
import [Link]
import [Link]
66
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
valnotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
[Link](0, [Link]())
}
}
private fun createNotificationChannel() {
if ([Link].SDK_INT>= Build.VERSION_CODES.O) {
val name = "Default Channel"
valdescriptionText = "Channel for app notifications"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel("default_channel", name, importance).apply {
description = descriptionText
}
valnotificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
[Link](channel)
}
}
}
68
Output:
Result:
69
Ex. No. 11 Write a mobile application that makes use of RSS Feed
Date:
Aim:
Procedure:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno11″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
70
android:layout_height="match_parent"/>
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
71
• So now the Permissions are added in the Manifest.
package [Link].exno11;
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
72
// If we set a layout, it must contain a ListView with id android:id/list.
setContentView([Link].activity_main)
MyAsyncTask().execute()
}
private inner class MyAsyncTask : AsyncTask<Unit, Void, List<String>>() {
override fun doInBackground(vararg params: Unit): List<String>? {
valnewHeadlines = ArrayList<String>()
valnewLinks = ArrayList<String>()
try {
// Using a more reliable RSS feed for testing if needed
valurl = URL("[Link]
val factory = [Link]()
[Link]= false
valxpp = [Link]()
valinputStream = getInputStream(url)
if (inputStream == null) return null
[Link](inputStream, "UTF-8")
var insideItem = false
var eventType = [Link]
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
when {
[Link]("item", ignoreCase = true) ->insideItem = true
[Link]("title", ignoreCase = true) -> {
if (insideItem) [Link]([Link]())
}
[Link]("link", ignoreCase = true) -> {
if (insideItem) [Link]([Link]())
}
}
} else if (eventType ==
XmlPullParser.END_TAG&&[Link]("item", ignoreCase = true)) {
insideItem = false
73
}
eventType = [Link]()
}
headlines = newHeadlines
links = newLinks
return headlines
} catch (e: Exception) {
Log.e("MainActivity", "Error parsing RSS", e)
}
return null
}
override fun onPostExecute(result: List<String>?) {
if (result != null) {
val adapter = ArrayAdapter(this@MainActivity,
[Link].simple_list_item_1, result)
listAdapter= adapter
}
}
}
override fun onListItemClick(l: ListView, v: View, position: Int, id: Long) {
if (position <[Link]) {
valuri = [Link](links[position])
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
}
}
private fun getInputStream(url: URL): InputStream? {
return try {
val connection = [Link]()
[Link]= 5000
[Link]= 5000
[Link]()
} catch (e: IOException) { null
74
}
}
}
Output:
Result:
Thus Android Application that makes use of RSS Feed is developed and
executed successfully.
75
Ex. No. 12 Develop a mobile application to send an email
Date:
Aim:
Procedure:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno12″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
76
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"/>
<EditText
android:id="@+id/txtSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
<EditText
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Message"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Send"
android:id="@+id/btnSend"/>
</LinearLayout>
77
package="[Link].exno12" >
<uses-permission android:name="[Link]"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Exno11"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name="[Link]"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]"/>
</intent-filter>
<intent-filter>
<action android:name="[Link]"/>
<category android:name="[Link]"/>
<data android:mimeType="message/rfc822"/>
</intent-filter>
</activity>
</application>
</manifest>
78
Code for [Link]:
package [Link].exno12;
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
Result:
80
Ex. No. 13 Develop a Simple Android Application for Native Calculator
Date:
Aim:
Procedure:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno13″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
81
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"
82
android:layout_weight="1"
android:text="+"
android:textSize="30sp"/>
<Button
android:id="@+id/Sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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"
83
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Answer is"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
package [Link].exno13;
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
84
private lateinit var div: Button
private lateinit var result: TextView
[Link](this)
[Link](this)
[Link](this)
[Link](this)
}
86
Output:
Result:
87