0% found this document useful (0 votes)
2 views65 pages

AP program 1 to 16

The document is an Android Programming Lab Manual for VI Semester CE/IT Diploma Engineering, authored by Ms. Swati Chavda. It provides step-by-step instructions for setting up Android Studio, creating a 'Hello World' application, and various practical exercises involving Android application development, including lifecycle management, layout designs, and using different widgets. The manual includes code snippets and XML layouts for practical implementations.

Uploaded by

babap9579
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views65 pages

AP program 1 to 16

The document is an Android Programming Lab Manual for VI Semester CE/IT Diploma Engineering, authored by Ms. Swati Chavda. It provides step-by-step instructions for setting up Android Studio, creating a 'Hello World' application, and various practical exercises involving Android application development, including lifecycle management, layout designs, and using different widgets. The manual includes code snippets and XML layouts for practical implementations.

Uploaded by

babap9579
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 65

ANDROID MANUAL | Ms Swati Chavda

ANDROID PROGRAMMING
LAB MANUAL
FOR VI SEMESTER CE/IT DIPLOMA ENGINEERING
BY Ms Swati Chavda (SMC), IOT/BSPP

1
ANDROID MANUAL | Ms Swati Chavda

Install Android Studio and Run Hello World

Android Studio is Google's IDE for Android Applications. It contains tools for
development, debugging, testing, and performance. That make it faster and easier to
develop apps.

We can test our apps with a large range of preconfigured emulators or on your
own mobile device, and build production APKs for publication.

For the latest information on system requirements and installation instructions,


refer to the documentation at developer.android.com.

Set-up Java Development Kit (JDK)


Android IDE : Android Studio or Eclipse

Link for Detail :https://www.geeksforgeeks.org/guide-to-install-and-set-up-android-


studio/

2
ANDROID MANUAL | Ms Swati Chavda

Pratical : 1 – Installation and setup of java development kit(JDK),setup android


SDK,setup eclipse IDE,setup android development tools (ADT) plugins,create
android virtual device.
Let's see the list of software required to setup android for eclipse IDE manually.

Install the JDK


• For creating android application, JDK must be installed if you are developing
the android application with Java language. download the JDK

Download and install the Eclipse for developing android application


• For developing the android application using eclipse IDE, you need to install
the Eclipse. you can download it from this location download the Eclipse.
Eclipse classic version is recommended but we are using the Eclipse IDE for
JavaEE Developers.

Download and Install the android SDK


• First of all, download the android SDK. In this example we have installed the
android SDK for windows (.exe version).
• Now double click on the exe file, it will be installed. I am using the android 2.2
version here.

Intall the ADT plugin for eclipse


• ADT (Android Development Tools) is required for developing the android
application in the eclipse IDE. It is the plugin for Eclipse IDE that is designed
to provide the integrated environment.
• For downloading the ADT, you need to follow these steps:
o Start the eclipse IDE, then select Help > Install new software...
o In the work with combo box, write https://dl-
ssl.google.com/android/eclipse/
o select the checkbox next to Developer Tools and click next
o You will see, a list of tools to be downloaded here, click next
o click finish
o After completing the installation, restart the eclipse IDE

3
ANDROID MANUAL | Ms Swati Chavda

Configure the ADT plugin


• After the installing ADT plugin, now tell the eclipse IDE for your android SDK
location. To do so:
o Select the Window menu > preferences
o Now select the android from the left panel. Here you may see a dialog
box asking if you want to send the statistics to the google. Click proceed.
o Click on the browse button and locate your SDK directory e.g. my SDK
location is C:\Program Files\Android\android-sdk.
o Click the apply button then OK.

Create the AVD


• For running the android application in the Android Emulator, you need to
create and AVD. For creating the AVD:
o Select the Window menu > AVD Manager
o Click on the new button, to create the AVD
o Now a dialog appears, write the AVD name e.g. myavd. Now choose
the
o target android version e.g. android2.2.
o click the create AVD

Create the hello android application

4
ANDROID MANUAL | Ms Swati Chavda

Practical:2 Create First Android Project, that will display “Hello World” in the
middle of the screen.
MainActivity.java
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

activity_main.xml

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


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello GUNI!"
app:layout_constraintBottom_toBottomOf="parent"
5
ANDROID MANUAL | Ms Swati Chavda

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

6
ANDROID MANUAL | Ms Swati Chavda

OUTPUT:-

7
ANDROID MANUAL | Ms Swati Chavda

Practical:3 Create and Start Activity Lifecycle and Instance State

MainActivity.java

package com.example.lifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "activity is created", Toast.LENGTH_SHORT).show();
}

protected void onStart(){


super.onStart();
Toast.makeText(this, "activity is started", Toast.LENGTH_SHORT).show();
}

@Override
protected void onResume() {
super.onResume();

Toast.makeText(this, "activity is resumed", Toast.LENGTH_SHORT).show();


}

@Override
protected void onPause() {
super.onPause();

8
ANDROID MANUAL | Ms Swati Chavda

Toast.makeText(this, "activity is paused", Toast.LENGTH_SHORT).show();


}

@Override
protected void onStop() {
super.onStop();
Toast.makeText(this, "activity is stopped", Toast.LENGTH_SHORT).show();
}

@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this, "activity is restarted",
Toast.LENGTH_SHORT).show();

@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(this, "activity is destroid", Toast.LENGTH_SHORT).show();
}

9
ANDROID MANUAL | Ms Swati Chavda

activity_main.xml

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


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello GUNI!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

10
ANDROID MANUAL | Ms Swati Chavda

Output:-

11
ANDROID MANUAL | Ms Swati Chavda

12
ANDROID MANUAL | Ms Swati Chavda

Practical:4 Write a android program to demonstrate the basic views(textview,


edittext, button, rating bar and clock)using table layout.
MainActivity.java
package com.example.tablelayout;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

activity_main.xml

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


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

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

<TextView
android:layout_width="wrap_content"
13
ANDROID MANUAL | Ms Swati Chavda

android:layout_height="wrap_content"
android:text="TIME"
android:layout_column="1"/>

<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textClock"
android:layout_column="2"/>

</TableRow>

<TableRow>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIRST NAME"
android:layout_column="1"/>

<EditText
android:width="200px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</TableRow>

<TableRow>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LAST NAME"
android:layout_column="1"/>

<EditText
android:layout_width="wrap_content"
14
ANDROID MANUAL | Ms Swati Chavda

android:layout_height="wrap_content"
android:width="100px" />

</TableRow>

<TableRow

android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2" />

</TableRow>

<TableRow

android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="Submit" />

</TableRow>

</TableLayout>
15
ANDROID MANUAL | Ms Swati Chavda

Output:

16
ANDROID MANUAL | Ms Swati Chavda

Practical: 5 Write android program using relative and linear layout.


(Hint: no need to add java file)

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Linear Layout Demo !"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

17
ANDROID MANUAL | Ms Swati Chavda

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

<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>

<Button android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/>

<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>

</LinearLayout>

18
ANDROID MANUAL | Ms Swati Chavda

Output:

19
ANDROID MANUAL | Ms Swati Chavda

Practical:6 Create android application to demonstrate different widgets with


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

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Registration" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:-" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="name" >
<requestFocus />
</EditText>
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No:-" />
<EditText

20
ANDROID MANUAL | Ms Swati Chavda

android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="mobile" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="53dp"
android:text="Gender" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Female" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobbies" />
</TableRow>
<TableRow>
21
ANDROID MANUAL | Ms Swati Chavda

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Singing" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yoga" />
</TableRow>
<TableRow>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submit"
android:text="Submit" />
</TableRow>
</TableLayout>

22
ANDROID MANUAL | Ms Swati Chavda

Output:

23
ANDROID MANUAL | Ms Swati Chavda

Practical:7 Write a program to demonstrate explicit intent. Or demonstration of


multiple java and xml file.

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

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="159dp"
tools:layout_editor_absoluteY="352dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.multipleactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
24
ANDROID MANUAL | Ms Swati Chavda

public class MainActivity extends AppCompatActivity {


private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);

Output:

25
ANDROID MANUAL | Ms Swati Chavda

26
ANDROID MANUAL | Ms Swati Chavda

Practical: 8 Create simple login form.

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Name" />

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"

27
ANDROID MANUAL | Ms Swati Chavda

android:hint="Password" />

<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="submit" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.login2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText txtUserName;
EditText txtPassword;
Button btnLogin;
Button btnCancel;
final String id = "hello";
final String pass = "abc";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
28
ANDROID MANUAL | Ms Swati Chavda

setContentView(R.layout.activity_main);
txtUserName = this.findViewById(R.id.id);
txtPassword = this.findViewById(R.id.password);
btnLogin = (Button) this.findViewById(R.id.submit);
btnLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if((txtUserName.getText().toString()).equals(id)&&txtPassword.getText()
.toString().equals(pass)) {
Toast.makeText(MainActivity.this, "Login Successful",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Invalid Login",
Toast.LENGTH_LONG).show();
}
}
});
}}

29
ANDROID MANUAL | Ms Swati Chavda

Output:

30
ANDROID MANUAL | Ms Swati Chavda

Practical: 9 Write a program to demonstrate implicit intent


MainActivity.java
package com.example.implicitintent;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = (EditText)findViewById(R.id.urlText);
Button btn = (Button) findViewById(R.id.btnNavigate);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = editText.getText().toString();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
}
}

31
ANDROID MANUAL | Ms Swati Chavda

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.implicitintent.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/urlText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:ems="10" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnNavigate"
android:layout_below="@+id/urlText"
android:text="Navigate"
android:layout_centerHorizontal="true" />
</RelativeLayout>

Output:

32
ANDROID MANUAL | Ms Swati Chavda

33
ANDROID MANUAL | Ms Swati Chavda

34
ANDROID MANUAL | Ms Swati Chavda

Practical:10 Write a program to perform basic arithmetic operations


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"
android:padding="20dp"
android:orientation="vertical"
android:background="@color/purple_200">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATOR"
android:textSize="25sp"
android:layout_marginBottom="16dp"
android:textColor="@android:color/black" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="20dp">

<EditText
android:id="@+id/first_no"
android:layout_width="102dp"
android:layout_height="59dp"
android:ems="10"
android:layout_marginHorizontal="50dp"
android:hint="Enter" />

<EditText
android:id="@+id/second_no"
35
ANDROID MANUAL | Ms Swati Chavda

android:layout_width="102dp"
android:layout_height="59dp"
android:ems="10"
android:hint="Enter" />

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="20dp">

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_marginLeft="250dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp">

<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="25sp"
android:layout_marginBottom="16dp" />

<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="+"
android:textSize="25sp"
tools:ignore="OnClick" />
36
ANDROID MANUAL | Ms Swati Chavda

<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textSize="25sp"
android:layout_marginBottom="16dp" />

<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="X"
android:textSize="25sp"/>

<Button
android:id="@+id/equals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="="
android:textSize="35sp"/>
</LinearLayout>

<TextView
android:id="@+id/answer"
android:layout_width="268dp"
android:layout_height="62dp"
android:layout_marginHorizontal="50dp"
android:hint="ans"
android:textSize="35sp" />

</LinearLayout>

37
ANDROID MANUAL | Ms Swati Chavda

MainActivity.java

package com.example.arithmatic;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

EditText no1 , no2;


Button add ,mul ,div , sub,equal;
TextView answer;
double ans = 0;

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

// for text views


no1 = findViewById(R.id.first_no);
no2 = findViewById(R.id.second_no);

// for button with operations


add = findViewById(R.id.add);
mul = findViewById(R.id.mul);
div = findViewById(R.id.div);
sub = findViewById(R.id.sub);

38
ANDROID MANUAL | Ms Swati Chavda

// for equal to button


equal = findViewById(R.id.equals);

// for answer field


answer = findViewById(R.id.answer);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = no1.getText().toString();
String num2 = no2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(),"Enter
Numbers",Toast.LENGTH_SHORT).show();
}
else {
double a = Double.parseDouble(no1.getText().toString());
double b = Double.parseDouble(no2.getText().toString());
ans = a + b;
}
}
});

sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = no1.getText().toString();
String num2 = no2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(),"Enter
Numbers",Toast.LENGTH_SHORT).show();
}
else {
double a = Double.parseDouble(no1.getText().toString());
double b = Double.parseDouble(no2.getText().toString());
ans = a - b;
39
ANDROID MANUAL | Ms Swati Chavda

}
}
});

mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = no1.getText().toString();
String num2 = no2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(),"Enter
Numbers",Toast.LENGTH_SHORT).show();
}
else {
double a = Double.parseDouble(no1.getText().toString());
double b = Double.parseDouble(no2.getText().toString());
ans = a * b;
}
}
});

div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = no1.getText().toString();
String num2 = no2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(), "Enter Numbers",
Toast.LENGTH_SHORT).show();
} else {
double a = Double.parseDouble(no1.getText().toString());
double b = Double.parseDouble(no2.getText().toString());
if (b != 0)
ans = a / b;
else
Toast.makeText(getApplicationContext(), "Enter Valid Numbers",
40
ANDROID MANUAL | Ms Swati Chavda

Toast.LENGTH_SHORT).show();
}
}
});

equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ans1 = String.valueOf(ans);
answer.setText(ans1);
ans= 0;
}
});

}
}

41
ANDROID MANUAL | Ms Swati Chavda

Output:

42
ANDROID MANUAL | Ms Swati Chavda

43
ANDROID MANUAL | Ms Swati Chavda

Practical:11 write a program to display toast.


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="display toast message!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.toast;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
44
ANDROID MANUAL | Ms Swati Chavda

Toast.makeText(getApplicationContext(),"Hello students its Toast",


Toast.LENGTH_SHORT).show();
}
}
Output:

45
ANDROID MANUAL | Ms Swati Chavda

Practical:12 Write a program to demonstrate broadcast and receiver.

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Airplane mode demo"
android:textColorLink="@color/white"
android:textStyle="bold"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

MyReceiver.java
package com.example.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
46
ANDROID MANUAL | Ms Swati Chavda

import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

if (isAirplaneModeOn(context.getApplicationContext())) {
Toast.makeText(context, "AirPlane mode is on",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "AirPlane mode is off",
Toast.LENGTH_SHORT).show();
}
}

private static boolean isAirplaneModeOn(Context context) {


return Settings.System.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}

MainActivity.java
package com.example.receiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

MyReceiver airplaneModeChangeReceiver = new MyReceiver();

47
ANDROID MANUAL | Ms Swati Chavda

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

@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(airplaneModeChangeReceiver, filter);
}

@Override
protected void onStop() {
super.onStop();
unregisterReceiver(airplaneModeChangeReceiver);
}
}

48
ANDROID MANUAL | Ms Swati Chavda

Output:

49
ANDROID MANUAL | Ms Swati Chavda

Practical:13 Write a program to demonstrate alert dialog.


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Back Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

package com.example.alert;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
50
ANDROID MANUAL | Ms Swati Chavda

}
// Declare the onBackPressed method
// when the back button is pressed
// this method will call
@Override
public void onBackPressed()
{

// Create the object of


// AlertDialog Builder class
AlertDialog.Builder builder
= new AlertDialog
.Builder(MainActivity.this);

// Set the message show for the Alert time


builder.setMessage("Do you want to exit ?");

// Set Alert Title


builder.setTitle("Alert !");

// Set Cancelable false


// for when the user clicks on the outside
// the Dialog Box then it will remain show
builder.setCancelable(false);

// Set the positive button with yes name


// OnClickListener method is use of
// DialogInterface interface.

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

@Override
public void onClick(DialogInterface dialog,
int which)
51
ANDROID MANUAL | Ms Swati Chavda

// When the user click yes button


// then app will close
finish();
}
});

// Set the Negative button with No name


// OnClickListener method is use
// of DialogInterface interface.
builder
.setNegativeButton(
"No",
new DialogInterface
.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which)
{

// If user click no
// then dialog box is canceled.
dialog.cancel();
}
});

// Create the Alert dialog


AlertDialog alertDialog = builder.create();

// Show the Alert Dialog box


alertDialog.show();
}
}

52
ANDROID MANUAL | Ms Swati Chavda

Output:

53
Practical: 10 Write an android program for Event registration using an
Anonymous Inner Class.

MainActivity.java

package com.example.evntreg1;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//--- find both the buttons---


Button sButton = (Button) findViewById(R.id.button_s);
Button lButton = (Button) findViewById(R.id.button_l);

// -- register click event with first button ---


sButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// --- find the text view --
TextView txtView = (TextView) findViewById(R.id.text_id);

// -- change text size --


txtView.setTextSize(14);
}
});
// -- register click event with second button ---
lButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// --- find the text view --
TextView txtView = (TextView) findViewById(R.id.text_id);

// -- change text size --


txtView.setTextSize(24);
}
});

}
}

Activity_main.xml

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


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

<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.514" />
<Button
android:id="@+id/button_s"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="small text"
app:layout_constraintBottom_toTopOf="@+id/button_l"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button_l"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="120dp"
android:text="large text"
app:layout_constraintBottom_toTopOf="@+id/text_id"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Output:
Practical: 11 Write an android program for Event registration using Listener
interface.

MainActivity.java

package com.example. evntreg2;


import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener

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

//--- find both the buttons---


Button sButton = (Button) findViewById(R.id.button_s);
Button lButton = (Button) findViewById(R.id.button_l);

// -- register click event with first button ---


sButton.setOnClickListener(this);

// -- register click event with second button ---


lButton.setOnClickListener(this);
}

//--- Implement the OnClickListener callback

public void onClick(View v)


{

if(v.getId() == R.id.button_s)
{
// --- find the text view --
TextView txtView = (TextView) findViewById(R.id.text_id);

// -- change text size --


txtView.setTextSize(20);
return;
}
if(v.getId() == R.id.button_l)
{
// --- find the text view --
TextView txtView = (TextView) findViewById(R.id.text_id);

// -- change text size --


txtView.setTextSize(40);
return;
}
}

Activity_main.xml

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


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

<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.514" />

<Button
android:id="@+id/button_s"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="small text"
app:layout_constraintBottom_toTopOf="@+id/button_l"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button_l"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="120dp"
android:text="large text"
app:layout_constraintBottom_toTopOf="@+id/text_id"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Output:
Practical: 12 Write an android program for Event registration using Layout
File

MainActivity.java

package com.example.evntreg3;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

//--- Implement the event handler for the first button.

public void doSmall(View v)


{
// --- find the text view --
TextView txtView = (TextView) findViewById(R.id.text_id);

// -- change text size --


txtView.setTextSize(30);
return;
}

//--- Implement the event handler for the second button.


public void doLarge(View v)
{
// --- find the text view --

TextView txtView = (TextView) findViewById(R.id.text_id);


// -- change text size –
txtView.setTextSize(50);
return;
}

Activity_main.xml

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


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

<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.514" />

<Button
android:id="@+id/button_s"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="small text"
app:layout_constraintBottom_toTopOf="@+id/button_l"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="doSmall"/>

<Button
android:id="@+id/button_l"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="120dp"
android:text="large text"
app:layout_constraintBottom_toTopOf="@+id/text_id"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
android:onClick="doLarge"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Output:

You might also like