You are on page 1of 34

MOBILE APPLICATION DEVELOPMENT

18CSMP68

Dept. of CSE, VVCE 1


What is Android?

Android is a mobile operating system currently developed by Google, based on the Linux kernel and
designed primarily for touchscreen mobile devices such as smartphones and tablets. And as we said
before, Android offers a unified approach to application development for mobile devices. Android is
an open-source operating system named Android. Google has made the code for all the low-level
"stuff" as well as the needed middleware to power and use an electronic device and gave Android
freely to anyone who wants to write code and build the operating system from it. There is even a full
application framework included, so third-party apps can be built and installed, then made available
for the user to run as they like. The "proper" name for this is the Android Open-Source Project, and
this is what people mean when they say things like Android is open and free. Android, in this iteration,
is free for anyone to use as they like.

Components of an Android Application

The Android applications will be built from four basic component types that are defined by the
Android architecture:

Note: No need of writing the text highlighted in record

Activities

Activities are pieces of executable code that come and go in time, instantiated by either the user or
the operating system and running as long as they are needed. They can interact with the user and
request data or services from other activities or services via queries or Intents. Activities usually
correspond to display screens: each Activity shows one screen to the user.

Services

These are analogous to services or daemons in desktop and server operating systems. They are
executable pieces of code that usually run in the background from the time of their instantiation until
the mobile handset is shut down. They generally don’t expose a user interface.

The classic example of a Service is an MP3 player that needs to keep playing queued files, even while
the user has gone on to use other applications. Your application may need to implement Services to
perform background tasks that persist without a user interface.

Broadcast and Intent Receivers

These respond to requests for service from another application. A Broadcast Receiver responds to a
system-wide announcement of an event. These announcements can come from Android itself (e.g.,
battery low) or from any program running on the system.

An Activity or Service provides other applications with access to its functionality by executing an Intent
Receiver, a small piece of executable code that responds to requests for data or services from other
activities. The requesting (client) activity issues an Intent, leaving it up to the Android framework to
figure out which application should receive and act on it.

Intents are one of the key architectural elements in Android that facilitate the creation of new
applications from existing applications (mobile mashups).

Dept. of CSE, VVCE 2


Content providers

These are created to share data with other activities or services. A content provider uses a standard
interface in the form of a URI to fulfill requests for data from other applications that may not even
know which content provider they are using. For example, when an application issues a query for
Contact data, it addresses the query to a URI of the form: content://contacts/people.

Android Activity Lifecycle

onCreate Called when your activity is first created.

This is the place you normally create your views, open any persistent datafiles your activity needs to
use, and in general initialize your activity. When calling onCreate, the Android framework is passed a
Bundle object that contains any activity state saved from when the activity ran before.

onStart Called just before your activity becomes visible on the screen.

Once onStart completes, if your activity can become the foreground activity on the screen, control will
transfer to onResume. If the activity cannot become the foreground activity for some reason, control
transfers to the onStop method.

onResume Called right after onStart if your activity is the foreground activity on the screen.

At this point your activity is running and interacting with the user. You are receiving keyboard and
touch inputs, and the screen is displaying your user interface. onResume is also called if your activity
loses the foreground to another activity, and that activity eventually exits, popping your activity back
to the foreground. This is where your activity would start (or resume) doing things that are needed to
update the user interface (receiving location updates or running an animation, for example).

Dept. of CSE, VVCE 3


onPause Called when Android is just about to resume a different activity, giving that activity the
foreground.

At this point your activity will no longer have access to the screen, so you should stop doing things
that consume battery and CPU cycles unnecessarily. If you are running an animation, no one is going
to be able to see it, so you might as well suspend it until you get the screen back. Your activity needs
to take advantage of this method to store any state that you will need in case your activity gains the
foreground again—and it is not guaranteed that your activity will resume. If the mobile device you are
running on runs out of memory, there is no virtual memory on disk to use for expansion, so your
activity may have to make way for a system process that needs memory. Once you exit this method,
Android may kill your activity at any time without returning control to you. onStop Called when your
activity is no longer visible, either because another activity has taken the foreground or because your
activity is being destroyed.

onDestroy the last chance for your activity to do any processing before it is destroyed.

Normally you’d get to this point because the activity is done and the framework called its finish
method. But as mentioned earlier, the method might be called because Android has decided it needs
the resources your activity is consuming.

1. Create an application to design a Visiting Card. The Visiting card should have a company logo at
the top right corner. The company name should be displayed in Capital letters, aligned to the center.
Information like the name of the employee, job title, phone number, address, email, fax and the
website address is to be displayed. Insert a horizontal line between the job title and the phone
number.

UI Design:

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

Dept. of CSE, VVCE 4


<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="192dp"
android:text="Dept. of CSE"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.423"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="136dp"
android:text="Assitant Professor"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:text="VVCE"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.151" />

<ImageView
android:id="@+id/imageView"
android:layout_width="210dp"
android:layout_height="123dp"
android:layout_marginTop="64dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.92"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/vvcelogo" />

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="215dp" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"

Dept. of CSE, VVCE 5


android:text="Rakesh K R"
android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java: No code needed here.

2. Develop an Android application using controls like Button, TextView, EditText for designing a
calculator having basic functionality like Addition, Subtraction, Multiplication, and Division.

UI design:

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

<EditText
android:id="@+id/input1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Dept. of CSE, VVCE 6


android:layout_marginTop="132dp"
android:ems="10"
android:hint="Enter a Number"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/input2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Enter a number"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input1" />

<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input2" />

<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:layout_marginLeft="72dp"
android:layout_marginTop="52dp"
android:onClick="addition"
android:text="Add"
app:layout_constraintEnd_toStartOf="@+id/sub"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/result" />

<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:layout_marginEnd="52dp"
android:layout_marginRight="52dp"
android:onClick="subtraction"
android:text="sub"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/result" />

<Button
android:id="@+id/mul"
android:layout_width="wrap_content"

Dept. of CSE, VVCE 7


android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="mul"
app:layout_constraintEnd_toStartOf="@+id/div"
app:layout_constraintHorizontal_bias="0.393"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add" />

<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="div"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.839"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sub" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Calculator"
tools:layout_editor_absoluteX="150dp"
tools:layout_editor_absoluteY="44dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:

package com.example.simcalci;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText input1,input2;


private TextView result;
private Button add,sub,mul,div;

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

public void addition(View view){


input1 = findViewById(R.id.input1);
input2 = findViewById(R.id.input2);
result = findViewById(R.id.result);

float sum =
(Float.parseFloat(input1.getText().toString()))+(Float.parseFloat(input2.ge
tText().toString()));

Dept. of CSE, VVCE 8


result.setText("Sum is "+sum);

}
public void subtraction(View view){
input1 = findViewById(R.id.input1);
input2 = findViewById(R.id.input2);
result = findViewById(R.id.result);

float sub = (Float.parseFloat(input1.getText().toString()))-


(Float.parseFloat(input2.getText().toString()));
result.setText("Difference is "+sub);

}
public void multiplication(View view){
input1 = findViewById(R.id.input1);
input2 = findViewById(R.id.input2);
result = findViewById(R.id.result);

float prod =
(Float.parseFloat(input1.getText().toString()))*(Float.parseFloat(input2.ge
tText().toString()));
result.setText("Difference is "+prod);

}
public void division(View view){
input1 = findViewById(R.id.input1);
input2 = findViewById(R.id.input2);
result = findViewById(R.id.result);
float secondNumber = Float.parseFloat(input2.getText().toString());
if (secondNumber!=0)
{
float div =
(Float.parseFloat(input1.getText().toString()))/(Float.parseFloat(input2.ge
tText().toString()));
result.setText("Division is "+div);
}
Toast.makeText(this, "The denominator cannot be zero",
Toast.LENGTH_SHORT).show();
}
}

Dept. of CSE, VVCE 9


App View:

3. Create a SIGN Up activity with Username and Password. Validation of password should happen
based on the following rules:

• Password should contain uppercase and lowercase letters.


• Password should contain letters and numbers.
• Password should contain special characters.
• Minimum length of the password (the default value is 8).

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 whichdisplays a message saying “Successful Login” or else display a
toast message saying “Login Failed”.The user is given only two attempts and after thatdisplay a toast
message saying “Failed Login Attempts” and disable the SIGN IN button. Use Bundle to transfer
information from one activity to another.

Note: In this app we need to create new activity for Screen 2 and Screen 3

Process of creating the activity:

For screen 2: Right click on Java folder-> new-> activity->empty activity-> name it as “LoginScreen”

For screen 3: Right click on Java folder-> new-> activity->empty activity-> name it as “LoginSuccess”

Dept. of CSE, VVCE 10


Screen 1 Design:

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:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup Activity"
app:layout_constraintBottom_toBottomOf="parent"
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.092" />

<EditText
android:id="@+id/email1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Enter Email"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<EditText

Dept. of CSE, VVCE 11


android:id="@+id/password1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email1" />

<Button
android:id="@+id/signupbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="Sign up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password1" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.app3;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

EditText email1,password1;
Button signupbtn;

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

email1 = findViewById(R.id.email1);
password1 = findViewById(R.id.password1);
signupbtn = findViewById(R.id.signupbtn);

signupbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = email1.getText().toString();
String password = password1.getText().toString();

if (!isValidPassword(password)){

Dept. of CSE, VVCE 12


Toast.makeText(MainActivity.this, "Password rules not
met", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new
Intent(MainActivity.this,LoginScreen.class);
intent.putExtra("email",email);
intent.putExtra("password",password);
startActivity(intent);
}
});
}

Pattern lowercase = Pattern.compile("^.*[a-z].*$");


Pattern uppercase = Pattern.compile("^.*[A-Z].*$");
Pattern numbers = Pattern.compile("^.*[0-9].*$");
Pattern specialCharacter = Pattern.compile("^.*[^a-zA-Z0-9].*$");

private Boolean isValidPassword(String password){


if (password.length() < 8){
return false;
}
if (!lowercase.matcher(password).matches()){
return false;
}
if (!uppercase.matcher(password).matches()){
return false;
}
if (!numbers.matcher(password).matches()){
return false;
}
if (!specialCharacter.matcher(password).matches()){
return false;
}
return true;
}
}

Dept. of CSE, VVCE 13


Screen 2 Design:

activity_login_screen.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=".LoginScreen">

<EditText
android:id="@+id/password2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email2" />

<EditText
android:id="@+id/email2"
android:layout_width="203dp"
android:layout_height="38dp"
android:layout_marginTop="52dp"
android:ems="10"
android:hint="Enter Email"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

Dept. of CSE, VVCE 14


<Button
android:id="@+id/signinbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Sign in"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password2" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signin Activity"
app:layout_constraintBottom_toBottomOf="parent"
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.165" />
</androidx.constraintlayout.widget.ConstraintLayout>

LoginScreen.java:

package com.example.app3;

import androidx.appcompat.app.AppCompatActivity;

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

public class LoginScreen extends AppCompatActivity {


EditText email2,password2;
Button signinbtn;
int count=2;

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

email2 = findViewById(R.id.email2);
password2 = findViewById(R.id.password2);
signinbtn = findViewById(R.id.signinbtn);

String registeredEmail = getIntent().getStringExtra("email");


String registeredPassword = getIntent().getStringExtra("password");

signinbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String eEmail = email2.getText().toString();
String ePassword = password2.getText().toString();

Dept. of CSE, VVCE 15


if(registeredEmail.equals(eEmail)&&registeredPassword.equals(ePassword)){
Intent intent = new
Intent(LoginScreen.this,LoginSuccess.class);
startActivity(intent);
}
else{
Toast.makeText(LoginScreen.this, "Login Failed",
Toast.LENGTH_SHORT).show();

}
count--;
if (count==0){
Toast.makeText(LoginScreen.this, "Failed Login
Attempts", Toast.LENGTH_SHORT).show();
signinbtn.setEnabled(false);
}
}
});
}
}

Screen 3 Design:

activity_login_success.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=".LoginSuccess">

<TextView
android:id="@+id/textView2"
android:layout_width="315dp"
android:layout_height="53dp"
android:text="Welcome You've logged in successfully"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"

Dept. of CSE, VVCE 16


app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.452" />
</androidx.constraintlayout.widget.ConstraintLayout>

LoginSuccess.java:

-- No code here

4. Develop an application to set an image as wallpaper. On click of a button, the wallpaper image
should start to change randomly every 30 seconds.

UI DESIGN:

Note: Images must be saved in drawable folder of the app

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:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wallpaper Changer"
app:layout_constraintBottom_toBottomOf="parent"
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.063" />

<Button
android:id="@+id/pickbtn"
android:layout_width="wrap_content"

Dept. of CSE, VVCE 17


android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Spin Pic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:

package com.example.wallpaper;

import androidx.appcompat.app.AppCompatActivity;

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {


Button pickbtn;
Timer myTimer;
WallpaperManager wpm;
int prev = 1;
Drawable drawable;

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

myTimer = new Timer();


wpm = WallpaperManager.getInstance(this);
pickbtn = findViewById(R.id.pickbtn);

pickbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setWallpaper();
}
});

}
private void setWallpaper(){
myTimer.schedule(new TimerTask() {
@Override
public void run() {
if (prev==1){
drawable = getResources().getDrawable(R.drawable.one);

Dept. of CSE, VVCE 18


prev = 2;
}
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 = 4;
}
else if (prev == 5){
drawable = getResources().getDrawable(R.drawable.five);
prev = 5;
}
Bitmap wallpaper = ((BitmapDrawable)drawable).getBitmap();
try {
wpm.setBitmap(wallpaper);
}
catch (IOException e){
e.printStackTrace();
}
}
},0,30000);
}
}

5. Write a program to create an activity with two buttons START and STOP. On pressing of the
START button, the activity must start the counter by displaying the numbers from One and the
counter must keep on counting until the STOP button is pressed. Display the counter value in a
TextViewcontrol.

UI DESIGN:

Dept. of CSE, VVCE 19


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:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Counter Applcation"
app:layout_constraintBottom_toBottomOf="parent"
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.085" />

<TextView
android:id="@+id/counterView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:text="Start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/counterView" />

<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Stop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/startBtn"
app:layout_constraintVertical_bias="0.03" />

</androidx.constraintlayout.widget.ConstraintLayout>

Dept. of CSE, VVCE 20


MainActivity.java:

package com.example.b3a5;

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;

public class MainActivity extends AppCompatActivity {


TextView counterView;
Button startBtn,stopBtn;
Handler customHandler = new Handler();
int i = 1;

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

counterView = findViewById(R.id.counterView);
startBtn = findViewById(R.id.startBtn);
stopBtn = findViewById(R.id.stopBtn);

startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customHandler.postDelayed(updateTimer,0);
}
});
stopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customHandler.removeCallbacks(updateTimer);
}
});
}
private final Runnable updateTimer=new Runnable() {
@Override
public void run() {
counterView.setText(""+i);
customHandler.postDelayed(this,1000);
i=i+1;
}
};
}

Dept. of CSE, VVCE 21


App view:

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.

UI DESIGN:

Note: city.xml & city.json file needs to be created in the “assets” folder

Dept. of CSE, VVCE 22


City.json:

[
{
"name":"Belgaum",
"lat":"34.345",
"long":"324.45",
"temp":"33",
"hum":"83%"
},
{
"name":"Raichur",
"lat":"44.345",
"long":"784.45",
"temp":"33",
"hum":"83%"
}
]

City.xml:

<?xml version="1.0" encoding="UTF-8"?>


<records>
<place>
<name>Mysore</name>
<lat>65.709</lat>
<long>655.87</long>
<temp>30.1</temp>
<hum>80%</hum>
</place>
<place>
<name>Mumbai</name>
<lat>98.709</lat>
<long>812.87</long>
<temp>31.1</temp>
<hum>82%</hum>
</place>
</records>

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:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parse XML and JSON"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"

Dept. of CSE, VVCE 23


app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.078" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:onClick="parsexml"
android:text="Parse XML"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:onClick="parsejson"
android:text="Parse JSON"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

<TextView
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:

package com.example.b3a6;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
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.InputStream;
import java.nio.charset.StandardCharsets;

import javax.xml.parsers.DocumentBuilder;

Dept. of CSE, VVCE 24


import javax.xml.parsers.DocumentBuilderFactory;

public class MainActivity extends AppCompatActivity {

TextView display;

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

public void parsexml(View V){


try{
InputStream is = getAssets().open("city.xml");
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
Document document = documentBuilder.parse(is);
StringBuilder sB = new StringBuilder();
sB.append("XML DATA");
sB.append("\n-------------");
NodeList nodeList = document.getElementsByTagName("place");
for (int i=0; i<nodeList.getLength();i++){
Node node = nodeList.item(i);
if(node.getNodeType() == node.ELEMENT_NODE){
Element element = (Element) node;
sB.append("\nName: ").append(getValue("name",element));
sB.append("\nLatitude:
").append(getValue("lat",element));
sB.append("\nLongitude:
").append(getValue("long",element));
sB.append("\nTemperature:
").append(getValue("temperature",element));
sB.append("\nHumidity:
").append(getValue("humidity",element));
}
}
display.setText(sB.toString());
}
catch (Exception e){
e.printStackTrace();
Toast.makeText(this, "Error Parsing XML",
Toast.LENGTH_SHORT).show();
}
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void parsejson(View V){
String json;
StringBuilder sB = 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);
sB.append("JSON DATA");
sB.append("\n-------------");

Dept. of CSE, VVCE 25


for (int i=0;i< jsonArray.length();i++){
JSONObject jo = jsonArray.getJSONObject(i);
sB.append("\nName").append(jo.getString("name"));
sB.append("\nLatitude").append(jo.getString("lat"));
sB.append("\nLongitude").append(jo.getString("long"));

sB.append("\nTemperature").append(jo.getString("temperature"));
sB.append("\nHumidity").append(jo.getString("humidity"));
sB.append("\n---------------------");
}
display.setText(sB.toString());
is.close();
}
catch (Exception e){
e.printStackTrace();
Toast.makeText(this, "Error in finding file",
Toast.LENGTH_SHORT).show();
}
}
private String getValue(String tag, Element element){
return
element.getElementsByTagName(tag).item(0).getChildNodes().item(0).getNodeVa
lue();
}
}

App view:

Dept. of CSE, VVCE 26


7. Develop a simple application with one Edit Text so that the user can write some text in it. Create
a button called “Convert Text to Speech” that converts the user input text into voice.

UI DESIGN:

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

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="408dp"
android:onClick="convert"
android:text="To speech"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input" />

<EditText
android:id="@+id/input"
android:layout_width="311dp"
android:layout_height="42dp"
android:layout_marginTop="96dp"
android:ems="10"
android:gravity="start|top"
android:hint="Enter text"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"

Dept. of CSE, VVCE 27


app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text to Speech App"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.087" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:

package com.example.b1a7;

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;

public class MainActivity extends AppCompatActivity {


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

input = findViewById(R.id.input);
ts = new TextToSpeech(getApplicationContext(), new
TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status!=TextToSpeech.ERROR){
ts.setLanguage(Locale.UK);
}
}
});
}
public void convert(View view){
String text = input.getText().toString();
ts.speak(text,TextToSpeech.QUEUE_FLUSH,null);
}
}

Dept. of CSE, VVCE 28


8. Create an activity like a phone dialer with CALL and SAVE buttons. On pressing the CALL button, it
must call the phone number and on pressing the SAVE button it must save the number to the phone
contacts.

XML file:

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

<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Save"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.708"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button12" />

<Button
android:id="@+id/callBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="52dp"

Dept. of CSE, VVCE 29


android:text="Call"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button9" />

<Button
android:id="@+id/clearBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="Clear"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextPhone"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginLeft="44dp"
android:layout_marginTop="240dp"
android:onClick="inputNumber"
android:text="*"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="240dp"
android:onClick="inputNumber"
android:text="#"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="176dp"
android:onClick="inputNumber"
android:text="9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="176dp"

Dept. of CSE, VVCE 30


android:onClick="inputNumber"
android:text="8"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginLeft="44dp"
android:layout_marginTop="176dp"
android:onClick="inputNumber"
android:text="7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="112dp"
android:onClick="inputNumber"
android:text="6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="112dp"
android:onClick="inputNumber"
android:text="5"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginLeft="44dp"
android:layout_marginTop="112dp"
android:onClick="inputNumber"
android:text="4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"

Dept. of CSE, VVCE 31


android:layout_marginTop="48dp"
android:onClick="inputNumber"
android:text="3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="48dp"
android:onClick="inputNumber"
android:text="2"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginLeft="44dp"
android:layout_marginTop="48dp"
android:onClick="inputNumber"
android:text="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="240dp"
android:onClick="inputNumber"
android:text="0"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />

<EditText
android:id="@+id/editTextPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:ems="10"
android:inputType="phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.273"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Call Appplication"

Dept. of CSE, VVCE 32


app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.068" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code: (MainActivity.java)

package com.example.b3a8;

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;

public class MainActivity extends AppCompatActivity {


EditText editTextPhone;
Button clearBtn,callBtn,saveBtn;

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

editTextPhone = findViewById(R.id.editTextPhone);
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 view) {
editTextPhone.setText("");
}
});
callBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNumber = editTextPhone.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 view) {
String phoneNumber = editTextPhone.getText().toString();
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);

intent.putExtra(ContactsContract.Intents.Insert.PHONE,phoneNumber);
startActivity(intent);

Dept. of CSE, VVCE 33


}
});
}
public void inputNumber(View V){
Button btn = (Button)V;
String digit = btn.getText().toString();
String phoneNumber = editTextPhone.getText().toString();
editTextPhone.setText(phoneNumber +digit);
}
}

End of Part A

Dept. of CSE, VVCE 34

You might also like