0% found this document useful (0 votes)
17 views78 pages

6 Sem MP Lab Report 1

Uploaded by

jrabhi300
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)
17 views78 pages

6 Sem MP Lab Report 1

Uploaded by

jrabhi300
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/ 78

CACS 351 Mobile Programming

Patan Multiple Campus


Patan Dhoka, Lalitpur

Lab Report
CACS 351 Mobile Programming

Submitted by
Ghanashyam Dhungana

Ghanashyam Dhungana
CACS 351 Mobile Programming

CACS 351 Mobile Programming

Tribhuvan University
Faculty of Humanities and Social Sciences
Bachelors in Computer Application

Submitted to
Bhupendra Luhar
Department of Computer Application
Patan Multiple Campus
Patan Dhoka, Lalitpur

Submitted by
GHANASHYAM DHUNGANA
BCA 6th Semester
Exam Roll Number:- 220119
Date :- 2079/02/13
CACS 351 Mobile Programming

ACKNOWLEDGEMENT

We express our deep appreciation to our Lecturer Mr. Bhupendra Luhar who gave us the
golden opportunity for doing this wonderful lab Report as well as helped us a lot in
finalizing this Report within the limited time frame.

We would like to express our special thanks of gratitude to our Campus Chief Dr. Laxman
Singh Kunwar who gave us permission for doing this wonderful lab Report. We would also
like to express our hearty gratitude to all those people who directly and indirectly helped us
to prepare this Report.

In spite of our herculean efforts, there might remain some errors in this Report. So, we
welcome critical feedback on this Report and look forward to constructive suggestions for
improving the Report. We assure you that your invaluable comments and suggestions find
their place in upcoming Reports.

Yours sincerely,
Ghanashyam Dhungana

Ghanashyam Dhungana
CACS 351 Mobile Programming

Preface
Computer and telecommunication technology that combine together has change
three way we talk, word, work or deal. The world has been transformed into a small global
village, where everyone on any comer is within the reach of each other. Information and
communication technology (ICT) has emerged as the key ingredient of every activity.

Today Mobile Programming are an essential parts of any computer system.


Computer science education is undergoing rapid change, as computer are now prevalent in
virtually every area of day by day life form embedded devices in automobiles through the
most sophisticated planning tools for different firms.

I have taken efforts in this lab Report. However, it would not been have been
possible without the kind supports of our Lecturer Mr. Bhupendra Luhar. I would like to
extend my sincere thanks to him.

I am highly indebted to Patan Multiple Campus for their guidance and constant
supervision as well as for providing necessary information regarding the Report and support
in the completion.

I would like to express my gratitude towards library and member of Patan Multiple
Campus for their kind co-operation and encouragement which help me in completion of this
Report

I would like to express my special gratitude and thanks to our Co-ordinator Sir Mr.
Bhoj Raj Joshi for his support and help for our personnel development and mainly for the
completion of this reports work of our Mobile Programming.
CACS 351 Mobile Programming

kmf]g g+= Phone No.


lqe'jg ljZjljBfno %–@^)%!) 5-260510
%–@^)@($ 5-260294
TRIBHUVAN UNIVERSITY %–%@@)&^ 5-522076

td;f]df Hof]ltu{do
kf6g ;+o'Qm SofDk; %–@^)(!! 5-260911

Patan Multiple Campus


kf6g 9f]sf, nlntk'/, g]kfn
Department of Computer Application Patan Dhoka, Lalitpur, Nepal
ldlt / Date : 2079/02/13

Subject : To whom it may Concern.

This is certify that Mr. Ghanashyam Dhungana is a regular student of


BCA 6th semester. He was regular in his Practical class and capable of
attending his Practical exam on the Partial fulfillment of his Tribhuvan
University Practical Exam 2079.
According to Tribhuvan University his symbol number is ...............
and registration number is 6-2-22-324-2018.

…………………………. ……………………
Bhoj Raj Joshi Bhupendra Luhar
Co-ordinator Subject Teacher
BCA Program Faculty Member
CACS 351 Mobile Programming

Table of Contant
1. Design login form using Relative, Absolute and Constraints layout. ..................................................1

2. Design following form using Table layout...........................................................................................3

3. Design interface for calculator having two Edit Text for input number, one Button for addition and
one Text View for displaying result. ....................................................................................................6

4. Write Develop a simple calculator application with two input fields for inputting numbers and two
buttons for performing multiplication and division and display result in Text view ...........................8

5. Develop an android application to calculate area and perimeter of Rectangle, your application
should contain fields to input length and breadth and two buttons for calculating area and perimeter.
Display result in a text View. .............................................................................................................11

6. Develop an android application to input your name, address, gender, hobbies, country and other
personal information and display this message in toast message. ......................................................14

7. Develop android application to illustrate activity life cycle. ..............................................................19

8. Develop an android application to calculate square and cube of any number and display result in
another activity. ..................................................................................................................................21

9. Develop an android application which get result back from child activity. .......................................24

10. Develop android application to add two fragment in activity in statically and dynamically. ..........27

a. Statically .....................................................................................................................................27
b. Dynamically .............................................................................................................................30
11. Develop an android application to create following type of menu. ..................................................33

a. Option menu ...............................................................................................................................33


b. Context menu ...........................................................................................................................35
c. Popup menu ................................................................................................................................37
12. Develop an android application to compute simple interest using custom dialog. ...........................39

13. Develop an android application to show list of 8 programming language name using list view. ....42

14. Develop an android application to show image, name and address of six employee using: ............44

a. List View.....................................................................................................................................44
b. Grid view .................................................................................................................................47
c. Recycle view ...............................................................................................................................51
15. Provided that SQLite database named university with table named “student” with following
column1 (Roll as integer, Name as text, address as text, and department as text, develop an android
application to connect database and perform following database operations. ...................................55
CACS 351 Mobile Programming

a. Insert five student records ...........................................................................................................55


b. Display student information .......................................................................................................55
c. Update student information where roll no. = 103 ......................................................................55
d. Delete student information who live in patan. ...........................................................................55
16. Develop an android application that demonstrate retrieval of contents from remote server. ...........63

17. Develop an android application that demonstrates sending of contents to remote server. ...............67

18. Develop an android application that display Google map. ...............................................................70


Mobile Programming (CACS 351)

1. Design login form using Relative, Absolute and Constraints layout.


//lab1a_relative.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:textSize="20sp"
android:layout_margin="10dp"
android:id="@+id/name"/>
<EditText
android:layout_toRightOf="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Username"
android:textSize="20sp"
android:id="@+id/uname"/>
<TextView
android:layout_below="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="20sp"
android:layout_margin="20dp"
android:id="@+id/pass"/>
<EditText
android:layout_below="@+id/uname"
android:layout_toRightOf="@+id/pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter password"
android:textSize="20sp"
android:inputType="textPassword"
android:id="@+id/upass"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:textSize="20sp"
android:layout_margin="15dp"
android:id="@+id/login"
android:layout_below="@+id/upass"/>
</RelativeLayout>

1 Ghanashyam Dhungana
Mobile Programming (CACS 351)

//MainActivity.java
package com.example.lab;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lab1b);
}
}

Output:

2 Ghanashyam Dhungana
Mobile Programming (CACS 351)

2. Design following form using Table layout.

student roll name Address


101 Ram Ktm
102 Hari Ltp
104 Geeta Pkr

//lab1b.xml

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


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="student roll"
android:textSize="20sp"
android:layout_column="1"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="20sp"
android:layout_column="2"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="address"
android:textSize="20sp"
android:layout_column="2"
android:layout_margin="10dp"/>
</TableRow>
<TableRow android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="101"
android:textSize="20sp"
android:layout_column="1"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"

3 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_height="wrap_content"
android:text="Ram"
android:textSize="20sp"
android:layout_column="2"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ktm"
android:textSize="20sp"
android:layout_column="3"
android:layout_margin="10dp"/>
</TableRow>
<TableRow android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="102"
android:textSize="20sp"
android:layout_column="1"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hari"
android:textSize="20sp"
android:layout_column="2"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lalitpur"
android:textSize="20sp"
android:layout_column="3"
android:layout_margin="10dp"/>
</TableRow>
<TableRow android:layout_height="match_parent"
android:layout_width="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="104"
android:textSize="20sp"
android:layout_column="1"
android:layout_margin="10dp"/>
<TextView

4 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Geeta"
android:textSize="20sp"
android:layout_column="2"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pokhara"
android:textSize="20sp"
android:layout_column="3"
android:layout_margin="10dp"/>
</TableRow>
</TableLayout>

//MainActivity.java
package com.example.lab;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lab1b);
}
}
Output:

5 Ghanashyam Dhungana
Mobile Programming (CACS 351)

3. Design interface for calculator having two Edit Text for input number, one Button
for addition and one Text View for displaying result.

//lab1c.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="enter first number"
android:textSize="20sp"
android:inputType="text"
android:id="@+id/fnum"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="enter second number"
android:textSize="20sp"
android:inputType="text"
android:id="@+id/snum"
android:layout_below="@+id/fnum"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_margin="10dp"
android:textSize="20sp"
android:id="@+id/add"
android:layout_below="@+id/snum"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:layout_margin="10dp"
android:textSize="20sp"
android:id="@+id/result"
android:layout_below="@+id/add"/>
</RelativeLayout>

6 Ghanashyam Dhungana
Mobile Programming (CACS 351)

//MainActivity.java
package com.example.lab;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lab1c);
}
}

Output:

7 Ghanashyam Dhungana
Mobile Programming (CACS 351)

4. Write Develop a simple calculator application with two input fields for inputting
numbers and two buttons for performing multiplication and division and display
result in Text view.

//lab2a.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator"
android:textSize="20sp"
android:layout_margin="10dp"
android:textStyle="bold"
android:id="@+id/calculator"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter first number"
android:textSize="20sp"
android:inputType="number"
android:layout_margin="10dp"
android:id="@+id/fnum"
android:layout_below="@+id/calculator" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter second number"
android:textSize="20sp"
android:layout_margin="10dp"
android:inputType="number"
android:id="@+id/snum"
android:layout_below="@+id/fnum" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mul"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:id="@+id/mulbtn"
android:layout_below="@+id/snum" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Div"

8 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:id="@+id/divbtn"
android:layout_below="@+id/snum"
android:layout_toRightOf="@+id/mulbtn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:textSize="20sp"
android:layout_margin="10dp"
android:textStyle="bold"
android:id="@+id/res"
android:layout_below="@+id/mulbtn"/>
</RelativeLayout>

//MainActivity.java
package com.example.lab;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText t1,t2;
Button b1,b2;
TextView res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lab2a);
t1=findViewById(R.id.fnum);
t2=findViewById(R.id.snum);
b1=findViewById(R.id.mulbtn);
b2=findViewById(R.id.divbtn);
res=findViewById(R.id.res);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1= Integer.parseInt(t1.getText().toString());
int n2= Integer.parseInt(t2.getText().toString());
int mul= (n1*n2);
res.setText("Result ="+mul);
}

9 Ghanashyam Dhungana
Mobile Programming (CACS 351)

});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1= Integer.parseInt(t1.getText().toString());
int n2= Integer.parseInt(t2.getText().toString());
int div= (n1/n2);
res.setText("Result ="+div);
}
});
}
}
Output:

10 Ghanashyam Dhungana
Mobile Programming (CACS 351)

5. Develop an android application to calculate area and perimeter of Rectangle, your


application should contain fields to input length and breadth and two buttons for
calculating area and perimeter. Display result in a text View.

//lab2b.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculating area and perimeter of rectangle"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"
android:id="@+id/text"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter length of rectangle"
android:textSize="20sp"
android:layout_margin="10dp"
android:inputType="number"
android:id="@+id/length"
android:layout_below="@+id/text"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter breadth of rectangle"
android:textSize="20sp"
android:layout_margin="10dp"
android:inputType="number"
android:id="@+id/breadth"
android:layout_below="@+id/length"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Area"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:id="@+id/area"
android:layout_below="@+id/breadth"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Perimeter"

11 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:id="@+id/perimeter"
android:layout_below="@+id/breadth"
android:layout_toRightOf="@+id/area"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"
android:id="@+id/res"
android:layout_below="@+id/area"/>
</RelativeLayout>

//MainActivity.java
package com.example.lab;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText t1,t2;
Button b1,b2;
TextView res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lab2b);

t1=findViewById(R.id.length);
t2=findViewById(R.id.breadth);
b1=findViewById(R.id.area);
b2=findViewById(R.id.perimeter);
res=findViewById(R.id.res);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1= Integer.parseInt(t1.getText().toString());
int n2= Integer.parseInt(t2.getText().toString());
int area= (n1*n2);
res.setText("Result ="+area);

12 Ghanashyam Dhungana
Mobile Programming (CACS 351)

}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1= Integer.parseInt(t1.getText().toString());
int n2= Integer.parseInt(t2.getText().toString());
int perimeter= 2*(n1+n2);
res.setText("Result ="+perimeter);
}
});
}
}

Output:

13 Ghanashyam Dhungana
Mobile Programming (CACS 351)

6. Develop an android application to input your name, address, gender, hobbies,


country and other personal information and display this message in toast message.

//lab2c.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Registration Form"
android:layout_gravity="center"
android:textStyle="bold"
android:layout_margin="10dp"
android:id="@+id/reg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Name"
android:layout_margin="10dp"
android:id="@+id/name"
android:layout_below="@id/reg"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:inputType="text"
android:id="@+id/text1"
android:layout_below="@id/reg"
android:layout_toRightOf="@id/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Address"
android:layout_margin="10dp"
android:id="@+id/address"
android:layout_below="@id/name"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="20sp"
android:id="@+id/text2"

14 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_below="@id/name"
android:layout_toRightOf="@+id/address"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Email"
android:layout_margin="10dp"
android:id="@+id/email"
android:layout_below="@id/address"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="20sp"
android:id="@+id/text3"
android:layout_below="@id/address"
android:layout_toRightOf="@+id/email"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
android:layout_margin="10dp"
android:textSize="20sp"
android:id="@+id/gender"
android:layout_below="@id/email"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/email"
android:layout_toRightOf="@+id/gender">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:textStyle="normal"
android:textSize="20sp"
android:id="@+id/radio1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:textStyle="normal"
android:textSize="20sp"
android:id="@+id/radio2"/>
</RadioGroup>
<TextView

15 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hobbies"
android:textSize="20sp"
android:layout_margin="10dp"
android:id="@+id/hobby"
android:layout_below="@id/gender"/>
<CheckBox
android:id="@+id/check1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/hobby"
android:onClick="checkItem"
android:text="playing"
android:textSize="20sp"
android:textStyle="normal" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="travelling"
android:textSize="20sp"
android:textStyle="normal"
android:onClick="checkItem"
android:id="@+id/check2"
android:layout_below="@id/check1"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="reading"
android:textSize="20sp"
android:textStyle="normal"
android:onClick="checkItem"
android:id="@+id/check3"
android:layout_below="@id/check2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country"
android:textSize="20sp"
android:layout_margin="10dp"
android:id="@+id/country"
android:layout_below="@id/check3"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/country_name"
android:spinnerMode="dropdown"
android:layout_margin="10dp"

16 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_toRightOf="@+id/country"
android:layout_below="@id/check3"
android:id="@+id/spinner"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:textSize="20sp"
android:layout_gravity="center"
android:onClick="Register"
android:id="@+id/btn"
android:layout_below="@+id/country"/>
</RelativeLayout>

//MainActivity.java
package com.example.lab;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText t1, t2;
CheckBox c1, c2, c3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lab2c);
}
public void checkItem(View v) {
Toast.makeText(getApplicationContext(), "checkbox selected",
Toast.LENGTH_SHORT).show();
}
public void Register(View v) {
Toast.makeText(getApplicationContext(), "registration successful",
Toast.LENGTH_SHORT).show();
}
}

17 Ghanashyam Dhungana
Mobile Programming (CACS 351)

Output:

18 Ghanashyam Dhungana
Mobile Programming (CACS 351)

7. Develop android application to illustrate activity life cycle.

//lab3a.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=".lab3a">
</androidx.constraintlayout.widget.ConstraintLayout>

//lab3a.java
package com.example.lab3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class lab3a extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lab3a);
Log.d("Lifecycle","activitycreated");
}
@Override
protected void onStart() {
super.onStart();
Log.d("Lifecycle","activitystarted");
}
@Override
protected void onResume() {
super.onResume();
Log.d("Lifecycle","activityresume");
}
@Override
protected void onPause() {
super.onPause();
Log.d("Lifecycle","activitypause");
}
@Override
protected void onStop() {
super.onStop();
Log.d("Lifecycle","activitystop");
}
@Override
protected void onDestroy() {

19 Ghanashyam Dhungana
Mobile Programming (CACS 351)

super.onDestroy();
Log.d("Lifecycle","activitydestroy");
}
}

Output:

20 Ghanashyam Dhungana
Mobile Programming (CACS 351)

8. Develop an android application to calculate square and cube of any number and
display result in another activity.
//lab3b_first.xml

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


<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"
android:orientation="vertical"
android:gravity="center"
tools:context=".lab3b_first">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter length"
android:textSize="20sp"
android:id="@+id/num1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Square"
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/btn1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cube"
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/btn2"/>
</LinearLayout>

//lab3b_first.java

package com.example.lab3;
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 lab3b_first extends AppCompatActivity {
EditText t1,t2;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {

21 Ghanashyam Dhungana
Mobile Programming (CACS 351)

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lab3b_first);
t1 = (EditText) findViewById(R.id.num1);
t2 = (EditText) findViewById(R.id.num1);
b1 = (Button) findViewById(R.id.btn1);
b2 = (Button) findViewById(R.id.btn2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1 = Integer.parseInt(t1.getText().toString());
int square = n1 * n1;
Intent in = new Intent(getApplicationContext(), lab3b_second.class);
in.putExtra("Result", square);
startActivity(in);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n2 = Integer.parseInt(t1.getText().toString());
int cube = n2 * n2 * n2;
Intent i = new Intent(getApplicationContext(), lab3b_second.class);
i.putExtra("Result1", cube);
startActivity(i);
}
});
}
}

//lab3b_second.xml

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


<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"
android:orientation="vertical"
android:gravity="center"
tools:context=".lab3b_second">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20sp"
android:id="@+id/txt1"/>
<TextView
android:layout_width="wrap_content"

22 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20sp"
android:id="@+id/txt2"/>
</LinearLayout>

//lab3b_second.java

package com.example.lab3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class lab3b_second extends AppCompatActivity {
TextView t1,t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lab3b_second);
Intent i = getIntent();
int res = i.getIntExtra("Result", 0);
t1 = findViewById(R.id.txt1);
t1.setText("Square Result=" + res);
Intent in =getIntent();
int res1=in.getIntExtra("Result1",0);
t2=(TextView)findViewById(R.id.txt2);
t2.setText("Cube Result="+res1);
}
}

Output:

23 Ghanashyam Dhungana
Mobile Programming (CACS 351)

9. Develop an android application which get result back from child activity.

//activity_main.xml

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


<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"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is first activity"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/teal_700"
android:id="@+id/txt1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goto Child"
android:textSize="20dp"
android:textStyle="bold"
android:id="@+id/btn1"/>
</LinearLayout>

//MainActivity.java

package com.example.unit4;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button b1;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.btn1);

24 Ghanashyam Dhungana
Mobile Programming (CACS 351)

t1=findViewById(R.id.txt1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i= new Intent(MainActivity.this,Child.class);
startActivityForResult(i,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1)
{
t1.setText(data.getStringExtra("message"));
}
}
}

//activity_child.xml

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


<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"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
tools:context=".Child">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is child activity"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/teal_700"
android:id="@+id/txt1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goto parent"
android:textSize="20dp"
android:textStyle="bold"
android:id="@+id/btn2"/>
</LinearLayout>

25 Ghanashyam Dhungana
Mobile Programming (CACS 351)

//Child.java

package com.example.unit4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Child extends AppCompatActivity {
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
b=findViewById(R.id.btn2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i= new Intent();
i.putExtra("message","Hello i am from child");
setResult(1,i);
finish();
}
});
}
}

Output:

26 Ghanashyam Dhungana
Mobile Programming (CACS 351)

10. Develop android application to add two fragment in activity in statically and
dynamically.

a. Statically

//fragment_first.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"
tools:context=".FirstFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is first fragment."
android:textColor="@color/teal_700"
android:background="@color/teal_200"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>

//fragment_second.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"
tools:context=".SecondFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is the second fragment."
android:textColor="@color/teal_700"
android:background="@color/purple_200"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>

//FirstFragment.java
package com.example.unit5;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;

27 Ghanashyam Dhungana
Mobile Programming (CACS 351)

import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_first, container, false);
return v;
}
}

//SecondFragment.java
package com.example.unit5;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v1= inflater.inflate(R.layout.fragment_second, container, false);
return v1;
}
}

//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:name="com.example.unit5.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/fragment1"
/>
<fragment
android:name="com.example.unit5.SecondFragment"
android:layout_width="match_parent"

28 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/fragment2"/>
</LinearLayout>

//MainActivity.java
package com.example.unit5;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Output:

29 Ghanashyam Dhungana
Mobile Programming (CACS 351)

b. Dynamically

//fragment_first.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"
tools:context=".FirstFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is first fragment."
android:textColor="@color/teal_700"
android:background="@color/teal_200"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>

//fragent_second.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"
tools:context=".SecondFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is the second fragment."
android:textColor="@color/teal_700"
android:background="@color/purple_200"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>

//FirstFragment.java
package com.example.unit5;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,

30 Ghanashyam Dhungana
Mobile Programming (CACS 351)

Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_first, container, false);
return v;
}
}

//SecondFragment.java
package com.example.unit5;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v1= inflater.inflate(R.layout.fragment_second, container, false);
return v1;
}
}

//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:id="@+id/layout1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:id="@+id/layout2"/>
</LinearLayout>

31 Ghanashyam Dhungana
Mobile Programming (CACS 351)

//MainActivity.java

package com.example.unit5;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirstFragment f1= new FirstFragment();
SecondFragment f2=new SecondFragment();

FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.layout1,f1);
ft.replace(R.id.layout2,f2);
ft.commit();
}
}

Output:

32 Ghanashyam Dhungana
Mobile Programming (CACS 351)

11. Develop an android application to create following type of menu.


a. Option menu

//Optionmenuexample.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=".optionmenuexample">
</androidx.constraintlayout.widget.ConstraintLayout>

//Optionmenu.xml

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


<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/java"
android:title="Java"/>
<item
android:id="@+id/python"
android:title="Python"/>
<item
android:id="@+id/php"
android:title="PHP"/>
<item
android:id="@+id/kotlin"
android:title="Kotlin"/>
</menu>

//optionmenuexample.java

package com.example.unit5;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
//optionmenuexample
public class optionmenuexample extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_optionmenuexample);

33 Ghanashyam Dhungana
Mobile Programming (CACS 351)

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.optionmenu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.java:
Toast.makeText(this, "Java menu item is clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.php:
Toast.makeText(this, "PHP menu item is clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.python:
Toast.makeText(this, "Python menu item is clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.kotlin:
Toast.makeText(this, "Kotlin menu item is clicked",
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(this, "No any menu item", Toast.LENGTH_SHORT).show();
}
return true;
}
}
Output:

34 Ghanashyam Dhungana
Mobile Programming (CACS 351)

b. Context menu
//contextmenuexample.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context=".contextmenuexample">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showcontext"
android:text="Show ContextMenu"
android:textStyle="bold"
android:gravity="center" />
</LinearLayout>

//contextmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/java"
android:title="Java"/>
<item
android:id="@+id/python"
android:title="Python"/>
<item
android:id="@+id/php"
android:title="PHP"/>
<item
android:id="@+id/kotlin"
android:title="Kotlin"/>
</menu>

//contextmenuexample.java
package com.example.unit5;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class contextmenuexample extends AppCompatActivity {

35 Ghanashyam Dhungana
Mobile Programming (CACS 351)

Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contextmenuexample);
b1=findViewById(R.id.showcontext);
registerForContextMenu(b1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater= getMenuInflater();
inflater.inflate(R.menu.contextmenu,menu);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.java:
Toast.makeText(this, "Java menu item is clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.php:
Toast.makeText(this, "PHP menu item is clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.python:
Toast.makeText(this, "Python menu item is clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.kotlin:
Toast.makeText(this, "Kotlin menu item is clicked",
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(this, "No any menu item", Toast.LENGTH_SHORT).show(); }
return true; }
}
Output:

36 Ghanashyam Dhungana
Mobile Programming (CACS 351)

c. Popup menu

//popupmenuexampe.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
android:gravity="center"
tools:context=".popupmenuexample">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup"
android:textStyle="bold"
android:textColor="@color/teal_700"
android:id="@+id/popbtn"/>
</LinearLayout>
//popupmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/java"
android:title="Java"/>
<item
android:id="@+id/python"
android:title="Python"/>
<item
android:id="@+id/php"
android:title="PHP"/>
</menu>
//popupmenuexample.java
package com.example.unit5;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public class popupmenuexample extends AppCompatActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popupmenuexample);

37 Ghanashyam Dhungana
Mobile Programming (CACS 351)

b1=findViewById(R.id.popbtn);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu pm =new PopupMenu(popupmenuexample.this,b1);
pm.inflate(R.menu.popupmenu);
pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.java:
Toast.makeText(popupmenuexample.this, "Java item is clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.php:
Toast.makeText(popupmenuexample.this, "Php item is clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.python:
Toast.makeText(popupmenuexample.this, "Python item is clicked",
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(popupmenuexample.this, "Invalid item",
Toast.LENGTH_SHORT).show();
break; }
return true; }
});
pm.show(); }
});
}
}
Output:

38 Ghanashyam Dhungana
Mobile Programming (CACS 351)

12. Develop an android application to compute simple interest using custom dialog.

//activity_lab4c
<?xml version="1.0" encoding="utf-8"?>
<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=".lab4c">
<Button
android:id="@+id/dialogbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Interest"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="40dp"
android:layout_gravity="center" />
</LinearLayout>

//lab4c_second
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Principal"
android:inputType="number"
android:id="@+id/principal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Time"
android:inputType="number"
android:id="@+id/time" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Rate"
android:inputType="number"
android:id="@+id/rate"
/>
<Button
android:layout_width="wrap_content"

39 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Interest"
android:id="@+id/interest"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result:"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:id="@+id/result"/>
</LinearLayout>

//lab4c.java
package com.example.lab4;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class lab4c extends AppCompatActivity {
Button b1, binterest;
EditText t1,t2, t3;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lab4c);
b1= findViewById(R.id.dialogbtn);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder= new AlertDialog.Builder(lab4c.this);
builder.setTitle("Calculate Interest");
builder.setCancelable(true);
//inflate and set layout for the dialog
LayoutInflater inflater=getLayoutInflater();
View view =inflater.inflate(R.layout.lab4c_second,null);
builder.setView(view);
t1=view.findViewById(R.id.principal);
t2=view.findViewById(R.id.time);
t3=view.findViewById(R.id.rate);
result=view.findViewById(R.id.result);
binterest=view.findViewById(R.id.interest);

40 Ghanashyam Dhungana
Mobile Programming (CACS 351)

binterest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num1=Integer.parseInt(t1.getText().toString());
int num2=Integer.parseInt(t2.getText().toString());
int num3=Integer.parseInt(t3.getText().toString());
int interest=(num1*num2*num3)/100;
result.setText("Result="+interest);
}
});
AlertDialog alert=builder.create();
alert.show();
}
});
}
}

Output:

41 Ghanashyam Dhungana
Mobile Programming (CACS 351)

13. Develop an android application to show list of 8 programming language name using
list view.

//activity_lab5a_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".lab5a_listview"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/purple_200"
android:dividerHeight="2dp"
android:id="@+id/mylist"/>
</LinearLayout>

//lab5a_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"
android:id="@+id/listtext"/>
</LinearLayout>

//lab5a_listview.java

package com.example.lab5;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class lab5a_listview extends AppCompatActivity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

42 Ghanashyam Dhungana
Mobile Programming (CACS 351)

setContentView(R.layout.activity_lab5a_listview);
list=findViewById(R.id.mylist);
String
program[]={"C","C#","PHP","Python","Java","Kotlin","Ruby","ASP.NET"};
final ArrayAdapter adapter=new
ArrayAdapter<String>(this,R.layout.lab5a_list_item,R.id.listtext,program);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
position, long l) {
String val=(String) adapter.getItem(position);
Toast.makeText(lab5a_listview.this, "Listitem is clicked",
Toast.LENGTH_SHORT).show();
}
});
}
}

Output:

43 Ghanashyam Dhungana
Mobile Programming (CACS 351)

14. Develop an android application to show image, name and address of six employee
using:
a. List View

//activity_lab5b_acustomlistview.xml

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


<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"
android:orientation="vertical"
tools:context=".lab5b_acustomlistview">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mylist"
android:divider="@color/purple_200"
android:dividerHeight="1dp"/>
</LinearLayout>

//lab5b_acustomlistview.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img"
android:id="@+id/image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="Name"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/address"
android:textColor="@color/black"
android:layout_marginTop="10dp"

44 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_toRightOf="@id/image"
android:layout_below="@id/name"
android:text="Address"
android:textSize="18sp"
android:textStyle="bold"/>
</RelativeLayout>

//la5b_acustomlistview.java

package com.example.lab5;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class lab5b_acustomlistview extends AppCompatActivity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lab5b_acustomlistview);
list=findViewById(R.id.mylist);
String[] name={"Luhan","Xuimin","Lay","Chen","Win","Bright"};
String[] address={"China","Korea","China","Korea","Thailand","Thailand"};
int[]
image={R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.img_1,R.drawable.i
mg_1,R.drawable.img_1};
Adapter_listview adapter=new Adapter_listview(this,name,address,image);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
switch (position){
case 0:
Toast.makeText(lab5b_acustomlistview.this, "First employee is clicked",
Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(lab5b_acustomlistview.this, "Second employee is clicked",
Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(lab5b_acustomlistview.this, "Third employee is clicked",
Toast.LENGTH_SHORT).show();
break;
case 3:

45 Ghanashyam Dhungana
Mobile Programming (CACS 351)

Toast.makeText(lab5b_acustomlistview.this, "Fourth employee is clicked",


Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(lab5b_acustomlistview.this, "Fifth employee is clicked",
Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(lab5b_acustomlistview.this, "Sixth employee is clicked",
Toast.LENGTH_SHORT).show();
break;
}

}
});
}
}

//Adapter_listview.java

package com.example.lab5;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
public class Adapter_listview extends ArrayAdapter<String> {
Activity context;
String[] name;
String[] address;
int[] image;
public Adapter_listview(@NonNull Activity context, String[] name, String[]
address,int[] image) {
//super(context, R.layout.lab5b_acustomlistview, name);
super(context, R.layout.lab5b_bcustomgridview, name);
this.context=context;
this.name=name;
this.address=address;
this.image=image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= context.getLayoutInflater();
View v=inflater.inflate(R.layout.lab5b_acustomlistview,null,true);

46 Ghanashyam Dhungana
Mobile Programming (CACS 351)

TextView txtName= v.findViewById(R.id.name);


ImageView imageView= v.findViewById(R.id.image);
TextView txtAddress= v.findViewById(R.id.address);
txtName.setText(name[position]);
imageView.setImageResource(image[position]);
txtAddress.setText(address[position]);
return v;
}
}

Output:

b. Grid view
// activity_lab5b_bcustomgridview.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=".lab5b_bcustomgridview">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/customgrid"
android:numColumns="auto_fit"/>
</RelativeLayout>

47 Ghanashyam Dhungana
Mobile Programming (CACS 351)

// lab5b_bcustomgridview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:src="@drawable/img"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_below="@id/image"
android:text="Name"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/address"
android:layout_below="@id/name"
android:text="Address"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
// lab5b_bcustomgridview.java
package com.example.lab5;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class lab5b_bcustomgridview extends AppCompatActivity {
GridView grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lab5b_bcustomgridview);
grid=findViewById(R.id.customgrid);
String[] name={"Name: Luhan","Xuimin","Lay","Chen","Win","Bright"};

48 Ghanashyam Dhungana
Mobile Programming (CACS 351)

String[] address={"Address:
China","Korea","China","Korea","Thailand","Thailand"};
int[]
image={R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.img
,R.drawable.img};
Adapter_listview adapter=new Adapter_listview(this,name,address,image);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
switch (position){
case 0:
Toast.makeText(lab5b_bcustomgridview.this, "First employee is clicked",
Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(lab5b_bcustomgridview.this, "Second employee is
clicked", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(lab5b_bcustomgridview.this, "Third employee is clicked",
Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(lab5b_bcustomgridview.this, "Fourth employee is
clicked", Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(lab5b_bcustomgridview.this, "Fifth employee is clicked",
Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(lab5b_bcustomgridview.this, "Sixth employee is clicked",
Toast.LENGTH_SHORT).show();
break; } }
});
} }

// Adapter_listview.java

package com.example.lab5;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

49 Ghanashyam Dhungana
Mobile Programming (CACS 351)

import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
public class Adapter_listview extends ArrayAdapter<String> {
Activity context;
String[] name;
String[] address;
int[] image;
public Adapter_listview(@NonNull Activity context, String[] name, String[]
address,int[] image) {
//super(context, R.layout.lab5b_acustomlistview, name);
super(context, R.layout.lab5b_bcustomgridview, name);
this.context=context;
this.name=name;
this.address=address;
this.image=image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= context.getLayoutInflater();
View v=inflater.inflate(R.layout.lab5b_bcustomgridview,null,true);
TextView txtName= v.findViewById(R.id.name);
ImageView imageView= v.findViewById(R.id.image);
TextView txtAddress= v.findViewById(R.id.address);
txtName.setText(name[position]);
imageView.setImageResource(image[position]);
txtAddress.setText(address[position]);
return v;
}
}
Output:

50 Ghanashyam Dhungana
Mobile Programming (CACS 351)

c. Recycle view
// activity_lab5b_crecycleview.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=".lab5b_crecycleview">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycleview"
android:scrollbars="vertical"/>
</RelativeLayout>

// lab5b_ccustomrecycleview.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/image"
android:src="@drawable/img"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_toRightOf="@+id/image"
android:text="Name"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/address"
android:layout_below="@id/name"
android:layout_toRightOf="@+id/image"
android:text="Address"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"/>
</RelativeLayout>

51 Ghanashyam Dhungana
Mobile Programming (CACS 351)

//Recycleview_Adapter.java

package com.example.lab5;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class Recycleview_Adapter extends
RecyclerView.Adapter<Recycleview_Adapter.ViewHolder> {
Activity context;
int [] image;
String [] name;
String [] address;
public Recycleview_Adapter(Activity context,String[]name,String[]address,int[]
image){
this.name=name;
this.address=address;
this.image=image;
this.context=context;
}
@NonNull
@Override
public Recycleview_Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup
parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(context);
View
listitem=layoutInflater.inflate(R.layout.lab5b_ccustomrecycleview,parent,false);
ViewHolder viewHolder=new ViewHolder(listitem);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull Recycleview_Adapter.ViewHolder holder,
int position) {
holder.txtname.setText(name[position]);
holder.txtaddress.setText(address[position]);
holder.imageView.setImageResource(image[position]);
}
@Override
public int getItemCount() {
return name.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
TextView txtname,txtaddress;
ImageView imageView;

52 Ghanashyam Dhungana
Mobile Programming (CACS 351)

public ViewHolder(@NonNull View itemView) {


super(itemView);
txtname=itemView.findViewById(R.id.name);
txtaddress=itemView.findViewById(R.id.address);
imageView=itemView.findViewById(R.id.image);
}
}
}

// lab5b_crecycleview.java

package com.example.lab5;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
public class lab5b_crecycleview extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lab5b_crecycleview);
recyclerView=findViewById(R.id.recycleview);
String name[]={"Metawin","Kiran","Kiran","Kiran","Jyoti","Subin"};
String address[]={"Lalitpur","Ktm","Ktm","Ktm","Ktm","Jhapa"};
int
image[]={R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.im
g,R.drawable.img};
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
Recycleview_Adapter adapter=new Recycleview_Adapter(this,name,address,image);
recyclerView.setAdapter(adapter);
}
}

Output:

53 Ghanashyam Dhungana
Mobile Programming (CACS 351)

54 Ghanashyam Dhungana
Mobile Programming (CACS 351)

15. Provided that SQLite database named university with table named “student” with
following column1 (Roll as integer, Name as text, address as text, and department as
text, develop an android application to connect database and perform following
database operations.
a. Insert five student records
b. Display student information
c. Update student information where roll no. = 103
d. Delete student information who live in patan.

DataModel.java
package com.example.sqlexample;
public class DataModel {
private int id;
private String name;
private String address;
public DataModel(int id, String name, String address){
this.id=id;
this.name=name;
this.address=address;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getAddress(){
return address; }
}

ListAdapter.java
package com.example.sqlexample;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ListAdapter extends ArrayAdapter<String> {
Activity context;
ArrayList<Integer> id;
ArrayList<String> name;
ArrayList<String> address;
public ListAdapter(Activity context, ArrayList<Integer> id,
ArrayList<String> name, ArrayList<String> address) {

55 Ghanashyam Dhungana
Mobile Programming (CACS 351)

//ArrayAdapter needs String so we are supplying title


super(context, R.layout.list_items,name);
this.context=context;
this.id=id;
this.name=name;
this.address=address;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.list_items, null,true);
//wiring widgets
TextView txtId = (TextView) rowView.findViewById(R.id.txtId);
TextView txtName = (TextView) rowView.findViewById(R.id.txtName);
TextView txtAddress = (TextView) rowView.findViewById
(R.id.txtAddress);
txtId.setText(id.get(position).toString());
txtName.setText(name.get(position).toString());
txtAddress.setText(address.get(position).toString());
return rowView; };
}

MyDbHelper.java
package com.example.sqlexample;
import static android.provider.Contacts.SettingsColumns.KEY;
import static org.xmlpull.v1.XmlPullParser.TEXT;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDbHelper extends SQLiteOpenHelper{


private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydb";
public MyDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createQuery="CREATE TABLE mytable(id INTEGER PRIMARY KEY,name
TEXT, address TEXT)";
db.execSQL(createQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);

56 Ghanashyam Dhungana
Mobile Programming (CACS 351)

// Create tables again


onCreate(db);
}
public void insertData(int id,String name, String address){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("id",id);
contentValues.put("name",name);
contentValues.put("address",address);
//inserting row
db.insert("mytable",null,contentValues);
db.close();
}
public Cursor selectData(){
SQLiteDatabase db=this.getReadableDatabase();
String query="SELECT * FROM mytable";
Cursor cursor=db.rawQuery(query,null);
return cursor;
}
public void updateData(String id,String name, String address){SQLiteDatabase db =
this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("name",name);
contentValues.put("address",address);
db.update("mytable",contentValues,"id=?",new String[]{id});
db.close();
}
public void deleteData(String id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete("mytable","id=?",new String[]{id});
}
}

RecyclerViewAdapter.java
package com.example.sqlexample;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class RecyclerViewAdapter extends


RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Activity context;

57 Ghanashyam Dhungana
Mobile Programming (CACS 351)

ArrayList<DataModel> data;
public RecyclerViewAdapter(Activity context, ArrayList<DataModel>
data){
this.context=context;
this.data=data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater layoutInflater = LayoutInflater.from(context);
View listItem= layoutInflater.inflate(R.layout.list_items,
parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final DataModel current=data.get(position);
holder.txtId.setText(current.getId()+"");
holder.txtName.setText(current.getName());
holder.txtAddress.setText(current.getAddress());
} @Override
public int getItemCount() {
return data.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView txtId,txtName,txtAddress;
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
txtId = itemView.findViewById(R.id.txtId);
txtName = itemView.findViewById(R.id.txtName);
txtAddress = itemView.findViewById(R.id.txtAddress); }
}
}

ListItems.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/relat"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"

58 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_margin="10dp"
android:text="Id"
android:textStyle="bold"
android:id="@+id/txtId" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/txtName"
android:textStyle="bold"
android:layout_toRightOf="@+id/txtId"
android:layout_marginTop="10dp"
android:text="Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/txtAddress"
android:text="Address"
android:layout_marginLeft="10dp"
android:layout_below="@+id/txtName" />
</RelativeLayout>

sqliteExample.xml(activity)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_margin="5dp"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Id"
android:inputType="number"
android:id="@+id/edtId" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:layout_below="@+id/edtId"
android:id="@+id/edtName" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Address"

59 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_below="@+id/edtName"
android:id="@+id/edtAddress" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edtAddress"
android:id="@+id/btnInsert"
android:text="Insert" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edtAddress"
android:layout_toRightOf="@+id/btnInsert"
android:id="@+id/btnSelect"
android:layout_marginLeft="10dp"
android:text="Select" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edtAddress"
android:layout_toRightOf="@+id/btnSelect"
android:id="@+id/btnUpdate"
android:layout_marginLeft="10dp"
android:text="Update" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edtAddress"
android:layout_toRightOf="@+id/btnUpdate"
android:id="@+id/btnDelete"
android:layout_marginLeft="10dp"
android:text="Delete" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Selected Data:"
android:layout_below="@+id/btnSelect"
android:layout_marginTop="10dp"
android:id="@+id/txtData"
android:textSize="18sp" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerview"
android:layout_below="@+id/txtData" />
</RelativeLayout>

60 Ghanashyam Dhungana
Mobile Programming (CACS 351)

SqliteExample.java
package com.example.sqlexample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

public class SqliteExample extends AppCompatActivity {


EditText edtId,edtName,edtAddress;
Button btnInsert,btnSelect,btnUpdate,btnDelete;
MyDbHelper myDbHelper;
RecyclerView recyclerView;
RecyclerView.Adapter recyclerViewAdapter;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.sqlite_example);
myDbHelper=new MyDbHelper(this);
edtId=findViewById(R.id.edtId);
edtName=findViewById(R.id.edtName);
edtAddress=findViewById(R.id.edtAddress);
btnInsert=findViewById(R.id.btnInsert);
btnUpdate=findViewById(R.id.btnUpdate);
btnSelect=findViewById(R.id.btnSelect);
btnDelete=findViewById(R.id.btnDelete);
recyclerView=findViewById(R.id.recyclerview);
btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id=Integer.parseInt(edtId.getText().toString());
String name=edtName.getText().toString();
String address=edtAddress.getText().toString();
myDbHelper.insertData(id,name,address);
Toast.makeText(getApplicationContext(),"Data Inserted Successfully
!",Toast.LENGTH_SHORT).show();
}
});
btnSelect.setOnClickListener(new View.OnClickListener() {

61 Ghanashyam Dhungana
Mobile Programming (CACS 351)

@Override
public void onClick(View view) {ArrayList<DataModel> data=new ArrayList<>();
//calling select function
Cursor cursor=myDbHelper.selectData();
while (cursor.moveToNext()){
int id=cursor.getInt(0);
String name=cursor.getString(1);
String address=cursor.getString(2);
DataModel dataModel=new DataModel(id,name,address);
data.add(dataModel);
}
layoutManager=new LinearLayoutManager
(SqliteExample.this);
recyclerView.setLayoutManager(layoutManager);
recyclerViewAdapter=new RecyclerViewAdapter (SqliteExample.this,data);
recyclerView.setAdapter(recyclerViewAdapter);
} });
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id=edtId.getText().toString();
String name=edtName.getText().toString();
String address=edtAddress.getText().toString();
myDbHelper.updateData(id,name,address);
Toast.makeText(getApplicationContext(),"Data Updated Successfully
!",Toast.LENGTH_SHORT).show();
} });
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id=edtId.getText().toString();
myDbHelper.deleteData(id);
Toast.makeText(getApplicationContext(),"Data Deleted Successfully
!",Toast.LENGTH_SHORT).show();
}
});
}
}

62 Ghanashyam Dhungana
Mobile Programming (CACS 351)

16. Develop an android application that demonstrate retrieval of contents from remote
server.
//connection.php

<?php
$conn= new mysqli("localhost","root","","campus");
if ($conn){
}
else
echo "Error in connection<br>";
$sql="SELECT * FROM Student";
$result=$conn->query($sql);
$json=array();
if($result->num_rows>0){
while ($row=$result->fetch_array()){
$json["data"][]=array("roll"=>$row["roll"],"name"=>$row["name"],"address"=>$ro
w["address"]);
}
}
echo json_encode($json);
$conn->close();
?>

//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:divider="@color/black"
android:dividerHeight="2dp"/>
</LinearLayout>

//datalist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Roll"

63 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:textSize="18sp"
android:layout_weight="1"
android:id="@+id/roll"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:textSize="18sp"
android:id="@+id/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:textSize="18sp"
android:id="@+id/address"/>
</LinearLayout>
// ListAdapter.java
package com.example.remoteapplication;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class ListAdapter extends ArrayAdapter<String> {
Activity context;
ArrayList<Integer>roll;
ArrayList<String>name;
ArrayList<String>address;
public ListAdapter(Activity context, ArrayList<Integer> roll,ArrayList<String>
name,ArrayList<String> address){
super(context,R.layout.datalist,name);
this.context=context;
this.roll=roll;
this.name=name;
this.address=address;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull
ViewGroup parent) {

64 Ghanashyam Dhungana
Mobile Programming (CACS 351)

LayoutInflater inflater=context.getLayoutInflater();
View row= inflater.inflate(R.layout.datalist,null,true);
TextView txtroll=row.findViewById(R.id.roll);
TextView txtname=row.findViewById(R.id.name);
TextView txtaddress=row.findViewById(R.id.address);
txtroll.setText(roll.get(position).toString());
txtname.setText(name.get(position).toString());
txtaddress.setText(address.get(position).toString());
return row;
}
}
//MainActivity.java
package com.example.remoteapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setContentView(R.layout.datalist);
list=findViewById(R.id.list);
volleyRequest();
}
public void volleyRequest() {
RequestQueue queue= Volley.newRequestQueue(this);
String url="http://10.0.2.2/Api/connection.php";
StringRequest stringRequest=new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
DecodeJson(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

65 Ghanashyam Dhungana
Mobile Programming (CACS 351)

Log.d("Exception:",error.toString());
} });
queue.add(stringRequest);
}
private void DecodeJson(String response) {
try {
ArrayList<Integer>roll=new ArrayList<>();
ArrayList<String>name=new ArrayList<>();
ArrayList<String>address=new ArrayList<>();
JSONObject result = new JSONObject(response);
JSONArray data = result.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject student = data.getJSONObject(i);
roll.add(student.getInt("roll"));
name.add(student.getString("name"));
address.add(student.getString("address"));
// Log.d("Row"+i, "roll=" + roll + "name=" + name + "address=" +
address);
}
ListAdapter adapter= new ListAdapter(this,roll, name, address);
list.setAdapter(adapter);
}
catch (Exception e){
}
}
}

Output:

66 Ghanashyam Dhungana
Mobile Programming (CACS 351)

17. Develop an android application that demonstrates sending of contents to remote


server.

//Setdata.php

<?php
$id=$_POST['id'];
$name=$_POST['name'];
$address=$_POST['address'];
$conn=new mysqli("localhost","root","","College");
if ($conn->connect_error) {
die("Connection failed:".$conn->connect_error);
}
$sql="INSERT INTO Student VALUES ('$id','$name','$address')";
$result=$conn->query($sql);
if ($result>0)
echo "Data insert successfully";
else
echo "Error in insertion data";
$conn->close();
?>

//Activity_senddata.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"
android:layout_margin="5dp"
android:orientation="vertical"
tools:context=".SendData">
<EditText
android:id="@+id/edtid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter id"
android:inputType="number"/>
<EditText
android:id="@+id/edtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name"
android:layout_below="@+id/edtid"/>

<EditText
android:id="@+id/edtaddress"
android:layout_width="match_parent"

67 Ghanashyam Dhungana
Mobile Programming (CACS 351)

android:layout_height="wrap_content"
android:hint="Enter address"
android:layout_below="@+id/edtname"/>
<Button
android:id="@+id/sendbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_below="@+id/edtaddress"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>

// SendData.java

public class SendData extends AppCompatActivity {


EditText edtId,edtName,edtAddress;
Button sendbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_data);
edtId=findViewById(R.id.edtid);
edtName=findViewById(R.id.edtname);
edtAddress=findViewById(R.id.edtaddress);
sendbtn=findViewById(R.id.sendbtn);
sendbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
volleyRequest();
}
});
}
public void volleyRequest()
{
RequestQueue queue= Volley.newRequestQueue(this);
String url="http://10.0.2.2/JSONEX/setdata.php";
StringRequest stringRequest=newStringRequest(Request.Method.POST, url,
new Response.Listener<String>() {

@Override
public void onResponse(String response) {

Toast.makeText(SendData.this,response,Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

68 Ghanashyam Dhungana
Mobile Programming (CACS 351)

Log.d("Exception",error.toString());
}
}){
protected HashMap<String, String> getParams(){
HashMap<String,String> params=new HashMap<>();
params.put("id",edtId.getText().toString());
params.put("name",edtName.getText().toString());
params.put("address",edtAddress.getText().toString());
return params;
}
};
queue.add(stringRequest);
}
}

69 Ghanashyam Dhungana
Mobile Programming (CACS 351)

18. Develop an android application that display Google map.


//Activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />

//MapsActivity.java
package com.example.googlemap;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.example.googlemap.databinding.ActivityMapsBinding;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private ActivityMapsBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be
used.
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng location = new LatLng(27.6794985,85.3143896);
mMap.addMarker(new MarkerOptions().position(location).title(" Patandhoka"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
mMap.animateCamera(CameraUpdateFactory.zoomTo(12.0f));
}
}

70 Ghanashyam Dhungana
Mobile Programming (CACS 351)

Output:

71 Ghanashyam Dhungana

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy