0% found this document useful (0 votes)
9 views39 pages

Mad 55

The document is a laboratory manual for Mobile Application Development at Ahmedabad Institute of Technology, outlining the vision, mission, and educational objectives of the IT department. It includes practical exercises for students, covering various aspects of Android application development, such as creating a 'Hello World' application and understanding the Android activity lifecycle. The manual also emphasizes safety protocols and proper conduct within the laboratory environment.

Uploaded by

shivam9gbs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views39 pages

Mad 55

The document is a laboratory manual for Mobile Application Development at Ahmedabad Institute of Technology, outlining the vision, mission, and educational objectives of the IT department. It includes practical exercises for students, covering various aspects of Android application development, such as creating a 'Hello World' application and understanding the Android activity lifecycle. The manual also emphasizes safety protocols and proper conduct within the laboratory environment.

Uploaded by

shivam9gbs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Ahmedabad Institute of Technology

IT Department
MOBILE APPLICATION & DEVELOPMENT
(3161612)

Laboratory Manual
NAME

ENROLLMENT NUMBER

BATCH

YEAR

SUBJECT COORDINATOR SUBJECT MEMBER


DEPARTMENT OF INFORMATION TECHNOLOGY

VISION
“To impart quality education and research in Information Technology to produce a competent, committed
and goal oriented workforce to fulfill the needs of the local and global IT Industry”

Mission of the Department


The Department‘s Mission is to advance knowledge in the field of Information Technology and Allied Science
by providing our students with the highest quality educational experience towards
1. M1: Producing quality workforce with cutting edge technology.
2. M2: Engaging in innovative teaching, learning, research and community service.
3. M3: Inculcating sense of ethical attitude, social responsibility and developing leadership quality
required for a responsible IT professional.

Program Educational Objectives (PEO):


The Program Educational Objectives (PEO) of B.Tech Information Technology program at the
Ahmedabad Institute of Technology are to produce graduates who are able to:
PEO 1. Apply the fundamentals of Information Technology and allied science, mathematics and scientific
investigation to solve real world problems appropriate to the discipline.
PEO 2. Apply current industry accepted computing practices and emerging technologies to analyze,design,
implement, and verify high quality IT-based solutions to real world problems.
PEO 3. Exhibit teamwork and effective communication skills for a successful professional career in their
chosen field.
PEO 4. Understand the ethical obligations, social impacts and apply their technical knowledge positively and
appropriately in the course of career and professional journey.
PROGRAM OUTCOMES (POs)
Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and
an engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and
engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design system
components or processes that meet the specified needs with appropriate consideration for the public
health and safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern engineering
and IT tools including prediction and modeling to complex engineering activities with an understanding of
the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
MOBILE APPLICATION DEVELOPMENT PRACTICAL BOOK
DEPARTMENT OF INFORMATION TECHNOLOGY
PREFACE

It gives us immense pleasure to present the first edition of Mobile Application Development Practical Book for
the B.E. 3rd year students of Ahmedabad Institute of Technology.
The lab exercises in this manual are designed to be both educational and practical. Each lab session provides
clear instructions, code snippets, and real-world examples to help you understand and apply the concepts.
Additionally, each exercise is accompanied by challenges to test your knowledge and encourage problem-
solving skills.To successfully complete the labs, you will need access to certain tools and resources,
including integrated development environments (IDEs) like Xcode and Android Studio, as well as emulators
and physical devices for testing. Detailed instructions on setting up these tools are provided in the initial
chapters..
The student is required to keep a laboratory manual in which the raw data will be recorded as well as the
questions will be kept. The lab write-ups form a permanent record of your work.

Lab Manual Revised by:


MOBILE APPLICATION DEVELOPMENT PRACTICAL BOOK
DEPARTMENT OF INFORMATION TECHNOLOGY

Do’s:

1. Enter the laboratory in student uniform and carry Student ID.


2. Leave footwear outside and keep bags or any other belongings in designated storage places. Do not keep any
valuable things in bags.
3. Carry only the laboratory notebook into the laboratory and keep other belongings at the place designated for
keeping belongings.
4. Know the location of the fire extinguisher and the first aid box and how to use them in case of an emergency.
5. Report fires or accidents to your lecturer/laboratory technician immediately
6. Report any broken plugs or exposed electrical wires to your lecturer/laboratory technician immediately.
7. Read and understand how to carry out an activity thoroughly before coming to the laboratory.
8. For system problems, call laboratory staff for resolving, and do not try to repair the system on your own.
9. Access to the internet is only on designated systems and against permission.
10. Ensure computer systems are properly shutdown and other equipment are turned-off after use.
11. Follow the rules of the laboratory strictly. Violation of rules may attract punishment.

Don’ts:

1. Do not eat or drink in the laboratory.


2. Do not carry and use any pen drive, electronic gadget or cell phone in the laboratory.
3. Do not install, update or download any software on your own in any computer inside the laboratory.
4. Do not use any unauthorized software or tools.
5. Do not alter or delete configuration settings of any computer laboratory system.
6. Tampering, deleting or modifying CMOS/BIOS settings, IP Configuration, system parameters, or system
files stored in the hard disk are strictly prohibited in the laboratory.
7. No student or personnel is allowed to attach or detach any peripheral to and from any IT equipment
or devices without explicit permission from the Lab In charge/ Lab Technician.
8. Do not write/scratch on monitors, walls, tables or chairs in the laboratory.
9. Avoid stepping on electrical wires or any other computer cables.
10. Do not open the system unit casing or monitor casing particularly when the power is turned on.
Some internal components hold electric voltages of up to 30000 volts, which can be fatal.
11. Do not insert metal objects such as clips, pins and needles into the computer casings.They may cause fire.
Ahmedabad Institute of Technology
IT Department

CERTIFICATE
This is to certify that Mr. / Ms.
Of Enrolment No has Satisfactorily completed
the course in as by the Gujarat
Technological University for Year (B.E.) semester of Computer
Engineering in the Academic year .

Date of Submission:-

Faculty Name :
Signature: Head Of Department (IT)
INDEX
Sr Experiment Title Page DATE SIGN
No No.

1 Create a “Hello World” application. That will display “Hello World”


in the middle of the screen using TextView Widget in the red color.

2 Create an application for demonstration of android activity life


cycle.

3 Create an application that will get the Text Entered in Edit Text
and display that Text using toast (Message).

4 Create an application in which when a user clicks a button, the user


will navigate to the next screen which displays a welcome message
(using Explicit Intent).

5 Create an application that will pass two numbers using EditText to


the next screen , and on the next screen display the sum of that
number.

6 Create an application with three different Radio Button and by clicking


them change the background of activity.

7 Create an android application to perform addition, subtraction,


multiplication, division operations of two numbers and also find
maximum from them. Display toast message.
8 Create sample application with login module.(Check username and
password) On successful login, display welcome message in next
activity. And on failing login, alert the user.

9 Create an application that uses the image button and clicking this
button change the image of it and also handle onLongClick event.

10 Case study :Advance android projects idea and implements some


application pages using android studio.
23002311605
5
PRACTICAL – 1
AIM: Create “Hello World” application. That will display “Hello World” in the middle of
the screen using TextView Widget in the red color.

Theory:
Prerequisites for this android project

Java: First of all you need to have the knowledge of Java Programming. Java programming plays a very
important role as we will develop the app code in Java

XML: XML is another important part of our android application. It will be used for the development of the user
interface for the application

Android Studio: Android Studio is the backbone of our application, as we will develop our app using android
studio. Android virtual device is also shipped with android studio that will be helpful in testing whether the
applications are working or not

Code:
● activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#FF0000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

● MainActivity.java
package com.example.prac1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
23002311605
5
}
}

Output

Post Practical Questions:

1. Which of the following virtual machines is used by the Android operating


system? a. JVM
b. Dalvik Virtual Machine
c. Simple Virtual Machine
d. None of the above

2. Android is based on which of the following languages?


a. Java
b. C++
c. C

3. Android was developed by.


a. Microsoft
b. Google
c. IBM
d. Apple
23002311605
5

PRACTICAL – 2
AIM: Create an android application that demonstrates the android activity life cycle.

Theory:

Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity
is the subclass of ContextThemeWrapper class.An activity is the single screen in android. It is like a
window or frame of Java. By the help of activity, you can place all your UI components or widgets in a
single screen. The 7 lifecycle method of Activity describes how activity will behave at different states.

Android Activity Lifecycle methods: Method Description

onCreate() called when activity is first created.

onStart() called when activity is becoming visible to the user.

onResume() called when activity will start interacting with the user.

onPause() called when activity is not visible to the user.

onStop() called when activity is no longer visible to the user.

onRestart() called after your activity is stopped, prior to start.

onDestroy() called before the activity is destroyed.


23002311605
5

Code:

● activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
23002311605
5

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

</RelativeLayout>

● MainActivity.java

package com.example.prac2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Lifecycle","OnCreateInvoke");
}
@Override
protected void onStart(){
super.onStart();
Log.d("Lifecycle","OnStartInvoke");
}
@Override
protected void onResume(){
super.onResume();
Log.d("Lifecycle","OnResumeInvoke");
}
@Override
protected void onPause(){
super.onPause();
Log.d("Lifecycle","OnPauseInvoke");
}
@Override
protected void onStop(){
super.onStop();
Log.d("Lifecycle","OnStopInvoke");
}
@Override
protected void onRestart(){
23002311605
5
super.onRestart();
Log.d("Lifecycle","OnRestartInvoke");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d("Lifecycle","OnDestroyInvoke");
}
}

Output
23002311605
5

Post Practical Questions:


1. What is an Activity in Android?
a) Android class
b) Android package
c) A single screen in application with supporting java
code d) None of the above
2. How can we kill an activity in android?
a) Finish()
b) finishActivity(int requestCode)
c) both a and b
d) neither a nor b
3. Which of the following is not an activity lifecycle callback
method? a) onClick() method
b) onCreate() method
c) onStart() method
d) onBackPressed() method
4. Does android support other languages than java?
a) Yes
b) No
c) May be
d) Can't say
5. Which of the following is contained in the src folder?
a) XML
b) Java source code
c) Manifest
d) None of the above
23002311605
5

Practical:-3
AIM:-Create an application that will get the Text Entered in Edit Text and display
that Text using toast (Message).

Theory:
What is Toast Massage?
A toast provides simple feedback about an operation in a small popup. It only fills the amount of
space required for the message and the current activity remains visible and interactive. Toasts
automatically disappear after a timeout.

For example, clicking Send on an email triggers a "Sending message..." toast, as shown in the
following screen capture:

If your app targets Android 12 (API level 31) or higher, its toast is limited to two lines of text and
shows the application icon next to the text. Be aware that the line length of this text varies by
screen size, so it's good to make the text as short as possible.

Alternatives to using toasts


● If your app is in the foreground, consider using a snackbar instead of using a toast.
Snackbars include user-actionable options, which can provide a better app experience.
● If your app is in the background, and you want users to take some action, use a notification
instead.

Instantiate a Toast object


Use the makeText() method, which takes the following parameters:

● The application Context.


● The text that should appear to the user.
● The duration that the toast should remain on the screen.
23002311605
5

The makeText() method returns a properly initialized Toast object.

Context context = getApplicationContext();


CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);


toast.show();

Code:

● activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/edit_text"
android:hint="Name!!!"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edit_text"
android:layout_centerInParent="true"

/>

</RelativeLayout>

● MainActivity.java
package com.example.prac2;
import android.appcompat.app.AppCompatActivity;
import android.view.View;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
23002311605
5
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click_me (View view) {
EditText e = findViewById(R.id.edit_text);

Toast.makeText(getApplicationContext(),e.getText().toString(),Toast.LENGTH_SHORT).show();
}
}

Output
23002311605
5

PRACTICAL – 4

AIM:-Create an application in which when a user clicks a button, the user will
navigate to the next screen which displays a welcome message (using Explicit
Intent).

Theory :

Android Intent is the message that is passed between components such as activities, content
providers, broadcast receivers, services etc.It is generally used with startActivity() method to
invoke activity, broadcast receivers etc.

The dictionary meaning of intent is intention or purpose. So, it can be described as the intention
to do action.
Android intents are mainly used to:
● Start the service
● Launch an activity
● Display a web page
● Display a list of contacts
● Broadcast a message
● Dial a phone call etc.
Types of Android Intents :

There are two types of intents in android: Implicit and Explicit

1) Implicit Intent:

Implicit Intent doesn't specify the component. In such case, intent provides information of
available components provided by the system that is to be invoked

For example, you may write the following code to view the webpage:
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("web page url"));
startActivity(intent);

2) Explicit Intent:

Explicit Intent specifies the component. In such a case, intent provides the external class to be
invoked.

Android Explicit intent specifies the component to be invoked from activity. In other words, we
can call another activity in android by explicit intent.

We can also pass the information from one activity to another using explicit intent.Here, we are
going to see an example to call one activity from another and vice-versa.

Android calling one activity from another activity example:


Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
23002311605
5

Code:

● activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/butt_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Welcome to next page"
android:textColor="@color/white"
android:onClick="OnClick" />

</RelativeLayout>

● MainActivity.java
package com.example.prac3;
import android.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity


{ Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = findViewById(R.id.butt_1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),MainActivity2.class);
startActivity(i);
}
});
}
}

● activity_main2.xml
23002311605
5
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome!!!!"/>
</RelativeLayout>

● MainActivity2.java
package com.example.prac3;
import android.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}

Output
23002311605
5
Post Practical Questions:
1. How can we stop the services in android?
A. Stopself() and stopservice()
B. Finish()
C. System.exit()
D. None of the above
2. What is the use of content provider in android?
A. Storing data in database
B. Sharing data between application
C. Sending data from one app to another app
D. None
3. Which of the following method is used to handle what happens after clicking
a button?
A. onClick
B. onCreate
C. onSelect
D. None
4. An Android is an abstract description of an operation to be performed. It can be used
with startActivity to launch an Activity.
A. Filters
B. Intent
C. Service
D. Broadcast receiver
5. Which of the following action will display the phone dialer with the given number
filled in?
A. ACTION_VIEW tel:123
B. ACTION_SET
C. ACTION_DIAL tel:123
D. None
23002311605
5

Practical:- 5
AIM:-Create an application that will pass two numbers using EditText to the next
screen , and on the next screen display the sum of that number.

Theory:-
Text view to display any text. In this example we are going to create an app to add two numbers.
Create an android project and use the following Text Fields control to take input from the user.
I have taken two textview to display messages and one textview to display output. Two plan text fields i.e
EditText to take input and one button.

In XML:
STEP-1: Declare a few variables and the values entered in the Text Views can be read by
using an id which we have set in the XML code above.
STEP-2: Add the click listener to the Add button.
STEP-3: When the Add button has been clicked, add the values and store it into the sum
variable.
STEP-4: To show the output in the result text view, set the sum in the textview.

Example:
public void sum(View v)
{

//get the edit text


EditText
t1=(EditText)findViewById(R.id.edi
tText);
EditText
t2=(EditText)findViewById(R.id.edi
tText2);

//convert value into int


int
x=Integer.parseInt(t1.getText().to
String());
int
y=Integer.parseInt(t2.getText().to
String());

//sum these two numbers


int z=x+y;

//display this text to


TextView
TextView
tv_data=(TextView)findViewById(R.i
d.tv_result);
tv_data.setText("The sum is
"+z);

Code:
● activity_main.xml
23002311605
5
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:hint="Enter no."
/>
<EditText
android:id="@+id/edt2"
android:layout_below="@id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter no."
android:layout_centerVertical="true"
/>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edt2"
android:text="Submit"/>
</RelativeLayout>

● MainActivity.java
package com.example.prac5;
import android.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;

public class MainActivity extends AppCompatActivity


{ Button b;
EditText ed1,ed2;
Double n1,n2,sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
23002311605
5
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= findViewById(R.id.btn1);
ed1= findViewById(R.id.edt1);
ed2= findViewById(R.id.edt2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
n1=Double.parseDouble(ed1.getText().toString());
n2=Double.parseDouble(ed2.getText().toString());
sum=n1+n2;
String m=Double.toString(sum);
Intent i = new Intent(getApplicationContext(),Activity2.class);
i.putExtra("Addition",m);
startActivity(i);
}
});
}
}

● activity_2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity2">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>

</RelativeLayout>

● Activity2.java
package com.example.prac5;
import android.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
23002311605
5
import android.widget.TextView;

public class Activity2 extends AppCompatActivity {

Textview textview;
@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent intent=getIntent();
String message= intent.getStringExtra("Addition");
textView=findViewById(R.id.tv1);
textView.setText(message);
}
}

Output
23002311605
5
Practical :-6
AIM:-Create an application with three different Radio Button and by clicking
them change the background of activity.

Theory:-

Radio buttons allow the user to select one option from a set. You should use radio buttons for
optional sets that are mutually exclusive if you think that the user needs to see all available
options side-by-side. If it's not necessary to show all options side-by-side, use a spinner instead.

To create each radio button option, create a RadioButton in your layout. However, because radio
buttons are mutually exclusive, you must group them together inside a RadioGroup. By grouping
them together, the system ensures that only one radio button can be selected at a time.

Key classes are the following:


● RadioButton
● RadioGroup

Responding to Click Events


When the user selects one of the radio buttons, the corresponding RadioButton object receives an
on-click event.
To define the click event handler for a button, add the android:onClick attribute to the
<RadioButton> element in your XML layout. The value for this attribute must be the name of the
method you want to call in response to a click event. The Activity hosting the layout must then
implement the corresponding method.

The method you declare in the android:onClick attribute must have a signature exactly as shown
above. Specifically, the method must:
● Be public
● Return void
● Define a View as its only parameter (this will be the View that was clicked)

Code:
● activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/layout">
<TextView
android:id="@+id/tv1"
23002311605
5
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHOOSE A COLOR"
android:layout_marginTop="40dp"
android:textSize="20dp"
android:layout_centerHorizontal="true"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/rg"
android:layout_below="@+id/tv1">
<RadioButton
android:id="@+id/rdr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RED"
android:textSize="15dp"
android:onClick="OnClick" />
<RadioButton
android:id="@+id/rdb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BLUE"
android:textSize="15dp"
android:onClick="OnClick"/>
<RadioButton
android:id="@+id/rdg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GREEN"
android:textSize="15dp"
android:onClick="OnClick"/>
</RadioGroup>
</RelativeLayout>

● MainActivity.java
package com.example.prac6;
import android.annotation.Nullable;
import android.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.graphics.Color;
public class MainActivity extends AppCompatActivity {
23002311605
5
RadioButton r,b,g;
RelativeLayout l1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1= findViewById(R.id.layout);
r= findViewById(R.id.rdr);
b= findViewById(R.id.rdb);
g= findViewById(R.id.rdg);
}
public void onClick (View v)
{
boolean checked = ((RadioButton) v).isChecked();
switch(v.getId()) {
case R.id.rdr:
if (checked)
l1.setBackgroundColor(Color.RED);
break;
case R.id.rdb:
if (checked)
l1.setBackgroundColor(Color.BLUE);
break;
case R.id.rdg:
if (checked)
l1.setBackgroundColor(Color.GREEN);
break;
}
}
}

Output
23002311605
5
Practical -7

AIM:- Create an android application to perform addition, subtraction,


multiplication, division operations of two numbers and also find maximum from
them. Display toast message.
Theory:-
Step 1: Project Setup
1. Launch Android Studio and create a new Android project with a suitable name and
package.
2. Choose an appropriate form factor and minimum API level for your project.
Step 2: XML Layout
1. Open the activity_main.xml layout file.
2. Define the UI elements: two EditText views for input numbers, Buttons for each
operation (add, subtract, multiply, divide, and square root), and a TextView to display
the result.
Step 3: Java Code (MainActivity.java)
1. Create a new Java class named MainActivity that extends AppCompatActivity.
2. Declare private instance variables for the EditText views, TextView, and Buttons.
3. Inside the onCreate method, use findViewById to initialize the UI elements by their
respective IDs.
4. Set OnClickListener for each operation Button to perform calculations when clicked.
5. Define a performCalculation method that takes an operator as an argument and
performs the calculation based on the operator (+, -, *, /).
6. Inside the performCalculation method, parse the input numbers from the EditText
views, perform the calculation, and display the result in the TextView.
7. Create a calculateSquareRoot method to calculate the square root of a number.
8. In both calculation methods, format the result using DecimalFormat to show a
maximum of two decimal places.

Code:

● activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number"
android:layout_marginTop="5dp"/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
23002311605
5
android:layout_height="wrap_content"
android:hint="Enter number"
android:layout_below="@id/et1"
android:layout_marginTop="5dp"/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et2"
android:text="Addition"
android:onClick="OnClickAdd"
android:layout_marginTop="5dp"
android:layout_marginHorizontal="115dp"/>
<Button
android:id="@+id/sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/add"
android:text="Subtraction"
android:onClick="OnClickSub"
android:layout_marginTop="5dp"
android:layout_marginHorizontal="115dp"/>
<Button
android:id="@+id/mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sub"
android:text="Multiplication"
android:onClick="OnClickMul"
android:layout_marginTop="5dp"
android:layout_marginHorizontal="115dp"/>
<Button
android:id="@+id/div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/mul"
android:text="Division"
android:onClick="OnClickDiv"
android:layout_marginTop="5dp"
android:layout_marginHorizontal="115dp"/>
<Button
android:id="@+id/max"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/div"
android:text="Maximum No."
android:onClick="OnClickMax"
android:layout_marginTop="5dp"
23002311605
5
android:layout_marginHorizontal="115dp"/>

</RelativeLayout>

● MainActivity.java
package com.example.prac8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{ EditText ed1,ed2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1= findViewById(R.id.et1);
ed2= findViewById(R.id.et2);
}
public void OnClickAdd(View v)
{
Double no1= Double.parseDouble(ed1.getText().toString());
Double no2= Double.parseDouble(ed2.getText().toString());
Double ans= no1+no2;
Toast.makeText(MainActivity.this,"Addition is
"+ans.toString(),Toast.LENGTH_LONG).show();
}
public void OnClickSub(View v)
{
Double no1= Double.parseDouble(ed1.getText().toString());
Double no2= Double.parseDouble(ed2.getText().toString());
Double ans= no1-no2;
Toast.makeText(MainActivity.this,"Subtraction is
"+ans.toString(),Toast.LENGTH_LONG).show();
}
public void OnClickMul(View v)
{
Double no1= Double.parseDouble(ed1.getText().toString());
Double no2= Double.parseDouble(ed2.getText().toString());
Double ans= no1*no2;
Toast.makeText(MainActivity.this,"Multiplication is
"+ans.toString(),Toast.LENGTH_LONG).show();
}
public void OnClickDiv(View v)
{
Double no1= Double.parseDouble(ed1.getText().toString());
Double no2= Double.parseDouble(ed2.getText().toString());
Double ans= no1/no2;
Toast.makeText(MainActivity.this,"Division is
23002311605
5
"+ans.toString(),Toast.LENGTH_LONG).show();
}
public void OnClickMax(View v) {
Double no1 = Double.parseDouble(ed1.getText().toString());
Double no2 = Double.parseDouble(ed2.getText().toString());
if (no1 > no2)
{
Toast.makeText(MainActivity.this, "Max no. is " + no1.toString(),
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "Max no. is " + no2.toString(),
Toast.LENGTH_LONG).show();

}
}
}

Output
23002311605
5

Practical -8
AIM:- Create sample application with login module. (Check username and
password) On successful login, display welcome message in next activity. And on
failing login, alert the user.
Theory:

A login application is the screen asking your credentials to login to some particular application.
You might have seen it when logging into facebook,twitter e.t.c

This chapter explains, how to create a login screen and how to manage security when false attempts
are made.

First you have to define two TextView asking username and password of the user. The password
TextView must have inputType set to password

In the java file, inside the method of onClick get the username and passwords text using getText()
and toString() method and match it with the text using equals() function.

Steps Description

1 You will use Android studio to create an Android application under a package
com.example.sairamkrishna.myapplication.

3 Modify src/MainActivity.java file to add necessary code.

4 Modify the res/layout/activity_main to add respective XML components

5 Run the application and choose a running android device and install the application on it and
verify the results

Code:
● activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
23002311605
5
android:hint="Username"
android:id="@+id/uname"
android:layout_centerInParent="true"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/pass"
android:layout_below="@+id/uname"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lgn"
android:layout_below="@+id/pass"
android:text="LOGIN"
android:onClick="Login"
android:layout_centerInParent="true"/>
</RelativeLayout>

● MainActivity.java
package com.example.login;
import android.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{ EditText username,password;
Button login;

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

public void Login(View view) {


username = findViewById(R.id.uname);
password = findViewById(R.id.pass);
Log.d("Tanvi", username.getText().toString());
23002311605
5
Log.d("Malpani", password.getText().toString());
if (username.getText().toString().trim().equals("Tanvi") &&
password.getText().toString().trim().equals("Malpani") ) {
Intent i = new Intent(this, MainActivity2.class);
startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Login Unsuccessful",
Toast.LENGTH_LONG).show();
login=findViewById(R.id.lgn);
login.setEnabled(false);
}
}
}

● activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login Successful"
android:textAlignment="center"
android:layout_centerInParent="true"
/>
</RelativeLayout>

● MainActivity2.java
package com.example.login;
import android.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
23002311605
5

Output
23002311605
5

Practical -9
AIM:- Create an application that uses the image button and clicking this button
changes the image of it and also handles the onLongClick event.

Theory:-
Events are a useful way to collect data about a user's interaction with interactive components of
Applications. Like button presses or screen touch etc. The Android framework maintains an event
queue as a first-in, first-out (FIFO) basis. You can capture these events in your program and take
appropriate action as per requirements.

There are following three concepts related to Android Event


Management −
Event Listeners − An event listener is an interface in the View
class that contains a single callback method. These methods will
be called by the Android framework when the View to which the
listener has been registered is triggered by user interaction with
the item in the UI.

Event Listeners Registration − Event Registration is the process


by which an Event Handler gets registered with an Event Listener
so that the handler is called when the Event Listener fires the
event.

Event Handlers − When an event happens and we have


registered an event listener for the event, the event listener calls
the Event Handlers, which is the method that actually handles
the event.

Event Listeners & Event Handlers


Event Handler Event Listener & Description

OnClickListener()
This is called when the user either clicks or touches or focuses
onClick() upon any widget like button, text, image etc. You will use
onClick() event handler to handle such event.

OnLongClickListener()
This is called when the user either clicks or touches or focuses
upon any widget like button, text, image etc. for one or more
onLongClick() seconds. You will use onLongClick() event handler to handle
such event.

OnFocusChangeListener()
This is called when the widget looses its focus ie. user goes away
onFocusChange() from the view item. You will use onFocusChange() event handler
to handle such event.
23002311605
5

OnFocusChangeListener()
This is called when the user is focused on the item and presses or
onKey() releases a hardware key on the device. You will use onKey()
event handler to handle such event.

OnTouchListener()
This is called when the user presses the key, releases the key, or
onTouch() any movement gesture on the screen. You will use onTouch()
event handler to handle such event.

OnMenuItemClickListener()
This is called when the user selects a menu item. You will use
onMenuItemClick() onMenuItemClick() event handler to handle such event.

onCreateContextMenuItemListener()
This is called when the context menu is being built(as the result
onCreateContextMenu() of a sustained "long click)

Code:
● activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:id="@+id/img1"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img1"
android:layout_centerHorizontal="true"
android:text="Change Image"
android:id="@+id/button"/>

</RelativeLayout>

● MainActivity.java
package com.example.image;
23002311605
5
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity
{ Button b1;
ImageView iv;
boolean flag;
int images[]={R.drawable.img1,R.drawable.img2,R.drawable.img3};
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.img1);
b1=(Button) findViewById(R.id.button);
flag=true;
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv.setImageResource(images[i]);
i++;
if(i==3)
i=0;
}
});
}}

Output

You might also like