Mad Lab Manual
Mad Lab Manual
DEVAGIRI, HAVERI-581110
LAB MANUAL
MOBILE APLICATION
DEVELOPMENT
(18CSMP68)
6th Semester CBCS 2018 Scheme
Prepared by
Ms. Chaitra N.Barki
‘Instructions to the Candidates’
1. Introduction
2. Android Studio Installation
Part-A
1. Create an application to design a Visiting Card.
2. Develop an Android application for designing a calculator.
3. Create a SIGN UP activity with Username and password.
4. Develop an application to set an image as wallpaper.
5. Create an activity with two buttons START and STOP (Displaying the
number from one).
6. Develop an application to create an activity with two buttons to
parse the XML and JSON files.
7. Create a button called “Convert Text to Speech” that converts the
user input text into voice.
8. Create an activity like a phone dialer with CALL and SAVE button.
Part-B(Mini Project)
9. Write a program to enter Medicine Name, Date and Time of the Day as
input from the user and store it in the SQLite database.
10. Develop a content provider application with an activity called “Meeting
Schedule”
11. Create an application to receive an incoming SMS which is notified to the
user.
12. Write a program to create an activity having a Text box, and also Save,
Open and Create buttons.
13. Create an application to demonstrate a basic media playerthat allows the
user to Forward, Backward, Play and Pause an audio.
14. Develop an application to demonstrate the use of Asynchronous tasks in
android.
15. Develop an application that makes use of the clipboard framework for
copying and pasting of the text.
16. Create an AIDL service that calculates Car Loan EMI
Mobile Application Development (18CSMP68) 2020-21
INTRODUCTION
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
Lint tools to catch performance, usability, version compatibility, and other problems
Built-in support for Google Cloud Platform, making it easy to integrate Google Cloud
Messaging and App Engine
Project structure
Each project in Android Studio contains one or more modules with source code files
and resource files. Types of modules include:
Library modules
By default, Android Studio displays your project files in the Android project view, as shown
in figure 1.
This view is organized by modules to provide quick access to your project's key source files.
All the build files are visible at the top level under Gradle Scripts and each app module
contains the following folders:
java: Contains the Java source code files, including JUnit test code.
res: Contains all non-code resources, such as XML layouts, UI strings, and bitmap images.
The Android Studio main window is made up of several logical areas identified in figure 2.
1. The toolbar lets you carry out a wide range of actions, including running your app and
launching Android tools.
2. The navigation bar helps you navigate through your project and open files for editing. It
provides a more compact view of the structure visible in the Project window.
3. The editor window is where you create and modify code. Depending on the current file
type, the editor can change. For example, when viewing a layout file, the editor displays
the Layout Editor.
4. The tool window bar runs around the outside of the IDE window and contains the
buttons that allow you to expand or collapse individual tool windows.
5. The tool windows give you access to specific tasks like project management, search,
version control, and more. You can expand them and collapse them.
6. The status bar displays the status of your project and the IDE itself, as well as any
warnings or messages.
Windows
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).
2. Follow the setup wizard in Android Studio and install any SDK packages that it recommends.
Mac
2. Drag and drop Android Studio into the Applications folder, then launch Android Studio.
3. Select whether you want to import previous Android Studio settings, then click OK.
4. The Android Studio Setup Wizard guides you through the rest of the setup, which includes
downloading Android SDK components that are required for development.
Linux
1. Unpack the .zip file you downloaded to an appropriate location for your applications, such as
within /usr/local/ for your user profile, or /opt/ for shared users.
If you're using a 64-bit version of Linux, make sure you first install the required libraries for
64-bit machines.
2. To launch Android Studio, open a terminal, navigate to the android-studio/bin/ directory, and
execute studio.sh.
3. Select whether you want to import previous Android Studio settings or not, then click OK.
4. The Android Studio Setup Wizard guides you through the rest of the setup, which includes
downloading Android SDK components that are required for development.
If you are running a 64-bit version of Ubuntu, you need to install some 32-bit libraries with
the following command:
PART A
XML-CODE
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="17dp"
Department of CSE, GEC Haveri Page 6
Mobile Application Development(18CSMP68) 2020-21
android:layout_marginLeft="17dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="244dp"
android:layout_marginRight="244dp"
android:layout_marginBottom="486dp"
android:text="VVCE"
android:textSize="38dp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="231dp"
android:layout_height="174dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-14dp"
android:layout_marginRight="-14dp"
android:layout_marginBottom="481dp"
app:srcCompat="@drawable/logo" />
<View
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="4dp"
android:layout_alignParentBottom="true"
android:background="#4444"
android:layout_marginBottom="466dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
Department of CSE, GEC Haveri Page 7
Mobile Application Development(18CSMP68) 2020-21
android:layout_marginEnd="117dp"
android:layout_marginRight="117dp"
android:layout_marginBottom="394dp"
android:text="Nithin Kumar"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView3"
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="64dp"
android:layout_marginRight="64dp"
android:layout_marginBottom="343dp"
android:text="Assistant Professor-CSE"
android:textSize="25dp" />
<TextView
android:id="@+id/textView4"
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="127dp"
android:layout_marginRight="127dp"
android:layout_marginBottom="294dp"
android:text="Ph No: 8050462225"
android:textSize="20dp" />
<TextView
android:id="@+id/textView5"
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="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="229dp"
android:text="Kannada Sahitya Parishath Road Gokulam 3rd Stage Mysuru-02"
android:textSize="20dp" />
<TextView
android:id="@+id/textView6"
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="44dp"
android:layout_marginRight="44dp"
android:layout_marginBottom="189dp"
android:text="Email: [email protected]"
android:textSize="20dp" />
</RelativeLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
OUTPUT:
XML-CODE:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="98dp"
android:layout_marginBottom="653dp"
android:text="SIMPLE CALCI"
android:textSize="38dp"
app:layout_constraintBottom_toBottomOf="parent"
Department of CSE, GEC Haveri Page 11
Mobile Application Development(18CSMP68) 2020-21
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.042" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="115dp"
android:layout_marginBottom="547dp"
android:ems="10"
android:hint="Enter the First Number"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="111dp"
android:layout_marginBottom="455dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter the Second Number"
android:text="" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
Department of CSE, GEC Haveri Page 12
Mobile Application Development(18CSMP68) 2020-21
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="203dp"
android:layout_marginBottom="350dp"
android:text="0"
android:textSize="40dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="274dp"
android:layout_marginBottom="237dp"
android:onClick="doAdd"
android:text="ADD" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="68dp"
android:layout_marginBottom="233dp"
android:onClick="doSub"
android:text="SUB" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
Department of CSE, GEC Haveri Page 13
Mobile Application Development(18CSMP68) 2020-21
android:layout_alignParentBottom="true"
android:layout_marginEnd="277dp"
android:layout_marginBottom="115dp"
android:onClick="doMul"
android:text="MUL" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="63dp"
android:layout_marginBottom="104dp"
android:onClick="doDiv"
android:text="DIV" />
</RelativeLayout>
JAVA-CODE:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText e1,e2;
TextView tv1;
@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);
tv1 = (TextView)findViewById(R.id.textView1);
}
public void doAdd(View V){
int a1 = Integer.parseInt(e1.getText().toString());
int a2 = Integer.parseInt(e2.getText().toString());
int result= a1+a2;
tv1.setText(""+result);
}
public void doSub(View V){
int a1 = Integer.parseInt(e1.getText().toString());
int a2 = Integer.parseInt(e2.getText().toString());
int result= a1-a2;
tv1.setText(""+result);
}
public void doMul(View V){
int a1 = Integer.parseInt(e1.getText().toString());
int a2 = Integer.parseInt(e2.getText().toString());
int result= a1*a2;
tv1.setText(""+result);
}
public void doDiv(View V){
int a1 = Integer.parseInt(e1.getText().toString());
int a2 = Integer.parseInt(e2.getText().toString());
float result= a1/a2;
tv1.setText(""+result);
}
}
OUTPUT:
On successful SIGN UP proceed to the next Login activity. Here the user
should SIGN IN using the Username and Password created during signup
activity. If the Username and Password are matched then navigate to the next
activity which displays a message saying “Successful Login” or else display a
toast message saying “Login Failed”. The user is given only two attempts and
after that display a toast message saying “Failed Login Attempts” and disable
the SIGN IN button. Use Bundle to transfer information from one activity to
another.
I. SignUp Activity
activity_signup.xml
<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"
Department of CSE, GEC Haveri Page 17
Mobile Application Development(18CSMP68) 2020-21
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="299dp"
android:layout_height="69dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="73dp"
android:layout_marginBottom="606dp"
android:text="SIGNUP PAGE"
android:textAlignment="center"
android:textColor="#120707"
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/emailEditText"
android:layout_width="308dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="42dp"
android:layout_marginBottom="475dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textSize="30sp" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="306dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="41dp"
android:layout_marginBottom="374dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
android:textSize="30sp" />
Department of CSE, GEC Haveri Page 19
Mobile Application Development(18CSMP68) 2020-21
<Button
android:id="@+id/signUpBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="125dp"
android:layout_marginBottom="231dp"
android:text="SIGN UP"
android:textSize="30sp" />
</RelativeLayout>
SignUpActivity.java
import .AppCompatActivity;
import .Intent;
import .Bundle;
import .View;
import .Button;
import .EditText;
import .Toast;
import .Pattern;
EditTextemailEditText, passwordEditText;
Button signUpBtn;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.passwordEditText);
signUpBtn = findViewById(R.id.signUpBtn);
signUpBtn.setOnClickListener(new View.OnClickListener() {
@Override
if (!isValidPassword(password)) {
return;
intent.putExtra("email", email);
intent.putExtra("password", password);
startActivity(intent);
});
if (password.length() < 8) {
return false;
if (!lowerCase.matcher(password).matches()) {
return false;
if (!upperCase.matcher(password).matches()) {
return false;
if (!number.matcher(password).matches()) {
return false;
}
Department of CSE, GEC Haveri Page 22
Mobile Application Development(18CSMP68) 2020-21
if (!specialCharacter.matcher(password).matches()) {
return false;
return true;
<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=".LoginActivity">
<TextView
android:id="@+id/textView"
android:layout_width="217dp"
android:layout_height="80dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
Department of CSE, GEC Haveri Page 23
Mobile Application Development(18CSMP68) 2020-21
android:layout_marginEnd="111dp"
android:layout_marginBottom="582dp"
android:text="LOGIN PAGE"
android:textAlignment="center"
android:textColor="#100202"
android:textSize="36sp" />
<EditText
android:id="@+id/emailEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="22dp"
android:layout_marginBottom="461dp"
android:ems="10"
android:hint="Enter Email"
android:inputType="textPersonName"
android:text=""
android:textSize="30sp" />
<EditText
android:id="@+id/passwordEditText"
Department of CSE, GEC Haveri Page 24
Mobile Application Development(18CSMP68) 2020-21
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="18dp"
android:layout_marginBottom="366dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
android:textSize="30sp" />
<Button
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="116dp"
android:layout_marginBottom="235dp"
android:text="LOGIN"
android:textSize="36sp" />
</RelativeLayout>
LoginActivity.java
import .AppCompatActivity;
import .Intent;
import .Bundle;
import .View;
import .Button;
import .EditText;
import .Toast;
Button loginBtn;
int counter=0;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginBtn = findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
Department of CSE, GEC Haveri Page 26
Mobile Application Development(18CSMP68) 2020-21
if (registeredEmail.equals(email) &®isteredPassword.equals(passwor
d)) {
y.class);
startActivity(intent);
} else {
NGTH_SHORT).show();
counter--;
if(counter==0){
Toast.makeText(getBaseContext(),"Failed Login Attemptes",
Toast.LENGTH_LONG).show();
}
});
<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=".LoginSuccessActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="307dp"
android:layout_height="216dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="67dp"
android:layout_marginBottom="413dp"
android:text="Login Successful"
android:textColor="#E91E63"
android:textSize="40dp" />
</RelativeLayout>
import .AppCompatActivity;
import .Bundle;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_success);
} }
OUTPUT:
XML CODE:
<TextView
android:layout_width="wrap_content"
android:layout_height="96dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="40dp"
android:layout_marginBottom="556dp"
android:text="Changing Wallpaper"
android:textAlignment="center"
android:textColor="#970435"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="280dp"
android:layout_height="86dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="55dp"
android:layout_marginBottom="373dp"
android:text="Click here to chnage wallpaper"
android:textSize="18sp" />
</RelativeLayout>
JAVA CODE:
import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
Button changewallpaper;
Timer mytimer;
Drawable drawable;
WallpaperManagerwpm;
int prev=1;
@Override
setContentView(R.layout.activity_main);
wpm = WallpaperManager.getInstance(this);
changewallpaper = findViewById(R.id.button);
changewallpaper.setOnClickListener(new View.OnClickListener() {
setWallpaper();
});
mytimer.schedule(new TimerTask() {
@Override
if(prev==1) {
drawable = getResources().getDrawable(R.drawable.one);
prev = 2;
}
Department of CSE, GEC Haveri Page 34
Mobile Application Development(18CSMP68) 2020-21
else if(prev==2) {
drawable = getResources().getDrawable(R.drawable.two);
prev=3;
else if(prev==3) {
drawable = getResources().getDrawable(R.drawable.three);
prev=4;
else if(prev==4) {
drawable = getResources().getDrawable(R.drawable.four);
prev=5;
else if(prev==5) {
drawable = getResources().getDrawable(R.drawable.five);
prev=1;
try {
wpm.setBitmap(wallpaper);
} catch (IOException e) {
e.printStackTrace();
},0,30000); } }
OUTPUT:
XML CODE:
<TextView
android:layout_width="311dp"
android:layout_height="69dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="41dp"
android:layout_marginBottom="591dp"
android:text="COUNTER APP"
android:textAlignment="center"
android:textColor="#120808"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="275dp"
android:layout_height="136dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="66dp"
android:layout_marginBottom="399dp"
android:text="Counter Value"
android:textAlignment="center"
android:textSize="36sp" />
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
Department of CSE, GEC Haveri Page 38
Mobile Application Development(18CSMP68) 2020-21
android:layout_marginEnd="253dp"
android:layout_marginBottom="291dp"
android:text="START"
android:textSize="18sp" />
<Button
android:id="@+id/btnSTOP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="52dp"
android:layout_marginBottom="289dp"
android:text="STOP"
android:textSize="18sp" />
</RelativeLayout>
JAVA CODE:
package com.example.a5a;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
Department of CSE, GEC Haveri Page 39
Mobile Application Development(18CSMP68) 2020-21
TextViewtxtcounter;
int i = 1;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtcounter = findViewById(R.id.textView1);
btnstart = findViewById(R.id.btn_start);
btnstop = findViewById(R.id.btn_stop);
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
customHandler.postDelayed(updateTimerThread,0);
});
btnstop.setOnClickListener(new View.OnClickListener() {
@Override
customHandler.removeCallbacks(updateTimerThread);
}
Department of CSE, GEC Haveri Page 40
Mobile Application Development(18CSMP68) 2020-21
});
@Override
txtcounter.setText(""+i);
customHandler.postDelayed(this,1000);
i++;
};
OUTPUT:
Program – 6: Create two files of XML and JSON type with values for
City_Name, Latitude, Longitude, Temperature, and Humidity. Develop an
application to create an activity with two buttons to parse the XML and JSON
files which when clicked should display the data in their respective layouts
side by side.
XML CODE:
<TextView
android:layout_width="352dp"
android:layout_height="56dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="21dp"
android:layout_marginBottom="617dp"
android:text="Parsing Application"
android:textAlignment="center"
android:textColor="#071CA8"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="251dp"
android:layout_marginBottom="518dp"
android:onClick="parsexml"
android:text="Parse XML" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
Department of CSE, GEC Haveri Page 43
Mobile Application Development(18CSMP68) 2020-21
android:layout_alignParentBottom="true"
android:layout_marginEnd="50dp"
android:layout_marginBottom="516dp"
android:onClick="parseJSON"
android:text="PARSE JSON" />
<TextView
android:id="@+id/display"
android:layout_width="342dp"
android:layout_height="426dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="31dp"
android:layout_marginBottom="54dp"
android:textColor="#5D0C6A"
android:textSize="24sp"
android:textStyle="italic" />
</RelativeLayout>
JAVA CODE:
package com.example.parseapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display=findViewById(R.id.display);
}
Department of CSE, GEC Haveri Page 45
Mobile Application Development(18CSMP68) 2020-21
stringBuilder.append("\nLongitude:").append(getValue("long",element));
stringBuilder.append("\nTemperature:").append(getValue("temperature",element));
stringBuilder.append("\nHumidity:").append(getValue("humidity",element));
stringBuilder.append("\n-------");
Department of CSE, GEC Haveri Page 46
Mobile Application Development(18CSMP68) 2020-21
}
}
display.setText(stringBuilder.toString());
} catch (Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this,"Error Parsing
XML",Toast.LENGTH_SHORT).show();
}
}
public void parseJSON(View v){
String json;
StringBuilder stringBuilder=new StringBuilder();
try{
InputStream is=getAssets().open("city.json");
int size=is.available();
byte[]buffer=new byte[size];
is.read(buffer);
json=new String(buffer,StandardCharsets.UTF_8);
JSONArray jsonArray=new JSONArray(json);
stringBuilder.append("JSON DATA");
stringBuilder.append("\n-------");
for (int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
stringBuilder.append("\nName:").append(jsonObject.getString("name"));
stringBuilder.append("\nLatitude:").append(jsonObject.getString("lat"));
stringBuilder.append("\nLongitude:").append(jsonObject.getString("long"));
Department of CSE, GEC Haveri Page 47
Mobile Application Development(18CSMP68) 2020-21
stringBuilder.append("\nTemperature:").append(jsonObject.getString("temp"));
stringBuilder.append("\nHumidity:").append(jsonObject.getString("humi"));
stringBuilder.append("\n--------");
}
display.setText(stringBuilder.toString());
is.close();
} catch (Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this,"Error in parsing JSON
data",Toast.LENGTH_SHORT).show();
}
}
private String getValue(String tag, Element element){
return
element.getElementsByTagName(tag).item(0).getChildNodes().item(0).getNodeV
alue();
}
}
OUTPUT:
XML CODE:
<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="243dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="72dp"
android:layout_marginBottom="582dp"
android:text="Text2Speech"
android:textAlignment="center"
android:textColor="#0E0101"
android:textSize="36sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="283dp"
android:layout_height="67dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="63dp"
android:layout_marginBottom="361dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
Department of CSE, GEC Haveri Page 51
Mobile Application Development(18CSMP68) 2020-21
android:textSize="30sp"
android:textStyle="italic" />
<Button
android:id="@+id/button"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="66dp"
android:layout_marginBottom="205dp"
android:onClick="convert"
android:text="Convert to Speech"
android:textSize="18sp" />
</RelativeLayout>
JAVA CODE:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.EditText;
import java.util.Locale;
TextToSpeech t1;
EditText e1;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=findViewById(R.id.editText);
@Override
if (status!=TextToSpeech.ERROR){
t1.setLanguage(Locale.UK);
});
t1.speak(tospeak,TextToSpeech.QUEUE_FLUSH,null);
OUTPUT:
XML CODE:
<TextView
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="94dp"
android:layout_marginBottom="628dp"
android:text="Call Application"
android:textAlignment="center"
android:textColorLink="#100808"
android:textSize="30sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="286dp"
android:layout_marginBottom="403dp"
android:onClick="inputNumber"
android:text="1" />
<Button
android:id="@+id/button2"
Department of CSE, GEC Haveri Page 56
Mobile Application Development(18CSMP68) 2020-21
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="177dp"
android:layout_marginBottom="406dp"
android:onClick="inputNumber"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="63dp"
android:layout_marginBottom="406dp"
android:onClick="inputNumber"
android:text="3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="285dp"
Department of CSE, GEC Haveri Page 57
Mobile Application Development(18CSMP68) 2020-21
android:layout_marginBottom="342dp"
android:onClick="inputNumber"
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="173dp"
android:layout_marginBottom="338dp"
android:onClick="inputNumber"
android:text="5" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="59dp"
android:layout_marginBottom="335dp"
android:onClick="inputNumber"
android:text="6" />
<Button
Department of CSE, GEC Haveri Page 58
Mobile Application Development(18CSMP68) 2020-21
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="287dp"
android:layout_marginBottom="276dp"
android:onClick="inputNumber"
android:text="7" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="173dp"
android:layout_marginBottom="276dp"
android:onClick="inputNumber"
android:text="8" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
Department of CSE, GEC Haveri Page 59
Mobile Application Development(18CSMP68) 2020-21
android:layout_marginEnd="62dp"
android:layout_marginBottom="276dp"
android:onClick="inputNumber"
android:text="9" />
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="286dp"
android:layout_marginBottom="206dp"
android:onClick="inputNumber"
android:text="#" />
<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="173dp"
android:layout_marginBottom="206dp"
android:onClick="inputNumber"
android:text="0" />
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="64dp"
android:layout_marginBottom="208dp"
android:onClick="inputNumber"
android:text="*" />
<EditText
android:id="@+id/phoneNumberEditText"
android:layout_width="305dp"
android:layout_height="64dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="55dp"
android:layout_marginBottom="526dp"
android:ems="10"
android:hint="Enter Phone NUmber"
android:inputType="textPersonName" />
<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Department of CSE, GEC Haveri Page 61
Mobile Application Development(18CSMP68) 2020-21
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="282dp"
android:layout_marginBottom="132dp"
android:text="save" />
<Button
android:id="@+id/callBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="174dp"
android:layout_marginBottom="132dp"
android:text="call" />
<Button
android:id="@+id/clearBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="70dp"
android:layout_marginBottom="134dp"
android:text="clear" />
</RelativeLayout>
Department of CSE, GEC Haveri Page 62
Mobile Application Development(18CSMP68) 2020-21
JAVA CODE:
package com.example.callapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
phoneNumberEditText=findViewById(R.id.phoneNumberEditText);
clearBtn=findViewById(R.id.clearBtn);
callBtn=findViewById(R.id.callBtn);
saveBtn=findViewById(R.id.saveBtn);
clearBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
phoneNumberEditText.setText(" ");
}
});
callBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phoneNumber= phoneNumberEditText.getText().toString();
Intent intent= new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+phoneNumber));
startActivity(intent);
}
});
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phoneNumber=phoneNumberEditText.getText().toString();
Intent intent= new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE,phoneNumber);
startActivity(intent);
}
});
}
Department of CSE, GEC Haveri Page 64
Mobile Application Development(18CSMP68) 2020-21
OUTPUT: