You are on page 1of 54

Name: Simran Sabat Roll No : FIT2146

T.Y.B.Sc(IT)
Advance Mobile Programming Journal
A.Y. 2023-24
(SEMESTER VI)

1|Page
Name: Simran Sabat Roll No : FIT2146

Dr. Homi Bhabha State University, Mumbai


Elphinstone College
156, M. G. Road, Fort, Mumbai 400 032.

DEPARTMENT OF INFORMATION TECHNOLOGY

CERTIFICATE

This is to certify that Mr. / Miss. Simran Shrikant Sabat having Exam
Seat No./Roll No. FIT2146 of T.Y.BSc.IT (Semester VI) has completed the
Practical work in the subject of Advance Mobile Programming during the
academic Year 2023-24 under the guidance of Asst.Prof. Sneha Jadhav being
the partial requirement for the fulfillment of the curriculum of Degree of
Bachelor of Science in Information Technology, Elphinstone College, Dr. Homi
Bhabha State University.

Professor In-Charge Co- Coordinator

Date: ………………. College Seal

2|Page
Name: Simran Sabat Roll No : FIT2146

INDEX

Practical Practical Name Date Signature


No
1 Building a simple Hello World
Application.
2 Programming Resources Android
Resources:(Color,Theme,String,
Draw able, Dimension, Image).
3 Programming Activities and
fragments (Activity Life Cycle,
Activity methods, Multiple
Activities, Life Cycle of
fragments and multiple
fragments.)
4 Program related to different
layouts (Linear, Relative, and
Table.)
Calculator Application
5
6 Programming menus and dialog
Programs on Intents, Events,
7 Listeners and Adapters.

3|Page
Name: Simran Sabat Roll No : FIT2146

Practical No 1:
Aim: Building a simple Hello World Application.
Steps to Install Android Studio

Step – 1: Head over to bellow link to get the Android Studio executable or zip
file.

https://developer.android.com/studio/#downloads

Step – 2: Click on the download android studio button.

Click on the “I have read and agree with the above terms and conditions”
checkbox followed by the download button.

4|Page
Name: Simran Sabat Roll No : FIT2146

Click on save file button in the appeared prompt box and the file will start
downloading.

Step – 3:After the downloading has finished, open the file from downloads and
run it. It will prompt the following dialogue box

5|Page
Name: Simran Sabat Roll No : FIT2146

Click on next.
In the next prompt it‟ll ask for a path for installation. Choose a path and hit next.
Step –4: It will start the installation, and once it is completed, it will be like the
image shown below

Click on next

Step – 5: Once “Finish” is clicked, it will ask whether the previous settings need
to be imported [if android studio had been installed earlier], or not.
It is better to choose the „Don‟t import Settings option‟.
6|Page
Name: Simran Sabat Roll No : FIT2146

Step – 6: This will start the Android Studio.

Meanwhile it will be finding the available SDK components.

Step – 7: After it has found the SDK components, it will redirect to the Welcome dialog box.

7|Page
Name: Simran Sabat Roll No : FIT2146

Click on next.

8|Page
Name: Simran Sabat Roll No : FIT2146

Choose Standard and click on Next. Now choose the theme, whether Light
theme or the Dark one. The light one is called the IntelliJ theme whereas the
dark theme is called Darcula. Choose as required.

 Click on the Next button


 Step – 8: Now it is time to download the SDK components.

9|Page
Name: Simran Sabat Roll No : FIT2146

Click on Finish.

It has started downloading the components

The Android Studio has been successfully configured. Now it‟s time to launch
and build apps.

10 | P a g e
Name: Simran Sabat Roll No : FIT2146

Steps to create a project in android


To create your new Android project, follow these steps:
 Install the latest version of Android Studio.

 In the Welcome to Android Studio window, click Start a new


Android Studio project.

If you have a project already opened, select File > New > New Project.

In the Choose your project window, select Empty Activity and click Next.

In the Configure your project window, complete the following:


1. Enter "My First App" in the Name field.
2. Enter "com.example.myfirstapp" in the Package name field.
3. If you'd like to place the project in a different folder, change its Save
location.

11 | P a g e
Name: Simran Sabat Roll No : FIT2146

4. Select either Java or Kotlin from the Language drop-down menu.


5. Select the checkbox next to Use androidx.* artifacts.
6. Leave the other options as they are.
Click Finish.

After some processing time, the Android Studio main window appears.

Note
To Open Project Window

select View > Tool Windows > Project

To Open MainActivity.java file

app > java > PackageName > MainActivity.java

To Open Layout activity_main.xml file

app > res > layout > activity_main.xml

12 | P a g e
Name: Simran Sabat Roll No : FIT2146

After the project is created, there are 2 files,


MainActivity.java andactivity_main.xml
1. Go to activity_main.xml and select Design View
2. In Design View, change the layout to LinearLayout(Vertical)
select Add TextView, and change the text to “Hello World!”
3. Click on Run and select the AVD already created(if not
created, firstcreate the AVD)
4. Output screen should show “Hello World”
5.
To create a new AVD:

1. Open the AVD Manager by clicking Tools > AVD Manager.


2. Click Create Virtual Device, at the bottom of the AVD Manager dialog. ...
3. Select a hardware profile, and then click Next.
4. Select the system image for a particular API level, and then click Next.
5. Change AVD properties as needed, and then click Finish.

13 | P a g e
Name: Simran Sabat Roll No : FIT2146

14 | P a g e
Name: Simran Sabat Roll No : FIT2146

Output:

15 | P a g e
Name: Simran Sabat Roll No : FIT2146

Practical No 2:
Aim: Programming Resources Android Resources:(Color, Theme,
String, Drawable, Dimension, Image).
a. Defining Color Property.
1. Create a new project and go to:
ProjectName>App>src>main>res>values>colors.xml
2. Defining new color properties in colors.xml
3. Go to
4. ProjectName->app->res->values->colors.xml

Go to ProjectName->app->res->values->colors.xml
Colors.xml

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


<resources>
Default Content
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>

Add this code in the


// Define new colors here using hexadecimal color codes.
colors.xml

<color name="Red">#FF0000</color>
<color name="Blue">#0000FF</color>

Now go to activity_main.xml and type the following code:


 Drag and drop LinearLayout(Vertical) From Layout tab in Palette
window
 Drag and drop four TextView from Text tab in palette window
 The default Code of one TextView is as follows

16 | P a g e
Name: Simran Sabat Roll No : FIT2146

<TextView android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="TextView" />

Now call the colors from the colors.xml as


android:background="@color/Red"

Now the TextView Code looks like

<TextView android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="TextView"

android:background="@color/Red"

Now the final Code of activity_main.xml is

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


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

<LinearLayout
android:layout_width="40
9dp"
android:layout_height="7
29dp"
android:orientation="verti
cal"
tools:layout_editor_absolu
teX="1dp"
tools:layout_editor_absolut
eY="1dp">

17 | P a g e
Name: Simran Sabat Roll No : FIT2146

<TextView
android:id="@+id/textV
iew"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="TextView"
android:background="@color/Red"
/>
<TextView android:id="@+id/textView2"
android:layout_width="match_parent"android:layout_height="52dp"
android:text="TextView"

android:background="@color/Grey"
/>
<TextView android:id="@+id/textView3"
android:layout_width="match_parent"android:layout_height="59dp"
android:text="TextView"
android:background="@color/Blue"
/>
<TextView android:id="@+id/textView4"
android:layout_width="match_parent"android:layout_height="51dp"
android:text="TextView"
android:background="@color/Cyan"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

18 | P a g e
Name: Simran Sabat Roll No : FIT2146

Output

We are done with Colors

b. Defining Theme Property.


 Defining new theme properties in styles.xml
 Create a new project and go to:
ProjectName>App>src>main>res>values>styles.

styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:background">#000000</item>

19 | P a g e
Name: Simran Sabat Roll No : FIT2146

<item name="android:textColor">#FFFFAA</item>
</style>
</resources>


Output

We are done with the styles


c. Defining string property
 Defining paragraph and header property in strings.xml
 Create a new project and go to:
ProjectName>App>src>main>res>values>strings.xml

20 | P a g e
Name: Simran Sabat Roll No : FIT2146

strings.xml
<resources>
<string name="app_name">Prof. Shahid Ansari</string>
<string name="Heading">Programming Resources</string>
<string name="Description">Android is a mobile operating system developed by Google.It is
based on a modified version of the Linux kernel and other open source software, and is
designed primarily for touch screen mobile devicessuch as smart phones and tablets.
</string>
</resources>

Now go to activity_main.xml and type the following code



<TextView android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="56dp" android:background="@color/Red"

android:text="@string/Heading"
android:textSize="24sp" />

<TextView android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Grey"

android:text="@string/Description"
android:textSize="24sp" />









 
21 | P a g e
Name: Simran Sabat Roll No : FIT2146


d. Adding images and dimensions

 Adding Images to Application created

 For adding a new image files, do the following:

 ProjectName>App>src>main>res>drawable>Right-click and paste the imagesthat


are copied

 Add a new dim.xml file and write the following code in dimens.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<dimen name="textview_height">35dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="font_size">26sp</dimen>
</resources>

Now go to activity_main.xml and type the following code:

22 | P a g e
Name: Simran Sabat Roll No : FIT2146

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


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

<LinearLayout android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">

<ImageView android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> 
Output

23 | P a g e
Name: Simran Sabat Roll No : FIT2146

Practical No 3:
Aim: Programming Activities and fragments
Activity Life Cycle, Activity methods, Multiple Activities, Life Cycle
of fragments and multiple fragments.
a) Activity Life Cycle
The various methods to be created are onStart(), onRestart(),
onStop(), onResume(),onDestroy() and onPause() as shown in

the figure.

Create a new project and go to, MainActivity.java

24 | P a g e
Name: Simran Sabat Roll No : FIT2146

package MaharashtraCollege.example.profshahidansari;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {

String tag= "Android Lifecycle Demonstration";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag,"In the onCreate() event");
}
public void onStart()
{
super.onStart();
Log.d(tag,"In the onStart() event");
}
public void onRestart()
{ super.onRestart();
Log.d(tag,"In the onRestart() event");
}
public void onPause()
{
super.onPause();
Log.d(tag,"In the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag,"In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag,"In the onDestroy() event");
}

Output:

25 | P a g e
Name: Simran Sabat Roll No : FIT2146

b) Life Cycle of fragments

Create a new project Add a new Fragment


ProjectName>App>src>main>Java
Right click on Java and select ‘Add Fragment’>Add Fragment(Blank)
Give a name to the fragment and click Finish

package MaharashtraCollege.example.profshahidansari;

26 | P a g e
Name: Simran Sabat Roll No : FIT2146

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.util.Log;

public class LifeCycleofFragment extends


Fragment { String tag = "Life Cycle of
Fragment";
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 =
"param1"; private static final String
ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters


private String
mParam1; private
String mParam2;

private OnFragmentInteractionListener

mListener; public LifeCycleofFragment() {

// TODO: Rename and change types and number of parameters


public static LifeCycleofFragment newInstance(String param1,
String param2) {
LifeCycleofFragment fragment = new
LifeCycleofFragment(); Bundle args = new Bundle();
args.putString(ARG_PARAM
1, param1);
args.putString(ARG_PARAM
2, param2);
fragment.setArguments(ar
gs); return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInsta
ceState);
if (getArguments() !=
null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,Bundle savedInstanceState) {

27 | P a g e
Name: Simran Sabat Roll No : FIT2146

// Inflate the layout for this fragment


return inflater.inflate(R.layout.fragment_life_cycleof, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()+ " must implement
OnFragmentInteractionListener");

}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;

}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);

}
public void onStart()
{
super.onStart();
Log.d(tag,"In the onStart() event");

28 | P a g e
Name: Simran Sabat Roll No : FIT2146

}
public void onRestart()
{
Log.d(tag,"In the onRestart() event");

}
public void onPause()

{
super.onPause();
Log.d(tag,"In the onPause() event");

}
public void onStop()
{
super.onStop();
Log.d(tag,"In the onStop() event");

public void onDestroy()

super.onDestroy();

Log.d(tag,"In the onDestroy() event");

29 | P a g e
Name: Simran Sabat Roll No : FIT2146

Output:

30 | P a g e
Name: Simran Sabat Roll No : FIT2146

Practical No 4:
Aim: Program related to different layouts
Linear,Relative,Table.
a) Linear Layout in Android
LinearLayout is a ViewGroup that is responsible for holding views in
it. It is a layout that arranges its children i.e the various views and
layouts linearly (one after another) in a single column(vertically) or a
single row(horizontally).

Horizontal LinearLayout
In a horizontal LinearLayout, as the name suggests, the Views defined
inside the Linear Layout will be arranged horizontally one after another,
like in a row. By default, the orientation is set to horizontal.
But its a good practice to explicitly specify the orientation of the
linear layout by setting the attribute android:orientation with value
horizontal in the LinearLayout tag.

Vertical Linear Layout


In a vertical LinearLayout, as the name suggests, the Views defined
inside the Linear Layout are arranged verically one after another, like
in a column. And for this we need to mention the android:orientation
attribute with value vertical within the LinearLayout tag.

31 | P a g e
Name: Simran Sabat Roll No : FIT2146

Activity_main.xml
Remember
<?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"
If you want horizontal layout just
android:layout_width="match_parent"
changeorientation to horizontal
android:layout_height="match_parent"
android:orientation="horizontal"
android:orientation="ver
tical"
tools:context=".MainActi
vity">

<EditText
android:id="@+id/edit
Text"
android:layout_width="wrap_con
tent"
android:layout_height="86dp"
android:layout_weight="1"
android:ems="10"
android:hint="Enter The Text"
android:inputType="textPersonN
ame"android:text="Type Here"
android:textColor="#FFEB3B" />

32 | P a g e
Name: Simran Sabat Roll No : FIT2146

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" You can also change the orientation
android:layout_weight="1" using attribute window as
android:text="Button" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>

Note: Here We are getting background as black because we set the


background color in style.xml in previous practical.

<item name="android:background">#000000</item>
<item name="android:textColor">#FFFFAA</item>

33 | P a g e
Name: Simran Sabat Roll No : FIT2146

Output:

Horizontal LinearLayout Vertical LinearLayout

b) Relative Layout:

RelativeLayout is a view group that displays child views in relative


positions. The position of each view can be specified as relative to
sibling elements (such as to the left-of or below another view) or in
positions relative to the parent RelativeLayout area (such as aligned
to the bottom, left or center).

A RelativeLayout is a very powerful utility for designing a user


interface because it can eliminate nested view groups and keep your
layout hierarchy flat, which improves performance. If you find
yourself using several nested LinearLayout groups, you may be able
to replace them with a single RelativeLayout.

34 | P a g e
Name: Simran Sabat Roll No : FIT2146

Activity_main.xml
Remember
<?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:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Text Here" />
<Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times" />
<Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="Click Here" />

</RelativeLayout>

35 | P a g e
Name: Simran Sabat Roll No : FIT2146

c) Table Layout:
Android TableLayout going to be arranged groups of views into rows
and columns. You will use the<TableRow> element to build a row in
the table. Each row has zero or more cells; each cell can hold one View
object.
TableLayout containers do not display border lines for their rows,
columns, or cells. The table will have as many columns as the row
with the most cells. A table can leave cells empty. Cells can span
multiple columns, as they can in HTML. You can span columns
by using the span field in the TableRow.LayoutParams class.

Main_activity.xml
Remember
<?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"
android:stretchColumns="1">
<TableRow>

<TextView
android:layout_width="225dp"
android:layout_height="56dp"
android:padding="3dip"
android:text="Row1,Col1" />

<TextView
android:layout_height="match_parent"
android:gravity="right"
android:padding="3dip"
android:text="Row1,Col2" />
</TableRow>

<TableRow>

<TextView
android:layout_width="242dp"
android:layout_height="64dp"
android:padding="3dip"
android:text="Row2,Col1" />

<TextView
android:layout_height="match_parent"
android:gravity="right"
android:padding="3dip"
android:text="Row2,Col2" />

36 | P a g e
Name: Simran Sabat Roll No : FIT2146

</TableRow>
</TableLayout>

37 | P a g e
Name: Simran Sabat Roll No : FIT2146

Practical No 5:
Aim: Calculator Application
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:stretchColumns="1">

<LinearLayout
android:layout_width="match_parent
"
android:layout_height="match_parent
"android:orientation="vertical">

<EditText
android:id="
@+id/et1"
android:layout_width="match_pare
nt"android:layout_height="70dp"
android:ems="10"
android:inputType="textPersonNa
me"android:text="Input1" />

<EditText
android:id="
@+id/et2"
android:layout_width="match_pare
nt"android:layout_height="64dp"
android:ems="10"
android:inputType="textPersonNa
me"
android:text="Input2" />

<Button
android:id="@+i
d/btnAdd"
android:layout_width="matc
h_parent"
android:layout_height="wra
p_content"
38 | P a g e
Name: Simran Sabat Roll No : FIT2146

android:text="Addition" />

<Button
android:id="@+i
d/btnSub"
android:layout_width="matc
h_parent"
android:layout_height="wra
p_content"
android:text="Subtraction"
/>

<Button
android:id="@+i
d/btnMult"
android:layout_width="matc
h_parent"
android:layout_height="wra
p_content"
android:text="Multiplication
" />

<Button
android:id="@+i
d/btnDiv"
android:layout_width="matc
h_parent"
android:layout_height="wra
p_content"
android:text="Division" />

<Button
android:id="@+i
d/btnClear"
android:layout_width="matc
h_parent"
android:layout_height="wra
p_content"
android:text="Clear" />

<TextView
android:id="@+i
d/tv1"
android:layout_width="match_parent"
android:layout_height="63dp"
android:text="Output"
android:textColor="@android:color/background_dark"
android:textSize="18sp"

39 | P a g e
Name: Simran Sabat Roll No : FIT2146

android:textStyl
e="bold"
app:fontFamily
="casual" />
</LinearLayout>
</LinearLayout>

Main_Activity.java

package MaharashtraCollege.example.profshahidansari;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import static android.view.View.*;

public class MainActivity extends AppCompatActivity {

EditText t1,t2;
Button b1,b2,b3,b4,b5;
TextView tv1;
int n1=0,n2=0;
String s1,s2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

t1 = (EditText) findViewById(R.id.et1);
t2 = (EditText) findViewById(R.id.et2);

b1 = (Button) findViewById(R.id.btnAdd);
b2 = (Button) findViewById(R.id.btnSub);
b3 = (Button) findViewById(R.id.btnMult);
b4 = (Button) findViewById(R.id.btnDiv);
b5 = (Button) findViewById(R.id.btnClear);

tv1 = (TextView) findViewById(R.id.tv1);

b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

40 | P a g e
Name: Simran Sabat Roll No : FIT2146

try {
String s1 = t1.getText().toString();
String s2 = t2.getText().toString();
n1 = Integer.parseInt(s1);
n2 = Integer.parseInt(s1);
int sum = n1 + n2;
tv1.setText("Addition ="+sum);
}
catch (NumberFormatException e)
{

}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

try {
String s1 = t1.getText().toString();
String s2 = t2.getText().toString();
n1 = Integer.parseInt(s1);
n2 = Integer.parseInt(s1);
int sub = n1 - n2;
tv1.setText("Subtraction ="+sub);
}
catch (NumberFormatException e)
{

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

try {
String s1 = t1.getText().toString();
String s2 = t2.getText().toString();
n1 = Integer.parseInt(s1);
n2 = Integer.parseInt(s1);
int m = n1 * n2;
tv1.setText("Multiplication ="+m);

41 | P a g e
Name: Simran Sabat Roll No : FIT2146

}
catch (NumberFormatException e)
{

}
});

b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

try {
String s1 = t1.getText().toString();
String s2 = t2.getText().toString();
n1 = Integer.parseInt(s1);
n2 = Integer.parseInt(s1);
int d = n1 / n2;
tv1.setText("Division ="+d);
}
catch (NumberFormatException e)
{

}
});

b5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

t1.setText(" ");
t2.setText(" ");
tv1.setText(" ");

}
});
}
}

42 | P a g e
Name: Simran Sabat Roll No : FIT2146

Output:

43 | P a g e
Name: Simran Sabat Roll No : FIT2146

Practical No 6:
Aim: Programming menus and dialog
Creation of Menu:
 Create a new project and go to,
 Project > App -> src -> res(Right-click on res and select) ->
New -> Android Resource File -> Filename(menus) and Resource
Type Menu
 Now go to, menus.xml file and make the following changes to
code file,

menus.xml

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


<menu xmlns:android="http://schemas.android.com/apk/res/android">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
<item android:id="@+id/newb" android:title="File" />
<item android:id="@+id/search" android:title="Open" />
<item android:id="@+id/save" android:title="Save"/>
<item android:id="@+id/Share" android:title="Save As" />
<item android:id="@+id/delete" android:title="Delete" />
<item android:id="@+id/Exit" android:title="Close" />

</menu>

44 | P a g e
Name: Simran Sabat Roll No : FIT2146

Now go to MainActivity.java file and make the following


changes to code,
MainActivity.java

package MaharashtraCollege.example.profshahidansari;

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;
public class MainActivity extends AppCompatActivity {

public boolean onCreateOptionsMenu(Menu menu)


{
MenuInflater mi=getMenuInflater();
mi.inflate(R.menu.menus,menu);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
public boolean onOptionsItemSelected(MenuItem item)
{

switch (item.getItemId())
{
case R.id.newb:
Toast.makeText(this,"File Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.Share:
Toast.makeText(this,"Open Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.delete:
Toast.makeText(this,"Save Selected",Toast.LENGTH_SHORT).show();

45 | P a g e
Name: Simran Sabat Roll No : FIT2146

return true;
case R.id.save:
Toast.makeText(this,"Save As Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.search:
Toast.makeText(this,"Delete Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.Exit: Toast.makeText(this,"Exit Selected",Toast.LENGTH_SHORT).show();
return true;
default:
Toast.makeText(this,"Default",Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
}

Output

46 | P a g e
Name: Simran Sabat Roll No : FIT2146

Creation of Dialog
Create a new project and go to, activity_main.xml, and make the
following additions to the code
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:stretchColumns="1">

<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/button_dialog"
android:onClick="onClickDialog"
android:text="Click to display an alert dialog"/>

<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/button_progressdialog"
android:onClick="onClickProgressDialog"
android:text="Click to display a progressdialog"/>

<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/button_datedialog"
android:onClick="onClickDateDialog"
android:text="Click to display a DatePicker dialog"/>

<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/button_timedialog"
android:onClick="onClickTimeDialog"
android:text="Click to display a TImePicker dialog"/>

<TextView
android:id="@+id/textView_date"
android:layout_width="398dp"
android:layout_height="wrap_content"

47 | P a g e
Name: Simran Sabat Roll No : FIT2146

android:text="TextView1"
android:textSize="50dp" />

<TextView
android:id="@+id/textView_time"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:text="TextView2"
android:textSize="50dp" />

</LinearLayout>

Go to file MainActivity.java and make the following changes to


the code.
Main_Activity.java

package MaharashtraCollege.example.profshahidansari;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.os.Bundle;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

CharSequence[] items={"Android","Security","Cloud"};
boolean[] itemsChecked=new boolean[items.length];

48 | P a g e
Name: Simran Sabat Roll No : FIT2146

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

public void onClickDialog(View v)


{
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("This is a dialog with a simple Text");


builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int id)


{
Toast.makeText(getBaseContext(), "OK CLicked", Toast.LENGTH_SHORT).show();
} });
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
@Override public void onClick(DialogInterface dialog, int id)
{
Toast.makeText(getBaseContext(), "Cancel CLicked", Toast.LENGTH_SHORT).show();
} });
builder.setMultiChoiceItems(items, itemsChecked, new
DialogInterface.OnMultiChoiceClickListener()
{
@Override public void onClick(DialogInterface dialog, int id, boolean isChecked)
{
Toast.makeText(getBaseContext(), items[id] + (isChecked ? "checked!" : "unchecked!"),
Toast.LENGTH_SHORT).show();
} });
AlertDialog dialog = builder.create(); builder.show();
}

49 | P a g e
Name: Simran Sabat Roll No : FIT2146

Output:

50 | P a g e
Name: Simran Sabat Roll No : FIT2146

Practical No 7:
Aim: Programs on Intents, Events, Listeners and Adapters.
Create a new project and go to, activity_main.xml, and make the following
additions to the code
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="132dp"
android:ems="10"
android:hint="Enter Data"
android:inputType="textPersonName" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Send" />
</RelativeLayout>

Main_Activity.java

51 | P a g e
Name: Simran Sabat Roll No : FIT2146

package MaharashtraCollege.example.profshahidansari;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import
android.view.View; import
android.widget.Button; import
android.widget.EditText;

public class MainActivity extends AppCompatActivity {EditText

t1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(EditText)findViewById(R.id.txt1);
b1=(Button)findViewById(R.id.btn1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data=t1.getText().toString();
Intent intent=new Intent(getApplicationContext(),DataRecieve.class);
intent.putExtra("data",data);
startActivity(intent);
}
});
}
}

Now create another activity Activity_data_receive.xml


Activity_data_receive.xml

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


<RelativeLayoutxmlns: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=".DataRecieve">

52 | P a g e
Name: Simran Sabat Roll No : FIT2146

<TextView android:id="@+id/tv1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="152dp"
android:layout_marginLeft="152dp"
android:layout_marginTop="161dp"
android:layout_marginEnd="132dp"
android:layout_marginRight="132dp" />
</RelativeLayout>

Data_Receive.java
package com.org.datapass.datapass;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class DataRecieveextends AppCompatActivity {TextViewt1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_recieve);
t1=(TextView)findViewById(R.id.tv1);
Intent intent=getIntent();
String data=intent.getStringExtra("data");
t1.setText(data);

}
}

53 | P a g e
Name: Simran Sabat Roll No : FIT2146

Output:

54 | P a g e

You might also like