0% found this document useful (0 votes)
5 views118 pages

Mad Manual Prints

The document outlines practical exercises and assessment criteria for a Mobile Application Development course, focusing on Android programming. It includes sample questions, programming tasks, and XML code examples for creating user interfaces. Additionally, it provides references for further reading and a detailed assessment scheme based on performance and product-related indicators.

Uploaded by

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

Mad Manual Prints

The document outlines practical exercises and assessment criteria for a Mobile Application Development course, focusing on Android programming. It includes sample questions, programming tasks, and XML code examples for creating user interfaces. Additionally, it provides references for further reading and a detailed assessment scheme based on performance and product-related indicators.

Uploaded by

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

Mobile Application Development (22617)

IX. Practical related Questions


Note: Below given are few sample questions for reference. Teachers must
design more such questions to ensure the achievement of identified CO.
1. List the files used to write Hello World program.
2. What is an activity in Android programming?

(Space for answers)


Ans 1:
1. The Main Activity File
The main activity code is a Java file MainActivity.java. This is the actual
application file which ultimately gets converted to a Dalvik executable and runs
your application.
2. The Manifest File
Whatever component you develop as a part of your application, you must declare all
its components in a manifest.xml which resides at the root of the application project
directory. This file works as an interface between Android OS and your application,
so if you do not declare your component in this file, then it will not be considered by
the OS.
3. The Strings File
The strings.xml file is located in the res/values folder and it contains all the text that
your application uses. For example, the names of buttons, labels, default text, and
similar types of strings go into this file. This file is responsible for their textual
content.
4. The Layout File
The activity_main.xml is a layout file available in res/layout directory, that is
referenced by your application when building its interface. You will modify this file
very frequently to change the layout of your application.

Ans 2:
An activity is the single screen in android. It is like 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.
X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to display HelloWorld.
2. Write a program to display student name and marks.
(Space for answers)
Ans 1:
<?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"

Maharashtra State Board of Technical Education 19


Mobile Application Development (22617)
android:background="#FFC107"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2196F3"
android:text="Hello World!"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Maharashtra State Board of Technical Education 20


Mobile Application Development (22617)
Ans 2:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f6f6f6"
tools:context=".MainActivity">

<TableRow android:background="#98ded9" android:padding="5px">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Students_Information"
android:textColor="#161d6f"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow android:background="#c7ffd8" android:padding="5px" >

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: Heet ; Marks: 96"
android:textColor="#161d6f"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow android:background="#98ded9" android:padding="5px">

<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: Meet ; Marks: 97"
android:textColor="#161d6f"
Maharashtra State Board of Technical Education 21
Mobile Application Development (22617)
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow android:background="#c7ffd8" android:padding="5px">

<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: Pratik ; Marks: 99"
android:textColor="#161d6f"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow android:background="#98ded9" android:padding="5px">

<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: Shaun ; Marks: 96"
android:textColor="#161d6f"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow android:background="#c7ffd8" android:padding="5px">

<TextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: Naman ; Marks: 97"
android:textColor="#161d6f"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow android:background="#98ded9" android:padding="5px">


Maharashtra State Board of Technical Education 22
Mobile Application Development (22617)

<TextView
android:id="@+id/text6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: Hisbaan ; Marks: 92"
android:textColor="#161d6f"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>

Maharashtra State Board of Technical Education 23


Mobile Application Development (22617)
X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to place Name, Age and mobile number linearly
(Vertical) on the display screen using Linear layout.
2. Write a program to place Name, Age and mobile number centrally on
the display screen using Absolute layout.

(Space for answers)

Ans 1.
<?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">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="199dp"
tools:layout_editor_absoluteY="362dp">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#03A9F4"
android:text="Name: Heet"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFC107"
android:text="Age:19"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold" />
Maharashtra State Board of Technical Education 27
Mobile Application Development (22617)

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#03A9F4"
android:text="Mobile_No:123456789"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Maharashtra State Board of Technical Education 28


Mobile Application Development (22617)
Ans 2:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"

android:layout_height="match_parent"
android:background="#c7ffd8"
tools:context=".MainActivity"
tools:ignore="Deprecated">

<TextView
android:id="@+id/textView"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="12dp"
android:layout_y="338dp"
android:text="Name: Heet"
android:textColor="#161d6f"
android:textSize="18sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="135dp"
android:layout_y="338dp"
android:text="Age: 18"
android:textColor="#161d6f"
android:textSize="18sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="213dp"
android:layout_y="338dp"
android:text="Phone_no: 123456789"
android:textColor="#161d6f"
android:textSize="18sp"
android:textStyle="bold" />

</AbsoluteLayout>

Maharashtra State Board of Technical Education 29


Mobile Application Development (22617)

XI. References / Suggestions for further Reading


1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com

XII. Assessment Scheme

Performance indicators Weightage


Process related (10 Marks) 30%
1. Logic Formation 10%
2. Debugging ability 15%
3. Follow ethical practices 5%
Product related (15 Marks) 70%
4. Interactive GUI 20%
5. Answer to Practical related questions 20%
6. Expected Output 20%
7. Timely Submission 10%
Total (25 Marks) 100%

Maharashtra State Board of Technical Education 30


Mobile Application Development (22617)
1 grid indices that run from 0 through N inclusive. Regardless of how GridLayout is configured,
grid index 0 is fixed to the leading edge of the container and grid index N is fixed to its
trailing edge (after padding is taken into account).

X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to display 10 students basic information in a table
form using Table layout.
2. Write a program to display all the data types in object-oriented
programming using Frame layout.

(Space for answers)

Ans 1. <?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">

<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#31326f"
tools:layout_editor_absoluteX="125dp"
tools:layout_editor_absoluteY="245dp">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:paddingTop="25dp"
android:paddingBottom="25dp"
android:text="Name"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView2"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_column="2"
Maharashtra State Board of Technical Education 35
Mobile Application Development (22617)
android:layout_gravity="center"
android:paddingTop="25dp"
android:paddingBottom="25dp"
android:text="AGE"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView3"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:text="Heet"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView4"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center"
android:text="19"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView5"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:text="Shaun"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView6"
android:layout_width="170dp"
Maharashtra State Board of Technical Education 36
Mobile Application Development (22617)
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center"
android:text="19"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView7"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:text="Pratik"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView8"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center"
android:text="18"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView9"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:text="Gaurang"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView10"
android:layout_width="170dp"
Maharashtra State Board of Technical Education 37
Mobile Application Development (22617)
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center"
android:text="19"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView11"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:text="Meet"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView12"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center"
android:text="19"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView13"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:text="Naman"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView14"
android:layout_width="170dp"
Maharashtra State Board of Technical Education 38
Mobile Application Development (22617)
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center"
android:text="18"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView15"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:text="Divyesh"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView16"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center"
android:text="19"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView17"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:text="Harry"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView18"
android:layout_width="170dp"
Maharashtra State Board of Technical Education 39
Mobile Application Development (22617)
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center"
android:text="19"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView19"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:text="Ron"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView20"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center"
android:text="18"
android:textColor="#ffc93c"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Maharashtra State Board of Technical Education 40


Mobile Application Development (22617)

Ans 2. <?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">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#02475e"
tools:layout_editor_absoluteX="114dp"
Maharashtra State Board of Technical Education 41
Mobile Application Development (22617)
tools:layout_editor_absoluteY="242dp">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:text="LONG"
android:textColor="#fefecc"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="BOOLEAN"
android:textColor="#fefecc"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:text="INT"
android:textColor="#fefecc"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="CHAR"
android:textColor="#fefecc"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:text="FLOAT"
android:textColor="#fefecc"
android:textSize="30sp"
android:textStyle="bold" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Maharashtra State Board of Technical Education 42


Mobile Application Development (22617)

XI. References / Suggestions for further Reading


1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com

XII. Assessment Scheme

Performance indicators Weightage


Process related (10 Marks) 30%
1. Logic Formation 10%
2. Debugging ability 15%
3. Follow ethical practices 5%
Maharashtra State Board of Technical Education 43
Mobile Application Development (22617)

IX. Practical related Questions


Note: Below given are few sample questions for reference. Teachers must
design more such questions to ensure the achievement of identified CO.
1. Which of these is not defined as a process state?
a. Non-visible
b. Visible
c. Foreground
d. Background

2. What is the name of the folder that contains the R.java file?
a. src
b. res
c. bin
d. gen

Ans 1 :
Non-visible is not defined as a process state.

Ans 2:
the name of the folder that contains the R.java file is Gen.

X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to accept username and password from the end user using Text
View and Edit Text.
2. Write a program to accept and display personal information of the student.

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

<TextView
android:id="@+id/Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UserName"
android:layout_marginTop="150dp"
android:layout_marginLeft="40dp" />

<EditText
android:id="@+id/User_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_toRightOf="@+id/Username"

Maharashtra State Board of Technical Education 47


Mobile Application Development (22617)
android:layout_marginTop="150dp"
/>

<TextView
android:id="@+id/Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:layout_marginTop="40dp"
android:layout_below="@+id/Username"
android:layout_marginLeft="40dp"
/>
<EditText
android:id="@+id/Pass_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginTop="190dp"
android:layout_toRightOf="@+id/Password"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Submit"
android:layout_marginTop="200dp"
android:layout_below="@+id/Pass_word"
/>
</RelativeLayout>

Maharashtra State Board of Technical Education 48


Mobile Application Development (22617)

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

<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Name" />

<EditText
android:id="@+id/editText_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="60dp"
android:layout_marginRight="@+id/Name"
android:ems="10" />

Maharashtra State Board of Technical Education 49


Mobile Application Development (22617)
<TextView
android:id="@+id/Age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Name"
android:layout_margin="30dp"
android:text="Age" />

<EditText
android:id="@+id/editText_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Name"
android:layout_margin="60dp"
android:layout_marginRight="@+id/Age"
android:ems="10" />

<Button
android:id="@+id/Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="260dp"
android:text="Submit" />

<TextView
android:id="@+id/Student_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Submit"
android:visibility="invisible" />

<TextView
android:id="@+id/Student_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Student_Name"
android:visibility="invisible" />
</RelativeLayout>

public class MainActivity extends AppCompatActivity {


EditText Name,Age;
String name,age;
TextView Display_Name,Display_Age;
Button display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display=findViewById(R.id.Submit);
display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
go();
}
});

Maharashtra State Board of Technical Education 50


Mobile Application Development (22617)

}
public void go()
{
Name=findViewById(R.id.editText_Name);
Age=findViewById(R.id.editText_age);

name=Name.getText().toString().trim();
age=Age.getText().toString().trim();

Display_Name=findViewById(R.id.Student_Name);
Display_Age=findViewById(R.id.Student_age);

Display_Name.setVisibility(View.VISIBLE);
Display_Name.setText("Name:"+name);

Display_Age.setVisibility(View.VISIBLE);
Display_Age.setText("Age:"+age);

}
}

Maharashtra State Board of Technical Education 51


Mobile Application Development (22617)

Maharashtra State Board of Technical Education 52


Mobile Application Development (22617)

X. Exercise
Note: Faculty must ensure that every group of students use different
input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to create a toggle button to display ON / OFF Bluetooth on the
display screen.
2. Write a program to create a simple calculator.

(Space for answers)


Ans 1.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> selector.xml
<RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.co
<selector
m/apk/res/android"
xmlns:android="http://schemas.android.co
xmlns:app="http://schemas.android.com
m/apk/res/android">
/apk/res-auto"
<item android:state_checked="true"
xmlns:tools="http://schemas.android.co
android:drawable="@drawable/bluetooth_
m/tools"
on"/>
android:layout_width="match_parent"
<item android:state_checked="false"
android:layout_height="match_parent"
android:drawable="@drawable/bluetooth_
tools:context=".MainActivity">
off"/>
<ToggleButton
</selector>
android:id="@+id/toggle"
android:layout_width="185dp"
android:layout_height="wrap_content
Java Code
"
package com.example.togglebutton;
android:layout_centerHorizontal="tru
e"
import
android:layout_marginHorizontal="1
androidx.appcompat.app.AppCompatActiv
30dp"
ity;
android:layout_marginTop="90dp"
android:drawableStart="@drawable/s
import android.os.Bundle;
elector"
import android.view.View;
android:textOff="Bluetooth Off"
import android.widget.ImageView;
android:textOn="Bluetooth ON" />
import android.widget.ToggleButton;
<ImageView
android:layout_width="wrap_content
public class MainActivity extends
"
AppCompatActivity {
android:layout_height="wrap_content
"
ToggleButton toggleButton;
android:id="@+id/imageView"
ImageView imageView;
android:layout_marginTop="100dp"
@Override
android:layout_below="@+id/toggle"
protected void onCreate(Bundle
android:layout_centerHorizontal="tru
savedInstanceState) {
e"
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ma
/> in);
</RelativeLayout>
Maharashtra State Board of Technical Education 58
Mobile Application Development (22617)
toggleButton=findViewById(R.id.tog imageView.setImageDrawabl
gle); e(getResources().getDrawable(R.drawable.
imageView=findViewById(R.id.imag bluetooth_on));
eView); }
else
imageView.setImageDrawable(getRe {
sources().getDrawable(R.drawable.bluetoo imageView.setImageDrawabl
th_off)); e(getResources().getDrawable(R.drawable.
bluetooth_off));
toggleButton.setOnClickListener(new }
View.OnClickListener(){ }
}
@Override );
public void onClick(View v){ }
if (toggleButton.isChecked()) }
{

Maharashtra State Board of Technical Education 59


Mobile Application Development (22617)
Ans 2.
android:layout_width="wrap_conte
XML code
nt"
<?xml version="1.0" encoding="utf-8"?> android:layout_height="wrap_cont
<RelativeLayout ent"
xmlns:android="http://schemas.android.co android:layout_below="@+id/btn_
m/apk/res/android" 6"
xmlns:app="http://schemas.android.com android:layout_toEndOf="@+id/bt
/apk/res-auto" n_5"
xmlns:tools="http://schemas.android.co android:layout_toRightOf="@+id/
m/tools" btn_5"
android:layout_width="match_parent" android:text="9"
android:layout_height="match_parent" android:textSize="18sp" />
tools:context=".MainActivity">
<Button
android:layout_width="match_parent android:id="@+id/btn_8"
" android:layout_width="wrap_conte
android:layout_height="match_parent nt"
" android:layout_height="wrap_cont
android:orientation="horizontal"> ent"
<Button android:layout_below="@+id/btn_
android:id="@+id/btn_1" 5"
android:layout_width="wrap_conte android:layout_toEndOf="@+id/bt
nt" n_7"
android:layout_height="wrap_cont android:layout_toRightOf="@+id/
ent" btn_7"
android:layout_alignParentLeft="tr android:text="8"
ue" android:textSize="18sp" />
android:layout_alignParentStart="t
rue" <Button
android:layout_below="@+id/edTe android:id="@+id/btn_7"
xt1" android:layout_width="wrap_conte
android:layout_marginTop="60dp" nt"
android:layout_height="wrap_cont
android:text="1" ent"
android:textSize="18sp" /> android:layout_alignLeft="@+id/bt
n_4"
<Button android:layout_alignStart="@+id/b
android:id="@+id/btn_0" tn_4"
android:layout_width="wrap_conte android:layout_below="@+id/btn_
nt" 4"
android:layout_height="wrap_cont android:text="7"
ent" android:textSize="18sp" />
android:layout_below="@+id/btn_
8" <Button
android:layout_toEndOf="@+id/bt android:id="@+id/btn_6"
n_7" android:layout_width="wrap_conte
android:layout_toRightOf="@+id/ nt"
btn_7" android:layout_height="wrap_cont
android:text="0" ent"
android:textSize="18sp" /> android:layout_alignBaseline="@+
id/btn_5"
<Button android:layout_alignBottom="@+i
android:id="@+id/btn_9" d/btn_5"
Maharashtra State Board of Technical Education 60
Mobile Application Development (22617)
android:layout_toEndOf="@+id/bt <Button
n_5" android:id="@+id/btn_2"
android:layout_toRightOf="@+id/ android:layout_width="wrap_conte
btn_5" nt"
android:text="6" android:layout_height="wrap_cont
android:textSize="18sp" /> ent"
android:layout_alignBaseline="@+
<Button id/btn_1"
android:id="@+id/btn_5" android:layout_alignBottom="@+i
android:layout_width="wrap_conte d/btn_1"
nt" android:layout_toEndOf="@+id/bt
android:layout_height="wrap_cont n_1"
ent" android:layout_toRightOf="@+id/
android:layout_below="@+id/btn_ btn_1"
2" android:text="2"
android:layout_toEndOf="@+id/bt android:textSize="18sp" />
n_4"
android:layout_toRightOf="@+id/ <Button
btn_4" android:id="@+id/btn_Add"
android:text="5" android:layout_width="wrap_conte
android:textSize="18sp" /> nt"
android:layout_height="wrap_cont
<Button ent"
android:id="@+id/btn_4" android:layout_above="@+id/btn_
android:layout_width="wrap_conte 6"
nt" android:layout_alignParentEnd="tr
android:layout_height="wrap_cont ue"
ent" android:layout_alignParentRight="
android:layout_alignLeft="@+id/bt true"
n_1" android:backgroundTint="@androi
android:layout_alignStart="@+id/b d:color/darker_gray"
tn_1" android:text="+"
android:layout_below="@+id/btn_ android:textColor="@android:colo
1" r/background_light"
android:text="4" android:textSize="18sp" />
android:textSize="18sp" />
<Button
<Button android:id="@+id/btn_Sub"
android:id="@+id/btn_3" android:layout_width="wrap_conte
android:layout_width="wrap_conte nt"
nt" android:layout_height="wrap_cont
android:layout_height="wrap_cont ent"
ent" android:layout_alignLeft="@+id/bt
android:layout_alignBaseline="@+ n_Add"
id/btn_2" android:layout_alignStart="@+id/b
android:layout_alignBottom="@+i tn_Add"
d/btn_2" android:layout_below="@+id/btn_
android:layout_toEndOf="@+id/bt Add"
n_2" android:backgroundTint="@androi
android:layout_toRightOf="@+id/ d:color/darker_gray"
btn_2" android:text="-"
android:text="3" android:textColor="@android:colo
android:textSize="18sp" /> r/background_light"
android:textSize="18sp" />
Maharashtra State Board of Technical Education 61
Mobile Application Development (22617)
ue"
<Button android:layout_marginTop="22dp"
android:id="@+id/btn_Mul" android:ems="10"
android:layout_width="wrap_conte android:inputType="textPersonNa
nt" me"
android:layout_height="wrap_cont android:textAlignment="textEnd"
ent" android:textSize="24sp" />
android:layout_alignLeft="@+id/bt
n_Sub" <Button
android:layout_alignStart="@+id/b android:id="@+id/btn_calc"
tn_Sub" android:layout_width="wrap_conte
android:layout_below="@+id/btn_ nt"
6" android:layout_height="wrap_cont
android:backgroundTint="@androi ent"
d:color/darker_gray" android:layout_below="@+id/btn_
android:text="*" 0"
android:textColor="@android:colo android:layout_toEndOf="@+id/bt
r/background_light" n_0"
android:textSize="18sp" /> android:layout_toRightOf="@+id/
btn_0"
<Button android:backgroundTint="@androi
android:id="@+id/btn_Div" d:color/holo_green_light"
android:layout_width="wrap_conte android:text="="
nt" android:textColor="@android:colo
android:layout_height="wrap_cont r/background_light"
ent" android:textSize="18sp" />
android:layout_alignLeft="@+id/bt
n_Mul" <Button
android:layout_alignStart="@+id/b android:id="@+id/btn_dec"
tn_Mul" android:layout_width="wrap_conte
android:layout_below="@+id/btn_ nt"
9" android:layout_height="wrap_cont
android:backgroundTint="@androi ent"
d:color/darker_gray" android:layout_below="@+id/btn_
android:text="/" 7"
android:textColor="@android:colo android:layout_toLeftOf="@+id/bt
r/background_light" n_8"
android:textSize="18sp" /> android:layout_toStartOf="@+id/bt
n_8"
<EditText android:text="."
android:id="@+id/edText1" android:textSize="18sp" />
android:layout_width="wrap_conte
nt" <Button
android:layout_height="wrap_cont android:id="@+id/btn_clear"
ent" android:layout_width="wrap_conte
android:layout_alignParentEnd="tr nt"
ue" android:layout_height="wrap_cont
android:layout_alignParentLeft="tr ent"
ue" android:layout_alignParentEnd="tr
android:layout_alignParentRight=" ue"
true" android:layout_alignParentRight="
android:layout_alignParentStart="t true"
rue" android:layout_below="@+id/btn_
android:layout_alignParentTop="tr Div"
Maharashtra State Board of Technical Education 62
Mobile Application Development (22617)
android:backgroundTint="@androi findViewById(R.id.btn_5);
d:color/holo_blue_dark" btn_6 = (Button)
findViewById(R.id.btn_6);
android:text="clear" btn_7 = (Button)
android:textColor="@android:colo findViewById(R.id.btn_7);
r/background_light" btn_8 = (Button)
android:textSize="18sp" /> findViewById(R.id.btn_8);
btn_9 = (Button)
findViewById(R.id.btn_9);
btn_Add = (Button)
findViewById(R.id.btn_Add);
</RelativeLayout> btn_Div = (Button)
findViewById(R.id.btn_Div);
btn_Sub = (Button)
Java Code findViewById(R.id.btn_Sub);
btn_Mul = (Button)
findViewById(R.id.btn_Mul);
btn_calc = (Button)
package com.example.exp9_2; findViewById(R.id.btn_calc);
btn_dec = (Button)
import findViewById(R.id.btn_dec);
androidx.appcompat.app.AppCompatActiv btn_clear = (Button)
findViewById(R.id.btn_clear);
ity; ed1 = (EditText)
findViewById(R.id.edText1);
import android.os.Bundle; btn_0.setOnClickListener(new
import android.view.View; View.OnClickListener() {
import android.widget.Button; @Override
import android.widget.EditText; public void onClick(View v) {
ed1.setText(ed1.getText()+"0");
public class MainActivity extends }
AppCompatActivity { });
Button
btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_ btn_1.setOnClickListener(new
7,btn_8,btn_9,btn_0,btn_Add,btn_Sub,btn View.OnClickListener() {
_Mul,btn_Div,btn_calc,btn_dec,btn_clear; @Override
EditText ed1; public void onClick(View v) {
ed1.setText(ed1.getText()+"1");
float Value1, Value2; }
boolean mAddition, mSubtract, });
mMultiplication, mDivision ;
@Override btn_2.setOnClickListener(new
protected void onCreate(Bundle View.OnClickListener() {
savedInstanceState) { @Override
super.onCreate( savedInstanceState ); public void onClick(View v) {
setContentView( ed1.setText(ed1.getText()+"2");
R.layout.activity_main ); }
btn_0 = (Button) });
findViewById(R.id.btn_0);
btn_1 = (Button)
findViewById(R.id.btn_1); btn_3.setOnClickListener(new
btn_2 = (Button) View.OnClickListener() {
findViewById(R.id.btn_2); @Override
btn_3 = (Button) public void onClick(View v) {
findViewById(R.id.btn_3);
btn_4 = (Button) ed1.setText(ed1.getText()+"3");
findViewById(R.id.btn_4);
btn_5 = (Button)

Maharashtra State Board of Technical Education 63


Mobile Application Development (22617)
} public void onClick(View v) {
}); ed1.setText(ed1.getText()+".");
}
btn_4.setOnClickListener(new });
View.OnClickListener() {
@Override btn_Add.setOnClickListener(new
public void onClick(View v) { View.OnClickListener() {
ed1.setText(ed1.getText()+"4"); @Override
} public void onClick(View v) {
});
if (ed1 == null){
btn_5.setOnClickListener(new ed1.setText("");
View.OnClickListener() { }else {
@Override Value1 =
public void onClick(View v) { Float.parseFloat(ed1.getText() + "");
ed1.setText(ed1.getText()+"5"); mAddition = true;
} ed1.setText(null);
}); }
}
btn_6.setOnClickListener(new });
View.OnClickListener() {
@Override btn_Sub.setOnClickListener(new
public void onClick(View v) { View.OnClickListener() {
ed1.setText(ed1.getText()+"6"); @Override
} public void onClick(View v) {
}); Value1 =
Float.parseFloat(ed1.getText() + "");
mSubtract = true ;
btn_7.setOnClickListener(new ed1.setText(null);
View.OnClickListener() { }
@Override });
public void onClick(View v) {
ed1.setText(ed1.getText()+"7"); btn_Mul.setOnClickListener(new
} View.OnClickListener() {
}); @Override
public void onClick(View v) {
btn_8.setOnClickListener(new Value1 =
View.OnClickListener() { Float.parseFloat(ed1.getText() + "");
@Override mMultiplication = true ;
public void onClick(View v) { ed1.setText(null);
ed1.setText(ed1.getText()+"8"); }
} });
});
btn_Div.setOnClickListener(new
btn_9.setOnClickListener(new View.OnClickListener() {
View.OnClickListener() { @Override
@Override public void onClick(View v) {
public void onClick(View v) { Value1 =
ed1.setText(ed1.getText()+"9"); Float.parseFloat(ed1.getText()+"");
} mDivision = true ;
}); ed1.setText(null);
btn_dec.setOnClickListener(new }
View.OnClickListener() { });
@Override

Maharashtra State Board of Technical Education 64


Mobile Application Development (22617)
btn_calc.setOnClickListener(new + "");
View.OnClickListener() { mMultiplication=false;
@Override }
public void onClick(View v) {
Value2 = if (mDivision == true){
Float.parseFloat(ed1.getText() + ""); ed1.setText(Value1 /
Value2+"");
if (mAddition == true){ mDivision=false;
}
ed1.setText(Value1 + Value2 }
+""); });
mAddition=false;
} btn_clear.setOnClickListener(new
View.OnClickListener() {
@Override
if (mSubtract == true){ public void onClick(View v) {
ed1.setText(Value1 - Value2 ed1.setText("");
+""); }
mSubtract=false; });
}
}
if (mMultiplication == true){ }
ed1.setText(Value1 * Value2

Output

Maharashtra State Board of Technical Education 65


Mobile Application Development (22617)

VIII. Resources required (Additional)


Sr. Instrument Specification Quantity Remarks
No. /Object
Android enabled 2 GB RAM 1 Data cable is
smartphone / Android mandatory for
1 version supporting emulators
emulator

IX. Practical related Questions


Note: Below given are few sample questions for reference. Teachers must
design more such questions to ensure the achievement of identified CO.
1. Name the file in which respective XML components can be added.
2. List all the UI components which can be used to develop login window.

(Space for answers)

1. xml file in android. The AndroidManifest. xml file contains information


of your package, including components of the application such as
activities, services, broadcast receivers, content providers etc.

2. . UI components used to develop login window are as follows:


i. TextView.
ii. EditText.
iii. Button.

X. Exercise
Note: Faculty must ensure that every group of students use different
input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to create a login form for a social networking site.
2. Write a program to create a login form for student registration system

(Space for answers)


An 1. import android.view.View;
package com.example.exp10_1; import android.widget.Button;
import android.widget.EditText;
import import android.widget.Toast;
androidx.appcompat.app.AppCo import
mpatActivity; android.widget.ToggleButton;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
Maharashtra State Board of Technical Education 69
Mobile Application Development (22617)
});
public class MainActivity extends
AppCompatActivity { }
}
EditText username;
String Username; <?xml version="1.0"
Button login; encoding="utf-8"?>
@Override <RelativeLayout
protected void onCreate(Bundle xmlns:android="http://schemas.a
savedInstanceState) { ndroid.com/apk/res/android"

super.onCreate(savedInstanceStat xmlns:app="http://schemas.andro
e); id.com/apk/res-auto"

setContentView(R.layout.activity_ xmlns:tools="http://schemas.andr
main); oid.com/tools"

username=findViewById(R.id.Use android:layout_width="match_pa
rname_ET); rent"

login=findViewById(R.id.Login); android:layout_height="match_p
arent"
login.setOnClickListener(new tools:context=".MainActivity">
View.OnClickListener()
{ <TextView
@Override android:id="@+id/Name"
public void onClick(View
v) { android:layout_width="wrap_con
tent"
Username=username.getText().toS
tring(); android:layout_height="wrap_co
ntent"
Toast.makeText(MainActivity.this android:text="Name"
,"Welcome
"+Username,Toast.LENGTH_SH android:layout_marginTop="40d
ORT).show(); p"
Intent intent=new
Intent(MainActivity.this,Empty_A android:layout_centerHorizontal=
ctivity.class); "true"
// android:textSize="20dp"
intent.putExtra("username",User />
name); <EditText
startActivity(intent);
android:id="@+id/Name_ET"
}
Maharashtra State Board of Technical Education 70
Mobile Application Development (22617)
android:layout_width="match_pa
rent" android:inputType="textPersonN
ame"
android:layout_height="wrap_co
ntent" android:layout_marginTop="20d
android:ems="10" p"

android:inputType="textPersonN android:layout_below="@+id/Age
ame" "
/>
android:layout_below="@+id/Na <TextView
me" android:id="@+id/Branch"

android:layout_marginTop="20d android:layout_width="wrap_con
p" tent"
/>
<TextView android:layout_height="wrap_co
android:id="@+id/Age" ntent"
android:text="Branch"
android:layout_width="wrap_con
tent" android:layout_marginTop="40d
p"
android:layout_height="wrap_co
ntent" android:layout_centerHorizontal=
android:text="Age" "true"

android:layout_marginTop="40d android:layout_below="@+id/Age
p" _ET"
android:textSize="20dp"
android:layout_centerHorizontal=
"true" />
<EditText
android:layout_below="@+id/Na
me_ET" android:id="@+id/Branch_ET"
android:textSize="20dp"
/> android:layout_width="match_pa
<EditText rent"
android:id="@+id/Age_ET"
android:layout_height="wrap_co
android:layout_width="match_pa ntent"
rent" android:ems="10"

android:layout_height="wrap_co android:inputType="textPersonN
ntent" ame"
android:ems="10"
Maharashtra State Board of Technical Education 71
Mobile Application Development (22617)
android:layout_marginTop="20d
p" android:layout_below="@+id/Se
mester"
android:layout_below="@+id/Bra />
nch" <Button
/> android:id="@+id/Login"
<TextView
android:id="@+id/Semester" android:layout_width="wrap_con
tent"
android:layout_width="wrap_con
tent" android:layout_height="wrap_co
ntent"
android:layout_height="wrap_co android:text="Login"
ntent"
android:text="Semester" android:layout_below="@+id/Se
mester_ET"
android:layout_marginTop="40d
p" android:layout_marginTop="40d
p"
android:layout_centerHorizontal=
"true" android:layout_centerHorizontal=
"true"
android:layout_below="@+id/Bra />
nch_ET"
android:textSize="20dp" <TextView
/> android:id="@+id/Login"
<EditText
android:layout_width="wrap_con
android:id="@+id/Semester_ET" tent"

android:layout_width="match_pa android:layout_height="wrap_co
rent" ntent"
android:text="Student Login"
android:layout_height="wrap_co android:textSize="40dp"
ntent"
android:ems="10" android:layout_centerHorizontal=
"true"
android:inputType="textPersonN
ame" android:textColor="@color/teal_2
00"
android:layout_marginTop="20d />
p" </RelativeLayout>

Output:

Maharashtra State Board of Technical Education 72


Mobile Application Development (22617)

Ans 2.
XML file
<?xml version="1.0" android:layout_width="match_pa
encoding="utf-8"?> rent"
<RelativeLayout
xmlns:android="http://schemas.a android:layout_height="match_p
ndroid.com/apk/res/android" arent"
tools:context=".MainActivity">
xmlns:app="http://schemas.andro
id.com/apk/res-auto" <TextView
android:id="@+id/Name"
xmlns:tools="http://schemas.andr
oid.com/tools" android:layout_width="wrap_con
Maharashtra State Board of Technical Education 73
Mobile Application Development (22617)
tent"
android:layout_centerHorizontal=
android:layout_height="wrap_co "true"
ntent"
android:text="Name" android:layout_below="@+id/Na
me_ET"
android:layout_marginTop="40d android:textSize="20dp"
p" />
<EditText
android:layout_centerHorizontal= android:id="@+id/Age_ET"
"true"
android:textSize="20dp" android:layout_width="match_pa
/> rent"
<EditText
android:layout_height="wrap_co
android:id="@+id/Name_ET" ntent"
android:ems="10"
android:layout_width="match_pa
rent" android:inputType="textPersonN
ame"
android:layout_height="wrap_co
ntent" android:layout_marginTop="20d
android:ems="10" p"

android:inputType="textPersonN android:layout_below="@+id/Age
ame" "
/>
android:layout_below="@+id/Na <TextView
me" android:id="@+id/Branch"

android:layout_marginTop="20d android:layout_width="wrap_con
p" tent"
/>
<TextView android:layout_height="wrap_co
android:id="@+id/Age" ntent"
android:text="Branch"
android:layout_width="wrap_con
tent" android:layout_marginTop="40d
p"
android:layout_height="wrap_co
ntent" android:layout_centerHorizontal=
android:text="Age" "true"

android:layout_marginTop="40d android:layout_below="@+id/Age
p" _ET"
Maharashtra State Board of Technical Education 74
Mobile Application Development (22617)
android:textSize="20dp" android:id="@+id/Semester_ET"

/> android:layout_width="match_pa
<EditText rent"

android:id="@+id/Branch_ET" android:layout_height="wrap_co
ntent"
android:layout_width="match_pa android:ems="10"
rent"
android:inputType="textPersonN
android:layout_height="wrap_co ame"
ntent"
android:ems="10" android:layout_marginTop="20d
p"
android:inputType="textPersonN
ame" android:layout_below="@+id/Se
mester"
android:layout_marginTop="20d />
p" <Button
android:id="@+id/Login"
android:layout_below="@+id/Bra
nch" android:layout_width="wrap_con
/> tent"
<TextView
android:id="@+id/Semester" android:layout_height="wrap_co
ntent"
android:layout_width="wrap_con android:text="Login"
tent"
android:layout_below="@+id/Se
android:layout_height="wrap_co mester_ET"
ntent"
android:text="Semester" android:layout_marginTop="40d
p"
android:layout_marginTop="40d
p" android:layout_centerHorizontal=
"true"
android:layout_centerHorizontal= />
"true"
<TextView
android:layout_below="@+id/Bra android:id="@+id/Login"
nch_ET"
android:textSize="20dp" android:layout_width="wrap_con
/> tent"
<EditText
android:layout_height="wrap_co
Maharashtra State Board of Technical Education 75
Mobile Application Development (22617)
ntent" e);
android:text="Student Login"
android:textSize="40dp" setContentView(R.layout.activity_
main);
android:layout_centerHorizontal=
"true" name_et=findViewById(R.id.Nam
e_ET);
android:textColor="@color/teal_2
00" age_et=findViewById(R.id.Age_E
/> T);
</RelativeLayout>
branch_et=findViewById(R.id.Br
Java File: anch_ET);
package com.example.exp10_2;
sem_et=findViewById(R.id.Semes
ter_ET);
import login
androidx.appcompat.app.AppCo =findViewById(R.id.Login);
mpatActivity;
import android.content.Intent; login.setOnClickListener(new
import android.os.Bundle; View.OnClickListener()
import android.view.View; {
import android.widget.Button; @Override
import android.widget.EditText; public void onClick(View
import android.widget.Toast; v) {
import
android.widget.ToggleButton; Username=name_et.getText().toSt
ring();
import android.os.Bundle;
Age=age_et.getText().toString();
public class MainActivity extends
AppCompatActivity { Branch=branch_et.getText().toStr
ing();
EditText
name_et,age_et,branch_et,sem_et Semester=sem_et.getText().toStrin
; g();
String
Username,Age,Branch,Semester; Toast.makeText(MainActivity.this
Button login; ,"Welcome "+Username
,Toast.LENGTH_SHORT).show()
@Override ;
protected void onCreate(Bundle
savedInstanceState) { Toast.makeText(MainActivity.this
,"Age "+Age
super.onCreate(savedInstanceStat ,Toast.LENGTH_SHORT).show()
Maharashtra State Board of Technical Education 76
Mobile Application Development (22617)
; Intent(MainActivity.this,Empty_A
ctivity.class);
Toast.makeText(MainActivity.this //
,"Branch "+Branch intent.putExtra("username",User
,Toast.LENGTH_SHORT).show() name);
; startActivity(intent);

Toast.makeText(MainActivity.this }
,"Semester "+Semester });
,Toast.LENGTH_SHORT).show()
; }
}
Intent intent=new

Output:

Maharashtra State Board of Technical Education 77


Mobile Application Development (22617)

(Space for answers)

Ans 1.
XML Code:
android:id="@+id/radioButt
<?xml version="1.0" on1"
encoding="utf-8"?>
<RelativeLayout android:layout_width="wrap
xmlns:android="http://schem _content"
as.android.com/apk/res/andro
id" android:layout_height="wrap
_content"
xmlns:app="http://schemas.a android:text="Radio
ndroid.com/apk/res-auto" Button 1"
xmlns:tools="http://schemas. android:layout_marginTop="
android.com/tools" 80dp"
android:textSize="40dp"
android:layout_width="matc />
h_parent" <RadioButton
android:layout_height="matc android:id="@+id/radioButt
h_parent" on2"
tools:context=".MainActivity android:layout_width="wrap
"> _content"
<TextView android:layout_height="wrap
_content"
android:id="@+id/textv" android:text="Radio
Button 2"
android:layout_width="wrap
_content" android:layout_marginTop="
40dp"
android:layout_height="wrap
_content" android:layout_below="@+id
android:text="Single /radioButton1"
Radio Buttons" android:textSize="40dp"
/>
android:textColor="#000000
" <RadioGroup
android:visibility="visible" android:layout_below="@+id
/radioButton2"
android:layout_centerHorizo
ntal="true" android:layout_width="matc
android:textSize="20dp" h_parent"
android:layout_marginTop=" android:layout_height="200d
20dp" p"
/>
<RadioButton android:layout_marginTop="
Maharashtra State Board of Technical Education 82
Mobile Application Development (22617)
50dp" android:checked="true"

android:id="@+id/radioGrou android:textSize="40dp"
p"> />
</RadioGroup>
<TextView <Button

android:id="@+id/textvr" android:id="@+id/Select"

android:layout_width="wrap android:layout_width="wrap
_content" _content"

android:layout_height="wrap android:layout_height="wrap
_content" _content"
android:text="Radio android:text="Select"
button inside RadioGroup"
android:layout_centerHorizo
android:textSize="20dp" ntal="true"

android:textColor="#000000 android:layout_below="@+id
" /radioGroup"
/>
android:visibility="visible" </RelativeLayout>
Java Code:
android:layout_marginTop="
25dp" package com.example.exp12;
/>
<RadioButton import
androidx.appcompat.app.App
android:id="@+id/male" CompatActivity;

android:layout_width="wrap import android.os.Bundle;


_content" import android.view.View;
import
android:layout_height="wrap android.widget.Button;
_content" import
android:text="Male" android.widget.RadioButton;
import
android:checked="true" android.widget.RadioGroup;
import android.widget.Toast;
android:textSize="40dp"
/>
<RadioButton public class MainActivity
extends AppCompatActivity {
android:id="@+id/female"
RadioButton
android:layout_width="wrap radgender,rad1,rad2;
_content" RadioGroup radSelect;
String gender;
android:layout_height="wrap Button select;
_content"
@Override
android:text="Female" protected void
onCreate(Bundle
Maharashtra State Board of Technical Education 83
Mobile Application Development (22617)
savedInstanceState) { {
int radioID=
super.onCreate(savedInstance radSelect.getCheckedRadioB
State); uttonId();

setContentView(R.layout.acti radgender=findViewById(rad
vity_main); ioID);

radSelect=findViewById(R.id gender=radgender.getText().t
.radioGroup); oString().trim();
if(rad1.isChecked())
rad1=findViewById(R.id.radi {
oButton1); Toast.makeText(this,
"Radio button 1 is selected",
rad2=findViewById(R.id.radi Toast.LENGTH_SHORT).sho
oButton2); w();
}
select=findViewById(R.id.Sel if(rad2.isChecked())
ect); {
Toast.makeText(this,
select.setOnClickListener( "Radio button 2 is selected",
new View.OnClickListener() { Toast.LENGTH_SHORT).sho
public w();
void onClick(View v) { }
Toast.makeText(this,
select(); "Gender :" + gender,
} Toast.LENGTH_SHORT).sho
} w();
); }
} }
public void select()

Maharashtra State Board of Technical Education 84


Mobile Application Development (22617)

XI. References / Suggestions for further Reading


1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com

Maharashtra State Board of Technical Education 85


Mobile Application Development (22617)
X. Exercise
Note: Below given are few sample questions for reference. Teachers must design
different questions for practice.
(Use blank space for answers or attach more pages if needed)
1. Write a program to display circular progress bar.
2. Write a program to show the following output.

(Space for answers)


Ans 1:
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
JAVA CODE
xmlns:android="http://schemas.android.co
m/apk/res/android" package com.example.progrogress;
xmlns:app="http://schemas.android.com
/apk/res-auto"
xmlns:tools="http://schemas.android.co import
m/tools" androidx.appcompat.app.AppCompatActiv
android:layout_width="match_parent" ity;
android:layout_height="match_parent" import android.app.ProgressDialog;
tools:context=".MainActivity"> import android.os.Bundle;
import android.view.View;
<Button import android.widget.Button;
android:id="@+id/btnDownloadFile" import android.os.Bundle;
android:layout_width="wrap_content
" public class MainActivity extends
android:layout_height="wrap_content AppCompatActivity {
" Button btnDownloadFile;
android:text="Download File" ProgressDialog progressDialog;
android:layout_centerInParent="true" @Override
/> protected void onCreate(Bundle
savedInstanceState) {
</RelativeLayout> super.onCreate(savedInstanceState);
Maharashtra State Board of Technical Education 89
Mobile Application Development (22617)
setContentView(R.layout.activity_ma while (progress <=100){
in); try {
btnDownloadFile = progressDialog.setPro
findViewById(R.id.btnDownloadFile); gress(progress);
btnDownloadFile.setOnClickListener progress++;
(new View.OnClickListener() { Thread.sleep(200);
@Override }catch (Exception ex){
public void onClick(View v) {
progressDialog = new }
ProgressDialog (MainActivity.this); }
progressDialog.setProgressStyle( progressDialog.dismiss();
ProgressDialog.STYLE_HORIZONTAL); }
progressDialog.setTitle("File });
Downloading"); t.start();
progressDialog.setProgress(100) progressDialog.show();
; }
Thread t = new Thread(new });
Runnable() { }
@Override
}
public void run() {
int progress = 0;
OUTPUT

Maharashtra State Board of Technical Education 90


Mobile Application Development (22617)

IX. Practical related Questions


Note: Below given are few sample questions for reference. Teachers must designmore
such questions to ensure the achievement of identified CO.
1. List all attributes of Image View.
2. Write steps to add following string array to grid view.
static final String [] example= new String {“A”, “B”, “C”, “D”, “E”};
3. Describe android:stretchMode attribute of Grid view in detail.
(Space for answers)

Ans 1) All the attributes of Image view are as follows:


1. android:adjustViewBounds
2. android:baseline
3. android:baselineAlignBottom
4. android:cropToPadding
5. android:maxHeight
6. android:maxWidth
7. android:scaleType
8. android:src.
9. android:tint
10.android:tintMode

Ans 2) Steps are as follows:


1. Create a main layout.
2. Paste the following xml and java code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="50dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >

Maharashtra State Board of Technical Education 95


Mobile Application Development (22617)

</GridView>
ActivityMain.java
import android.app.Activity;

import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;

import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
GridView grid;
static final String[] letters = new String[] {
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
grid = (GridView) findViewById(R.id.gridView);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, letters);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Mah arashtra State Board of Technical Education 96
Mobile Application Development (22617)

Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});

}
}
3. Run the application

Ans 3) android:stretchMode : Defines how columns should stretch to fill the


available empty space, if any. This must be either of the values : none : Stretching
is disabled. spacingWidth : The spacing between each column is stretched.

X. Exercise
(Use blank space provide for answers or attached more pages if needed)
1. Write a program to show the following output. Use appropriate view for the same.

2. Write a program to display an image using Image View and a button


named as “Change Image”. Once you click on button another image

Maharashtra State Board of Technical Education 97


Mobile Application Development (22617)
should get displayed.
3. Write a program to display 15 buttons using grid view.
4. Write a program to display a text view using vertical scroll view.

(Space for answers)

Ans
XML code
import
<?xml version="1.0" encoding="utf-8"?> androidx.appcompat.app.AppCompatActiv
<RelativeLayout ity;
xmlns:android="http://schemas.android.co
m/apk/res/android" import android.os.Bundle;
xmlns:app="http://schemas.android.com
/apk/res-auto" public class MainActivity extends
xmlns:tools="http://schemas.android.co AppCompatActivity {
m/tools"
android:layout_width="match_parent" @Override
android:layout_height="match_parent" protected void onCreate(Bundle
tools:context=".MainActivity"> savedInstanceState) {
super.onCreate(savedInstanceState);
<ListView setContentView(R.layout.activity_ma
android:layout_width="match_parent in);
" }
android:layout_height="match_parent }
"
android:entries="@array/Language_li
st"
/>

</RelativeLayout>
strings.xml
<resources>
<string
name="app_name">Exp14_1</string>
<array name="Language_list">
<item>Android</item>
<item>Java</item>
<item>PHP</item>
<item>Hadoop</item>
<item>Sap</item>
<item>Phython</item>
<item>Ajax</item>
<item>C++</item>
<item>Puby</item>
<item>Rails</item>
</array>
</resources>
Java Code
package com.example.exp14_1;
Maharashtra State Board of Technical Education 98
Mobile Application Development (22617)

Output:

Maharashtra State Board of Technical Education 99


Mobile Application Development (22617)

Ans 2:

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#458"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="347dp" />

<Button
android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="58dp"
android:gravity="center"
android:onClick="mess"
android:text="First Image "
android:textSize="20sp" />
<Button
android:id="@+id/button2"
android:layout_width="218dp"
android:layout_height="63dp"
android:onClick="mess"
android:text="Second Image"
android:textSize="20sp" />
</LinearLayout>
ActivityMain.java
package com.example.exp14_2;
Maharashtra State Board of Technical Education 100
Mobile Application Development (22617)

import
android.os.Bundle;
import
android.view.View;
import
android.widget.ImageView;
import
android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void mess(View v)
{
//initialize image view object
ImageView im=(ImageView)findViewById(R.id.imageView1);
//get clicked button id from view object
switch(v.getId())
{
case R.id.button1:
//if button1 is clicked than set image1
im.setImageResource(R.drawable.myi
mage); break;
case R.id.button2:
//if button2 is clicked than set image2
im.setImageResource(R.drawable.myi

Maharashtra State Board of Technical Education 101


Mobile Application Development (22617)

mage2); break;
}
}
}

Ans 3
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="50dp"
Maharashtra State Board of Technical Education 102
android:listSelector="@color/white"
Mobile Application Development (22617)

android:numColumns="3"
android:padding="20dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
tools:context=".MainActivity" />
MainActivity.java
package com.example.exp14_3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.activity_grid);
gridview.setAdapter(new ButtonAdapter(this));
}
}
ButtonAdapter.java
package com.example.exp14_3;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;
public class ButtonAdapter extends BaseAdapter
{
private Context mContext;
private int btn_id;
private int total_btns = 15;
Maharashtra State Board of Technical Education 103
Mobile Application Development (22617)

public ButtonAdapter(Context context) {


this.mContext = context;
}
@Override
public int getCount() {
return total_btns;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup)
{
Button btn;
if (view == null) {
btn = new Button(mContext);
btn.setText(" Button " + (++btn_id));
btn.setLayoutParams (new ViewGroup.LayoutParams(300,300));
} else {
btn = (Button) view;
}
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Maharashtra State Board of Technical Education 104
Mobile Application Development (22617)

Toast.makeText(v.getContext(), "Button #" +


(i + 1), Toast.LENGTH_SHORT).show();
}
});
return btn;
}
}

4) Ans:
XML File:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Exp_14_3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Scroll down"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="1000dp"
android:text="You have scrolled too far..."
android:textSize="20dp"/>
</RelativeLayout>
</ScrollView>
Java File:
package com.example.exp14_4;
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);
}
}
Maharashtra State Board of Technical Education 105
X. Exercise
(Use blank space provide for answers or attached more pages if needed)
1. Write a program to create a HelloWorld Activity using all lifecycles methods to
display messages using Log.d.

(Space for answers)

Ans1:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
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">

</androidx.constraintlayout.widget.ConstraintLayout>

Java code
package com.example.exp_17_1;

import
androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.os.Bundle;

public class MainActivity extends

AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked by meena
talele");
}
@Override
protected void onStart() {
super.onStart();
Log.i("lifecycle","onStart
invoked");
}
@Override
protected void onResume(){
super.onResume();
Log.v("lifecycle","onResume
invoked");
}
@Override
protected void onPause(){
super.onPause();
Log.d("lifecycle","onPause
invoked");
}
@Override
protected void onStop(){
super.onStop();
Log.d("lifecycle","onStop
invoked");
}

@Override
protected void onRestart(){
super.onRestart();
Log.d("lifecycle","onRestart
invoked");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d("lifecycle","onDestroy
invoked");
}
//DONE BY Heet Zatakia

Output:

XI. References / Suggestions for further Reading


1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com

XII. Assessment Scheme

Performance indicators Weightage


Process related (10 Marks) 30%
1. Logic Formation 10%
2. Debugging ability 15%
3. Follow ethical practices 5%
Product related (15 Marks) 70%
4. Interactive GUI 20%
5. Answer to Practical related questions 20%
6. Expected Output 20%
7. Timely Submission 10%
Total (25 Marks) 100%
Mobile Application Development (22617)
Java Code:
package com.example.exp18_1;
import androidx.appcompat.app.AppCompatActivity; import
android.content.Intent;
import android.net.Uri;
import android.os.Bundle; import
android.view.View; import
android.widget.Button; import
android.widget.EditText;

public class MainActivity extends AppCompatActivity {


EditText editText;
Button btnNavigate; String
text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnNavigate=findViewById(R.id.btnNavigate);
btnNavigate.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
editText=findViewById(R.id.edittext);
text=editText.getText().toString().trim();
if(text.equals("www.youtube.com"))
{
go();

}
}
});
}
public void go()
{
Uri uri=Uri.parse("https://www.youtube.com"); Intent
intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
}

<?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/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
Maharashtra State Board of Technical Education 11
Mobile Application Development (22617)
android:hint="Enter URL"
/>
<Button
android:id="@+id/btnNavigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate"
android:layout_below="@+id/edittext"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
/>
</RelativeLayout>

Output:

Ans 2:

<?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">
Maharashtra State Board of Technical Education 12
Mobile Application Development (22617)

<Button
android:id="@+id/btnOpenDialer" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Open Dialer"
android:layout_centerInParent="true"
/>

</RelativeLayout>

Java

package com.example.exp18_2;

import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;


import android.view.View;
import android.widget.Button; import android.os.Bundle;

public class MainActivity extends AppCompatActivity { Button btnOenDialer;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
btnOenDialer=findViewById(R.id.btnOpenDialer); btnOenDialer.setOnClickListener(new
View.OnClickListener() {

@Override

public void onClick(View v) { Intent intent=new

Intent((Intent.ACTION_DIAL));
startActivity(intent);

}
);
}
}

Maharashtra State Board of Technical Education 13


Mobile Application Development (22617)

Ans 3) 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:id=”@+id/relativeLayout”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<EditText
android:id=”@+id/editText”
android:layout_width=”315dp”
android:layout_height=”53dp”
android:layout_centerHorizontal=”true”
android:layout_centerVertical=”true”
android:fontFamily=”@font/poppins_medium”
android:hint=”Enter a number”
android:inputType=”number”
android:textSize=”20sp”
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.371”/>

Maharashtra State Board of Technical Education 14


Mobile Application Development (22617)
<Button
android:id=”@+id/btnFact”
android:layout_width=”195dp”
android:layout_height=”43dp”
android:layout_marginTop=”84dp”
android:fontFamily=”@font/poppins_medium”
android:text=”Factorial”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/editText”/>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.exp18_2;
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;
public class MainActivity extends AppCompatActivity
{EditText editText;
Button btnFact;
int fact = 1;
int num=0;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
btnFact = findViewById(R.id.btnFact);
btnFact.setOnClickListener(new View.OnClickListener()
{@Override
public void onClick(View v)
{go();
}
});
}
public void go(){
num = Integer.parseInt(editText.getText().toString().trim());
for(int i = 1; i <= num; i++){
fact = fact * i;
}
navigate();
}
public void navigate(){
Intent intent = new Intent(MainActivity.this,EmptyActivity.class);
intent.putExtra(“Fact”,fact);
intent.putExtra(“Num”,num);
startActivity(intent);
}
}

Maharashtra State Board of Technical Education 15


Mobile Application Development (22617)
activity_empty.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=”.EmptyActivity”>
<TextView
android:id=”@+id/textview1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”250dp”
android:fontFamily=”@font/poppins_medium”
android:text=””
android:textColor=”#FFFFFF”
android:textSize=”24sp”/>
<TextView
android:id=”@+id/textview2”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textview1”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”20dp”
android:fontFamily=”@font/poppins_medium”
android:text=””
android:textColor=”#FFFFFF”
android:textSize=”24sp” />
</RelativeLayout>

EmptyActivity.java

package com.example.exp18_2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class EmptyActivity extends AppCompatActivity
{TextView textView1,textView2;
int num,fact;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_empty);
Intent intent = getIntent();
num = intent.getIntExtra(“Num”,0);
fact = intent.getIntExtra(“Fact”,0);
textView1 = findViewById(R.id.textview1);
textView2 = findViewById(R.id.textview2);
textView1.setText(“Number = “+ num);
textView2.setText(“Factorial = “+ fact);
}
Maharashtra State Board of Technical Education 136
Mobile Application Development (22617)
}

I. References / Suggestions for furtherReading


1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com

II. Assessment Scheme

Performance indicators Weightage


Process related (10 Marks) 30%
1. Logic Formation 10%
2. Debugging ability 15%
3. Follow ethical practices 5%
Product related (15 Marks) 70%
4. Interactive GUI 20%
5. Answer to Practical related questions 20%
6. Expected Output 20%
7. Timely Submission 10%
Total (25 Marks) 100%

Maharashtra State Board of Technical Education 137


Mobile Application Development (22617)

Ans 2) Broadcast Receivers Lifecycle

Ans 3) System Events related to Broadcast Receivers are as follows:


1. android.intent.action.BATTERY_CHANGED
2. android.intent.action.BATTERY_LOW
3. android.intent.action.BATTERY_OKAY
4. android.intent.action.BOOT_COMPLETED
5. android.intent.action.BUG_REPORT
6. android.intent.action.CALL
7. android.intent.action.CALL_BUTTON
X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to demonstrate all the system broadcast messages.

(Space for answers)

ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/a
pk/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_marginStart="97dp"
android:layout_marginTop="341dp"
android:text="Broadcasting button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Maharashtra State Board of Technical Education 141


Mobile Application Development (22617)
MainActivity.java

package com.example.exp21;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity


{Button button;
MyReceivermyReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{ MainActivity.this.registerReceiver(myReceiver,filter);
}
});
}

@Override
protected void onDestroy()
{ super.onDestroy();
MainActivity.this.
unregisterReceiver(myReceiver);
}
}

MyReceiver.java
package com.example.exp21;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver


{public MyReceiver(){
super();
}

@Override
public void onReceive(Context context, Intent intent)
{String intentAction = intent.getAction();
if(intentAction != null){
String toastMessage = "unknown intent action";
Maharashtra State Board of Technical Education 142
Mobile Application Development (22617)
switch(intentAction){
case Intent.ACTION_AIRPLANE_MODE_CHANGED:
boolean status = intent.getBooleanExtra("state",true);
if(status){
toastMessage = "Airoplane Mode turned on";
}else {
toastMessage = "Airoplane Mode turned off";
}
break;
}
Toast.makeText(context,toastMessage,Toast.LENGTH_SHORT).show();
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast_messages">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Exp21">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<receiver android:name=".MyReceiver"
android:exported="false">
</receiver>

</application>

</manifest>

Maharashtra State Board of Technical Education 143


Mobile Application Development (22617)
(SensorEvent) method so you don’t block it.

iii. Avoid using deprecated methods or sensor types: Several methods and constants
have been deprecated. In particular, the TYPE_ORIENTATION sensor type has been
deprecated.

iv. Verify sensors before you use them: Always verify that a sensor exists on a device
before you attempt to acquire data from it. Don&#39;t assume that a sensor exists
simply because it&#39;s a frequently-used sensor.

v. Choose sensor delays carefully: Sensors can provide data at very high rates.
Allowing the system to send extra data that you don&#39;t need wastes systemresources
and uses battery power.

Ans 2. i. Sensor Class: The android.hardware.Sensor class provides methods to get


information about the sensor such as sensor name, sensor type, sensor resolution,
sensor type etc.

ii. Sensor Manager Class: The android.hardware.SensorManager class provides


methods :
● to get sensor instance,
● to access and list sensors,
● to register and unregister sensor listeners etc.

You can get the instance of SensorManager by calling the method getSystemService() and
passing the SENSOR_SERVICE constant in it.

SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);

Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
3. Write a program to changes the background color when device is shuffled.
4. Write a program to display the list of sensors supported by the mobile device.

(Space for answers)


Ans 3) 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”
android:id=”@+id/Layout”
tools:context=”.MainActivity”>
</androidx.constraintlayout.widget.ConstraintLayout>

Maharashtra State Board of Technical Education 147


Mobile Application Development (22617)
MainActivity.java
package com.example.exp22;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity
{ConstraintLayout constraintLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraintLayout = findViewById(R.id.Layout);
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT){
constraintLayout.setBackgroundColor( Color.parseColor(“#2cbdf2”));
}
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE){ constraintLayout.setBackgroundColor(Color.parse
Color”#f083f2”));
}}}

Maharashtra State Board of Technical Education 148


Mobile Application Development (22617)

Ans 4) actvity_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”>
<ListView
android:id=”@+id/listView1”
android:layout_width=”match_parent”
android:layout_height=”match_parent” />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.exp22_1;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
public class MainActivity extends AppCompatActivity
{SensorManager smm;
List<Sensor> sensor;
ListView lv;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
lv = (ListView) findViewById (R.id.listView1);
sensor = smm.getSensorList(Sensor.TYPE_ALL);
lv.setAdapter(new ArrayAdapter<Sensor>(this,android.R.layout.simple_list_item_1,
sensor));
}
}

Maharashtra State Board of Technical Education 149


Mobile Application Development (22617)

Maharashtra State Board of Technical Education 150


Mobile Application Development (22617)

Ans 2) startFaceDetection() method is used to detect the face.


X. Exercise
Note: Faculty must ensure that every group of students use different input value. (Solve
any one of the following. Use blank space for answers or attach more pages if needed)
1. Write a program to capture an image and display it using image view.
2. Write a program to record a video using various camera methods.

(Space for answers)

Ans 1) activity_main.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”
android:orientation=”vertical”>
<ImageView
android:id=”@+id/imageView”
android:layout_width=”fill_parent”
android:layout_height=”403dp”
android:layout_centerHorizontal=”true” />
<Button
android:id=”@+id/button”
android:layout_width=”match_parent”
android:layout_height=”73dp”
android:fontFamily=”@font/poppins_medium”
android:text=”Click here to capture image using camera”/>
</LinearLayout>

MainActivity.java
package com.example.exp23;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;

Maharashtra State Board of Technical Education 154


Mobile Application Development (22617)
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{Button button ;
ImageView imageView ;
public static final int RequestPermissionCode = 1 ;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);
EnableRuntimePermission();
button.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View view)
{Intent intent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 7 && resultCode == RESULT_OK)
{Bitmap bitmap = (Bitmap) data.getExtras().get(“data”);
imageView.setImageBitmap(bitmap);
}
}
public void EnableRuntimePermission(){
Maharashtra State Board of Technical Education 155
Mobile Application Development (22617)
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manif
est.permission.CAMERA))
{
Toast.makeText(MainActivity.this,”CAMERA permission allows us to
Access CAMERA app”, Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this,new
String[]{Manifest.permission.CAMERA}, RequestPermissionCode);
}
}
@Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult)
{switch (RC) {
case RequestPermissionCode:
if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,&quot;Permission Granted, Now your
application can access CAMERA.&quot;, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,”Permission Canceled, Now your
application cannot access CAMERA.”, Toast.LENGTH_LONG).show();
}
break;
}
}
}
AndroidManifest.xml

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


<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.exp23”>
<uses-permission android:name=”android.permission.CAMERA” />
<uses-feature
android:name=”android.hardware.camera2”
android:required=”true”/>
<application
android:allowBackup=”true”
Maharashtra State Board of Technical Education 156
Mobile Application Development (22617)
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/Theme.Exp23”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER”/>
</intent-filter>
</activity>
<meta-data
android:name=”preloaded_fonts”
android:resource=”@array/preloaded_fonts”/>
</application>
</manifest>

Maharashtra State Board of Technical Education 157


Mobile Application Development (22617)

Ans 2) activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Capture Video"
android:onClick="captureVideo"
android:layout_gravity="center_horizontal"
android:layout_marginTop="150dp"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Play Video"
android:onClick="playVideo"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
/>
</LinearLayout>
MainActivty.java
package com.example.exp23_2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
public class MainActivity extends AppCompatActivity
{private static int VIDEO_REQUEST = 101;
private Uri videoUri = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void captureVideo(View view) {
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if(videoIntent.resolveActivity(getPackageManager())!=null)
{ startActivityForResult(videoIntent,VIDEO_REQUEST);
}
}
public void playVideo(View view) {
Intent playIntent = new Intent(this,VideoPlayActivity.class);
playIntent.putExtra("videoUri", videoUri.toString());
startActivity(playIntent);
Maharashtra State Board of Technical Education 158
Mobile Application Development (22617)
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{super.onActivityResult(requestCode, resultCode, data);
if (requestCode == VIDEO_REQUEST && resultCode == RESULT_OK)
{videoUri = data.getData();
}
}
}
activity_play_video.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=".VideoPlayActivity">
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="35dp"
android:id="@+id/videoView"
/>
</RelativeLayout>
VideoPlayActivity.java
package com.example.exp23_2;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;
public class VideoPlayActivity extends AppCompatActivity
{private VideoView mVideoView;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play);
mVideoView = findViewById(R.id.videoView);
Uri videoUri = Uri.parse(getIntent().getExtras().getString("videoUri"));
mVideoView.setVideoURI(videoUri);
mVideoView.start();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp23_2">
<uses-feature
android:name="android.hardware.camera2"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
Maharashtra State Board of Technical Education 159
Mobile Application Development (22617)
android:supportsRtl="true"
android:theme="@style/Theme.Exp23_2">
<activity android:name=".VideoPlayActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Maharashtra State Board of Technical Education 160


Mobile Application Development (22617)
(Space for answers)

Activity_main.xml

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


<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/a
pk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionGroup="true"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginTop="72dp"
android:text="Bluetooth_example"
android:textSize="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:text="Turn On"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:clickable="true"
android:onClick="on"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:text="ListDevices"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:onClick="list"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Maharashtra State Board of Technical Education 164
Mobile Application Development (22617)
android:layout_marginTop="54dp"
android:text="Turn Off"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:onClick="off"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="136dp"
android:layout_marginTop="40dp"
android:text="Get Visisble"

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2"
android:onClick="visible"/>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="11dp"
android:layout_marginTop="27dp"
android:text="Paired Device"
android:textColor="#ff34ff06"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.exp24;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
Maharashtra State Board of Technical Education 165
Mobile Application Development (22617)
public class MainActivity extends AppCompatActivity
{Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.button);
b2 = findViewById(R.id.button2);
b3 = findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
BA = BluetoothAdapter.getDefaultAdapter();
lv = findViewById(R.id.listView);
}
public void on(View
v){if(!BA.isEnabled()
){
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn,0);
Toast.makeText(getApplicationContext(),"Turned On", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(),"Already On", Toast.LENGTH_LONG).show();
}
}
public void off(View
v){BA.disable();
Toast.makeText(getApplicationContext(),"Turned Off", Toast.LENGTH_LONG).show();
}
public void visible(View v){
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible,0);
}
public void list(View
v){ pairedDevices =
BA.getBondedDevices();ArrayList list =
new ArrayList();
for(BluetoothDevicebt : pairedDevices) list.add(bt.getName());
Toast.makeText(getApplicationContext(),"Show Paired Devices",
Toast.LENGTH_LONG).show();
final ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1,list);
lv.setAdapter(adapter);
}
}

Android_Manifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp24">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Maharashtra State Board of Technical Education 166
Mobile Application Development (22617)
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Exp24">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>
</manifest>

XI. References / Suggestions for further Reading


1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com

XII. Assessment Scheme

Performance indicators Weightage


Process related (10 Marks) 30%
1. Logic Formation 10%
2. Debugging ability 15%
3. Follow ethical practices 5%
Product related (15 Marks) 70%
4. Interactive GUI 20%
5. Answer to Practical related questions 20%
6. Expected Output 20%
7. Timely Submission 10%
Total (25 Marks) 100%

List of student Team Members

1 Gaurang Jadhav
2 Shweta Gupta
3 Heet Zatakia

Maharashtra State Board of Technical Education 167


X. Exercise
Note: Faculty must ensure that every group of students use different input
value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to turn on, get visible, list devices and turn off
Bluetooth with the help of following GUI.

(Space for answers)


Ans 1) acitvity_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"
android:transitionGroup="true"

android:id="@+id/relativeLayout">

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Bluetooth Example"
android:textSize="35dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="28dp"
android:layout_toStartOf="@+id/imageView"
android:layout_toLeftOf="@+id/imageView"
android:clickable="true"
android:onClick="on"
android:text="Turn On"
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_below="@+id/textview"
android:layout_alignBottom="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginStart="144dp"
android:layout_marginLeft="144dp"
android:layout_marginTop="24dp"
android:onClick="visible"

android:text="Get visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="136dp"
android:layout_marginLeft="136dp"
android:layout_marginTop="28dp"
android:onClick="list"
android:text="List devices"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textview" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="28dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="off"
android:text="turn off"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textview" />

<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/button"
android:layout_alignLeft="@+id/button"
android:layout_alignParentBottom="true"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:layout_alignStart="@+id/listView"
android:layout_alignLeft="@+id/listView"

android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="Paired devices:"
android:textColor="#ff34ff06"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.exp24;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends Activity {
Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;

ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
BA = BluetoothAdapter.getDefaultAdapter();

lv = (ListView)findViewById(R.id.listView);
}
public void on(View v){
if (!BA.isEnabled()) {
Intent turnOn = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), "Turned
on",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Already on",
Toast.LENGTH_LONG).show();
}
}
public void off(View v){
BA.disable();
Toast.makeText(getApplicationContext(), "Turned off"
,Toast.LENGTH_LONG).show();
}
public void visible(View v){
Intent getVisible = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
public void list(View v){
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Showing Paired

Devices",Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new
ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.exp24">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Exp24">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
X. Exercise
Note: Faculty must ensure that every group of students use different input values.
(Use blank space for answers or attach more pages if needed)
1. Write a program to insert data in SQLite database using AsyncTask

(Space for answers)

Ans1) 1. activity_main.xml

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


<androidx.constraintlayout.widget.Constra android:id="@+id/buttonNormalInsert"
intLayout
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.co
m/apk/res/android" android:layout_height="wrap_content"

xmlns:app="http://schemas.android.com/a android:layout_below="@+id/editTextRec
pk/res-auto" ordNum"

xmlns:tools="http://schemas.android.com/t android:layout_alignLeft="@+id/editText
ools" RecordNum"
android:layout_width="match_parent" android:layout_marginStart="44dp"
android:layout_height="match_parent" android:layout_marginLeft="44dp"
tools:context=".MainActivity"> android:layout_marginTop="44dp"
<EditText
android:fontFamily="@font/poppins_medi
android:id="@+id/editTextRecordNum" um"
android:layout_width="318dp" android:text="Normal Insert"
android:layout_height="50dp"
app:layout_constraintStart_toStartOf="par
android:layout_alignParentLeft="true" ent"

android:layout_alignParentTop="true" app:layout_constraintTop_toBottomOf="
android:layout_marginStart="44dp" @+id/editTextRecordNum" />
android:layout_marginLeft="44dp" <Button
android:layout_marginTop="124dp" android:id="@+id/buttonFastInsert"
android:ems="10"
android:layout_width="wrap_content"
android:fontFamily="@font/poppins_medi
um" android:layout_height="wrap_content"
android:inputType="number"
android:singleLine="true" android:layout_alignBaseline="@+id/butt
android:textColor="#FFFFFF" onNormalInsert"

app:layout_constraintStart_toStartOf="par android:layout_alignBottom="@+id/butto
ent" nNormalInsert"
android:layout_marginTop="44dp"
app:layout_constraintTop_toTopOf="pare android:layout_marginEnd="48dp"
nt" android:layout_marginRight="48dp"
tools:ignore="MissingConstraints" />
<Button android:layout_toRightOf="@+id/buttonN
ormalInsert" android:layout_alignLeft="@+id/buttonNo
rmalInsert"
android:fontFamily="@font/poppins_medi android:layout_marginStart="16dp"
um" android:layout_marginLeft="16dp"
android:text="Fast Insert" android:layout_marginTop="36dp"

app:layout_constraintEnd_toEndOf="pare android:fontFamily="@font/poppins_medi
nt" um"
android:padding="10dp"
app:layout_constraintTop_toBottomOf=" android:text="Status"
@+id/editTextRecordNum" /> android:textColor="#FFFFFF"
<TextView android:textSize="18sp"
android:id="@+id/textViewStatus"
app:layout_constraintStart_toStartOf="par
android:layout_width="wrap_content" ent"

android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="
@+id/buttonNormalInsert" />
android:layout_below="@+id/buttonNorm </androidx.constraintlayout.widget.Constr
alInsert" aintLayout>

2. MainActivity.java

package com.example.coexp26; case R.id.buttonFastInsert:


new
import android.os.AsyncTask; AsyncInsertData("fast").execute();
import android.os.Bundle; break;
import android.app.Activity; }
import android.view.View; }
import android.widget.EditText; };
import android.widget.TextView; // EditText for entering desired
public class MainActivity extends Activity number of records to be inserted
{ editTextRecordNum = (EditText)
findViewById(R.id.editTextRecordNum);
final String TAG = "MainActivity.java"; // Button for normal and fast insert
EditText editTextRecordNum;
TextView tvStatus; findViewById(R.id.buttonNormalInsert).s
etOnClickListener(handler);
@Override
protected void onCreate(Bundle findViewById(R.id.buttonFastInsert).setO
savedInstanceState) { nClickListener(handler);
super.onCreate(savedInstanceState); // status TextView
tvStatus = (TextView)
setContentView(R.layout.activity_main); findViewById(R.id.textViewStatus);
View.OnClickListener handler = new }
View.OnClickListener() { // we used AsyncTask so it won't block
public void onClick(View v) { the UI thread during inserts.
switch (v.getId()) { class AsyncInsertData extends
case R.id.buttonNormalInsert: AsyncTask<String, String, String> {
new DatabaseHandler databaseHandler;
AsyncInsertData("normal").execute(); String type;
break;
long timeElapsed; long lStartTime =
protected AsyncInsertData(String System.nanoTime();
type){ if (type.equals("normal")) {
this.type = type;
this.databaseHandler = new databaseHandler.insertNormal(insertCount
DatabaseHandler(MainActivity.this); );
} } else {
// @type - can be 'normal' or 'fast'
@Override databaseHandler.insertFast(insertCount);
protected void onPreExecute() { }
super.onPreExecute(); // execution finished
tvStatus.setText("Inserting " + long lEndTime =
editTextRecordNum.getText() + " System.nanoTime();
records..."); // display execution time
} timeElapsed = lEndTime -
@Override lStartTime;
protected String } catch (Exception e) {
doInBackground(String... aurl) { e.printStackTrace();
try { }
// get number of records to be return null;
inserted }
int insertCount = protected void onPostExecute(String
Integer.parseInt(editTextRecordNum.getT unused) {
ext().toString()); tvStatus.setText("Done inserting "
+ databaseHandler.countRecords() + "
// empty the table records. Time elapsed: " + timeElapsed /
1000000 + " ms.");
databaseHandler.deleteRecords(); }}}
// keep track of execution time

3. DatabaseHandler.java

package com.example.coexp26; "DatabaseHandler.java";


// database version
import android.content.ContentValues; private static final int
import android.content.Context; DATABASE_VERSION = 7;
import android.database.Cursor; // database name
import protected static final String
android.database.sqlite.SQLiteDatabase; DATABASE_NAME = "NinjaDatabase2";
import // table details
android.database.sqlite.SQLiteOpenHelper public String tableName = "locations";
; public String fieldObjectId = "id";
import public String fieldObjectName =
android.database.sqlite.SQLiteStatement; "name";
import android.util.Log; public String fieldObjectDescription =
public class DatabaseHandler extends "description";
SQLiteOpenHelper { // constructor
// for our logs
public static final String TAG = public DatabaseHandler(Context
context) { }
super(context, DATABASE_NAME, db.setTransactionSuccessful();
null, DATABASE_VERSION);
} db.endTransaction();
db.close();
// creating table
}
@Override
// inserts the record without using
public void onCreate(SQLiteDatabase
db) { transaction and prepare statement
String sql = "";
sql += "CREATE TABLE " + public void insertNormal(int
tableName; insertCount){
sql += " ( "; try{
sql += fieldObjectId + " INTEGER SQLiteDatabase db =
this.getWritableDatabase();
PRIMARY KEY AUTOINCREMENT, ";
sql += fieldObjectName + " TEXT, "; for(int x=1; x<=insertCount; x++){
sql += fieldObjectDescription + " ContentValues values = new
TEXT "; ContentValues();
values.put(fieldObjectName,
sql += " ) ";
db.execSQL(sql); "Name # " + x);
String INDEX = "CREATE UNIQUE
values.put(fieldObjectDescription,
INDEX locations_index ON "
+ tableName + " (name, "Description # " + x);
description)"; db.insert(tableName, null,
db.execSQL(INDEX); values);
} }
// When upgrading the database, it will db.close();
drop the current table and recreate. }catch(Exception e){
@Override e.printStackTrace();
public void onUpgrade(SQLiteDatabase }}
db, int oldVersion, int newVersion) { // deletes all records
String sql = "DROP TABLE IF public void deleteRecords(){
EXISTS " + tableName; SQLiteDatabase db =
db.execSQL(sql); this.getWritableDatabase();
onCreate(db); db.execSQL("delete from "+
} tableName);
// insert data using transaction and db.close();
prepared statement }
public void insertFast(int insertCount) { // count records
// you can use INSERT only public int countRecords(){
String sql = "INSERT OR REPLACE SQLiteDatabase db =
INTO " + tableName + " ( name, this.getWritableDatabase();
description ) VALUES ( ?, ? )"; Cursor cursor =
SQLiteDatabase db = db.rawQuery("SELECT count(*) from " +
this.getWritableDatabase(); tableName, null);
db.beginTransactionNonExclusive(); cursor.moveToFirst();
// db.beginTransaction(); int recCount = cursor.getInt(0);
SQLiteStatement stmt = cursor.close();
db.compileStatement(sql); db.close();
for(int x=1; x<=insertCount; x++){ return recCount;
stmt.bindString(1, "Name # " + x); }}
stmt.bindString(2, "Description # "
+ x);
stmt.execute();
stmt.clearBindings();
3. Add EditText for postal address.
a. Name it "address".
b. Set text to "".

c. Set a hint to "Postal address".


d. This one should have 3 constraints; one linked to the bottom of "Last name",
second to left border and third to the right border.
4. Add EditText for email.
a. Name it "email".
b. Set text to "".
c. Set a hint to "Email".
d. This one should have 3 constraints; one linked to the bottom of "Postal
address", second to left border and third to the right border.
5. Add a Button for registration.
a. Name it "registration".
b. Set text to "Registration".

Ans 3) EditText is used for user input. TextView is used to display text and is not
editable by the user. TextView is the widget used when you want the user to View
the Text (such as a label, etc) and EditText used when you want the user to be able to
edit the text.

X. Exercise
Note: Faculty must ensure that every group of students use different input
values.
(Use blank space for answers or attach more pages if needed)
1. Write a program to create the login form with necessary validations like length of
username and password, empty text fields, count of unsuccessful login attempts.
Display the login successful/Unsuccessful toastmessage.

(Space for answers)


Ans1) 1. activity_main.xml

<?xml version="1.0" encoding="utf-8"?> xmlns:tools="http://schemas.android.com/t


<RelativeLayout ools"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.co android:layout_height="match_parent"
m/apk/res/android" tools:context=".MainActivity">
<EditText
xmlns:app="http://schemas.android.com/a android:id="@+id/User_password"
pk/res-auto"
android:layout_width="wrap_content"
android:textColor="#FFFFFF"
android:layout_height="wrap_content" android:textSize="40dp" />
<Button
android:layout_below="@+id/User_email" android:id="@+id/User_login"
android:layout_width="150dp"
android:layout_alignParentStart="true"
android:layout_height="wrap_content"

android:layout_alignParentLeft="true" android:layout_below="@+id/User_passw
ord"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_alignParentRight="true" android:layout_marginTop="25dp"
android:layout_marginStart="49dp" android:text="Login" />
android:layout_marginLeft="49dp" <EditText
android:layout_marginTop="25dp" android:id="@+id/User_email"
android:layout_marginEnd="53dp" android:layout_width="310dp"
android:layout_marginRight="53dp"
android:ems="10" android:layout_height="wrap_content"
android:hint="Password:"
android:inputType="textPassword" android:layout_below="@+id/User_Text"
android:padding="10dp" />
<TextView
android:id="@+id/User_Text" android:layout_alignParentStart="true"

android:layout_width="wrap_content" android:layout_alignParentLeft="true"

android:layout_height="wrap_content" android:layout_alignParentEnd="true"

android:layout_alignParentTop="true" android:layout_alignParentRight="true"
android:layout_marginStart="49dp"
android:layout_alignParentEnd="true" android:layout_marginLeft="49dp"
android:layout_marginTop="80dp"
android:layout_alignParentRight="true" android:layout_marginEnd="52dp"
android:layout_marginTop="113dp" android:layout_marginRight="52dp"
android:layout_marginEnd="106dp" android:hint="Email:"
android:layout_marginRight="106dp"
android:inputType="textEmailAddress"
android:fontFamily="@font/poppins_semi android:padding="10dp" />
bold" </RelativeLayout>
android:text="User Login"
2. MainActivity.java

package com.example.coexp28; Uemail.getText().toString().trim();


import Upassword =
androidx.appcompat.app.AppCompatActiv Upass.getText().toString().trim();
ity; if(Uuseremail.isEmpty()) {
import android.content.Intent; Uemail.setError("Enter an e-mail,it
import is blank");
android.icu.text.UnicodeSetSpanner; Uemail.requestFocus();
import android.os.Bundle; return;
import android.util.Patterns; }
import android.view.View;
import android.widget.Button; if(!Patterns.EMAIL_ADDRESS.matcher(U
useremail).matches()){
import android.widget.EditText;
Uemail.setError("Enter an e-
import android.widget.Toast;
mail");
public class MainActivity extends
Uemail.requestFocus();
AppCompatActivity {
Button login; return;
EditText Uemail,Upass; }
public String Uuseremail,Upassword; if(Upassword.isEmpty()) {
@Override Upass.setError("Enter a password ,
protected void onCreate(Bundle it is blank");
savedInstanceState) { Upass.requestFocus();
super.onCreate(savedInstanceState); return;
}
setContentView(R.layout.activity_main); if(Upassword.length()<6){
login = Upass.setError("Minimum length is
findViewById(R.id.User_login); 6 characters");
login.setOnClickListener(new Upass.requestFocus();
View.OnClickListener() { return;
@Override }
public void onClick(View v) {
check(); if(Uuseremail.equals("[email protected]
}}); om") && Upassword.equals("heetzatakia
@10")){
Uemail = Toast.makeText(MainActivity.this,"Welco
findViewById(R.id.User_email); me User",Toast.LENGTH_LONG).show();
Upass = Intent intent = new
findViewById(R.id.User_password); Intent(MainActivity.this,MainActivity2.cla
}
ss);
public void check(){
startActivity(intent);
Uuseremail =
}
else{ ;
Toast.makeText(this,"Wrong email }
or }
password",Toast.LENGTH_LONG).show() }

3. activity_main2.xml

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


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
</androidx.constraintlayout.widget.ConstraintLayout>

5. MainActivity2.java

package com.example.coexp28;

import androidx.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);
}}

5. AndroidMainfest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.coexp28">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.COEXP28">
<activity android:name=".MainActivity2"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>
X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to send and receive SMS, make use of the following GUI.

(Space for answers)

Ans1) 1. activity_main.xml

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


<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.co android:orientation="vertical"
m/apk/res/android" android:padding="16dp"
android:gravity="center_horizontal"
xmlns:app="http://schemas.android.com/a tools:context=".MainActivity">
pk/res-auto"
<EditText
xmlns:tools="http://schemas.android.com/t android:id="@+id/et_phone"
android:padding="12dp"
android:layout_width="match_parent" android:minLines="6"
android:inputType="textMultiLine"
android:layout_height="wrap_content" android:gravity="top"
android:hint="Enter Phone Number" android:layout_marginTop="8dp"
android:padding="12dp"
android:maxLength="10" android:background="@color/cardview_s
android:inputType="phone" hadow_start_color"/>

android:background="@color/cardview_s <Button
hadow_start_color"/> android:id="@+id/bt_send"

<EditText android:layout_width="wrap_content"
android:id="@+id/et_message"
android:layout_height="wrap_content"
android:layout_width="match_parent" android:text="Send SMS"
android:layout_marginTop="32dp"/>
android:layout_height="wrap_content"
android:hint="Enter Message" </LinearLayout>

2. MainActivity.java

package com.example.coexp29;
import androidx.annotation.NonNull; EditText etPhone, etMessage;
import Button btSend;
androidx.appcompat.app.AppCompatActiv
ity; @Override
import androidx.core.app.ActivityCompat; protected void onCreate(Bundle
import savedInstanceState) {
androidx.core.content.ContextCompat; super.onCreate(savedInstanceState);

import android.Manifest; setContentView(R.layout.activity_main);


import android.content.pm.ActivityInfo;
import etPhone =
android.content.pm.PackageManager; findViewById(R.id.et_phone);
import android.os.Bundle; etMessage =
findViewById(R.id.et_message);
import android.telephony.SmsManager; btSend =
import android.view.View;
findViewById(R.id.bt_send);
import android.widget.Button;
import android.widget.EditText; btSend.setOnClickListener(new
import android.widget.Toast; View.OnClickListener() {
@Override
public class MainActivity extends public void onClick(View view) {
AppCompatActivity {
if Toast.LENGTH_LONG).show();
(ContextCompat.checkSelfPermission(Mai }else{
nActivity.this
, Toast.makeText(getApplicationContext(),"
Enter value first.",
Manifest.permission.SEND_SMS)
Toast.LENGTH_SHORT).show();
== }
PackageManager.PERMISSION_GRANTE
D){ }

sendMessage(); @Override
}else{ public void
onRequestPermissionsResult(int
ActivityCompat.requestPermissions(Main requestCode, @NonNull
Activity.this, new @org.jetbrains.annotations.NotNull
String[]{Manifest.permission.SEND_SMS
String[] permissions, @NonNull
}, 100);
@org.jetbrains.annotations.NotNull int[]
}
grantResults) {
}
});
super.onRequestPermissionsResult(request
}
Code, permissions, grantResults);
private void sendMessage() {
if (requestCode == 100 &&
String sPhone =
grantResults.length > 0 &&
etPhone.getText().toString().trim();
grantResults[0]
String sMessage =
==
etMessage.getText().toString().trim();
PackageManager.PERMISSION_GRANTE
D){
if (!sPhone.equals("") &&
!sMessage.equals("")){ sendMessage();
SmsManager smsManager = }else{
SmsManager.getDefault();
Toast.makeText(getApplicationContext(),
smsManager.sendTextMessage(sPhone, "Permission Denied!",
null, sMessage, null, null); Toast.LENGTH_SHORT).show();
}
}
Toast.makeText(getApplicationContext(), }
"SMS sent successfully!",
3. AndroidManifest.xml

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


<manifest
xmlns:android="http://schemas.android.com/ap
k/res/android"
package="com.example.coexp29">
<uses-permission
android:name="android.permission.SEND_SM
S"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_ro
und"
android:supportsRtl="true"

android:theme="@style/Theme.COEXP29">
<activity android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />

<category
android:name="android.intent.category.LAUN
CHER" />
</intent-filter>
</activity>
</application>

</manifest>
XI. References / Suggestions for further Reading
1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com
X. Exercise
Note: Faculty must ensure that every group of students use different input
value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to send email.

(Space for answers)

Ans1) 1. Activity_main.xml

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="To:"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

<EditText
android:id="@+id/edit_text_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subject:"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

<EditText
android:id="@+id/edit_text_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailSubject" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message:"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

<EditText
android:id="@+id/edit_text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|top"
android:lines="10" />

<Button
android:id="@+id/button_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send" />

</LinearLayout>

2. MainActivity.java

package com.example.coexp30;
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;

public class MainActivity extends AppCompatActivity {


private EditText mEditTextTo,mEditTextSubject,mEditTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mEditTextTo = findViewById(R.id.edit_text_to);
mEditTextSubject = findViewById(R.id.edit_text_subject);
mEditTextMessage = findViewById(R.id.edit_text_message);

Button buttonSend = findViewById(R.id.button_send);


buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMail();
}
});
}
private void sendMail() {
String recipientList = mEditTextTo.getText().toString();
String[] recipients = recipientList.split(",");
String subject = mEditTextSubject.getText().toString();
String message = mEditTextMessage.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose an email client"));
}
}
X. Exercise
Note: Faculty must ensure that every group of students use different input values.
(Use blank space for answers or attach more pages if needed)
1. Write a program to locate the user's current location.

(Space for answers)

Ans1) 1. 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">

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/google_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
</RelativeLayout>

2. build.gradle (:app)

plugins { buildTypes {
id 'com.android.application' release {
} minifyEnabled false
proguardFiles
android { getDefaultProguardFile('proguard-android-
compileSdkVersion 30 optimize.txt'), 'proguard-rules.pro'
buildToolsVersion "30.0.3" }
}
defaultConfig { compileOptions {
applicationId "com.example.coexp31" sourceCompatibility
minSdkVersion 17 JavaVersion.VERSION_1_8
targetSdkVersion 30 targetCompatibility
JavaVersion.VERSION_1_8
versionCode 1 }
versionName "1.0" }

testInstrumentationRunner dependencies {
"androidx.test.runner.AndroidJUnitRunner"
} implementation
'androidx.appcompat:appcompat:1.3.0' implementation
implementation 'com.google.android.gms:play-services-
'com.google.android.material:material:1.3.0' location:18.0.0'
implementation testImplementation 'junit:junit:4.+'
'androidx.constraintlayout:constraintlayout:2.0. androidTestImplementation
4' 'androidx.test.ext:junit:1.1.2'
implementation androidTestImplementation
'com.google.android.gms:play-services- 'androidx.test.espresso:espresso-core:3.3.0'
maps:17.0.1' }

3. strings.xml

<resources>
<string name="app_name">COEXP31</string>
<string name="map_key"
translatable="false">AIzaSyB_qsP8AOP_P0MdlPz-48TDaJYjTP3vbjo</string>
</resources>

4. MainActivity.java

package com.example.coexp31; ces;


import
import com.google.android.gms.maps.CameraUpdateF
androidx.appcompat.app.AppCompatActivity; actory;
import
import android.os.Bundle; com.google.android.gms.maps.GoogleMap;
import androidx.annotation.NonNull; import
import com.google.android.gms.maps.OnMapReadyCa
androidx.appcompat.app.AppCompatActivity; llback;
import androidx.core.app.ActivityCompat; import
import com.google.android.gms.maps.SupportMapFra
androidx.fragment.app.FragmentActivity; gment;
import
import android.Manifest; com.google.android.gms.maps.model.LatLng;
import android.content.pm.PackageManager; import
import android.location.Location; com.google.android.gms.maps.model.MarkerO
import android.os.Bundle; ptions;
import android.widget.Toast; import
com.google.android.gms.tasks.OnSuccessListen
er;
import import com.google.android.gms.tasks.Task;
com.google.android.gms.location.FusedLocatio
nProviderClient; public class MainActivity extends
import FragmentActivity implements
com.google.android.gms.location.LocationServi OnMapReadyCallback {
tLocation.getLatitude()+""+currentLocation.get
Location currentLocation; Longitude(),Toast.LENGTH_SHORT).show();
FusedLocationProviderClient SupportMapFragment
fusedLocationProviderClient; supportMapFragment=(SupportMapFragment)
getSupportFragmentManager().findFragmentBy
private static final int REQUEST_CODE =
101; Id(R.id.google_map);
// SupportMapFragment
@Override supportMapFragment = (SupportMapFragment)
protected void onCreate(Bundle getSupportFragmentManager().findFragmentBy
savedInstanceState) { Id(R.id.google_map);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); supportMapFragment.getMapAsync(MainActiv
ity.this);
}
fusedLocationProviderClient = }
LocationServices.getFusedLocationProviderCli });
ent(this);//CLIENT REGISTER
fetchLastLocation(); }
}
@Override
private void fetchLastLocation() { public void onMapReady(GoogleMap
if googleMap) {
(ActivityCompat.checkSelfPermission(this, LatLng latLng = new
Manifest.permission.ACCESS_FINE_LOCATI LatLng(currentLocation.getLatitude(),currentLo
ON) != cation.getLongitude());
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, MarkerOptions markerOptions = new
Manifest.permission.ACCESS_COARSE_LOCA MarkerOptions().position(latLng).title("I am
TION) !=
here");
PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this, new googleMap.animateCamera(CameraUpdateFact


String[]{Manifest.permission.ACCESS_FINE_ ory.newLatLng(latLng));
LOCATION}, REQUEST_CODE);
return; googleMap.animateCamera(CameraUpdateFact
ory.newLatLngZoom(latLng,5));
googleMap.addMarker(markerOptions);
}
}
Task<Location> task = @Override
fusedLocationProviderClient.getLastLocation(); public void onRequestPermissionsResult(int
task.addOnSuccessListener(new requestCode, @NonNull String[] permissions,
OnSuccessListener<Location>() { @NonNull int[] grantResults) {
@Override switch (requestCode){
public void onSuccess(Location case REQUEST_CODE:
location) { if(grantResults.length>0 &&
if (location != null){ grantResults[0] ==
PackageManager.PERMISSION_GRANTED){
currentLocation = location; fetchLastLocation();
Toast.makeText(getApplicationContext(),curren }
break; }
} }

5. AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.coexp31">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.COEXP31">

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/map_key"/>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>
ianistan Islamabad JAMMU AND
KASHMIR

Lahore,°

°UNJA9
Pakistan UTTARAKHAND
HAR’7ANA

New DeIN UTTAR


’ *RADESH
RAJASTHAN
Jaipur
Karach›

GUJARA+ Ahmedabad
India

Ur

Mumbai / AHARASHTFA

*ELANGANA
Hyderabad

ANOHRA
KAPNATAkA PRADESD

Bengaluru

KEPALA

Sri Lan

19.043085172.8572711

Google Google
Ans1) 1. 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"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_source"
android:layout_width="317dp"
android:layout_height="56dp"
android:layout_marginTop="220dp"
android:background="@android:drawable/editbox_background"
android:fontFamily="@font/poppins_medium"
android:hint="Enter Source Location"
android:padding="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_destination"
android:layout_width="317dp"
android:layout_height="52dp"
android:layout_marginTop="36dp"
android:background="@android:drawable/editbox_background"
android:fontFamily="@font/poppins_medium"
android:hint="Enter Destination Location"
android:padding="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_source" />
<Button
android:id="@+id/bt_track"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:fontFamily="@font/poppins_medium"
android:text="Display Location"
app:backgroundTint="#3F51B5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_destination" />
</androidx.constraintlayout.widget.ConstraintLayout>

2. MainActivity.java

package com.example.coexp32;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText etSource,etDestination;
Button btTrack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etSource = findViewById(R.id.et_source);
etDestination = findViewById(R.id.et_destination);
btTrack = findViewById(R.id.bt_track);
btTrack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sSource = etSource.getText().toString().trim();
String sDestination = etDestination.getText().toString().trim();
if(sSource.equals("") && sDestination.equals("")){
Toast.makeText(MainActivity.this, "Enter both location",
Toast.LENGTH_SHORT).show();
}else{
DisplayTrack(sSource,sDestination); }}}); }
private void DisplayTrack(String sSource, String sDestination) {
try{

Uri uri = Uri.parse("https://www.google.co.in/maps/dir/" + sSource + "/"


+ sDestination);
Intent intent= new Intent(Intent.ACTION_VIEW,uri);
intent.setPackage("com.google.android.apps.maps");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}catch (ActivityNotFoundException e){
Uri uri =
Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.maps");
Intent intent= new Intent(Intent.ACTION_VIEW,uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}}}

You might also like