You are on page 1of 77

Practical No: 22

Develop a program to implement sensors.


Exercise:
1. Write a program to changes the background colour when device is shuffled
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android
"xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACTION_POWER_CONNECTED" />
<uses-permission android:name="android.permission.ACTION_POWER_DISCONNECTED" />
<uses-permission android:name="android.permission.BATTERY_CHANGED" />
<uses-permission android:name="android.permission.ACTION_SHUTDOWN" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rule
s" android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="Practical_21"
android:supportsRtl="true"
android:theme="@style/Theme.Practical_15"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


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

.java file
package com.example.practical_15;

import
android.content.BroadcastReceiver
;import android.content.Context;
import
android.content.Intent;
import
android.content.IntentFilter
;import
android.graphics.Color;
import android.os.Bundle;
import
android.widget.LinearLayout
;import
android.widget.TextView;
import
android.widget.Toast;
import
androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends
AppCompatActivity {
private LinearLayout
mainLayout;private TextView
textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
)
mainLayout = (LinearLayout) findViewById(R.id.main_layout);
textView = (TextView) findViewById(R.id.textview);
registerReceiver(shutdownReceiver,
new IntentFilter(Intent.ACTION_SHUTDOWN));
}
private BroadcastReceiver shutdownReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

Toast.makeText(context, "Device shuffled!",


Toast.LENGTH_LONG).show();
mainLayout.setBackgroundColor(Color.rgb(255, 0, 0));
textView.setText("Device shuffled! Background
color changed.");
}
};
}
.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginLeft="15dp"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:textColor="#171717"
android:layout_marginRight="15dp"
android:text="Shuffle your device
to change the background color"
/>

</LinearLayout>
Output:
lOM oA R c P S D| 35 59 439 5

Practical No: 21
Develop a program to implement broadcast receiver.
Exercise:
1. Write a program to demonstrate all the system broadcast messages.

AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/andr
oid"xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACTION_POWER_CONNECTED" />
<uses-permission
android:name="android.permission.ACTION_POWER_DISCONNECTED"/>
<uses-permission android:name="android.permission.BATTERY_CHANGED" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_ru
les" android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="Practical_21"
android:supportsRtl="true"
android:theme="@style/Theme.Practical_15"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


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

.java file
package com.example.practical_15;

import
android.content.BroadcastRec
eiver;import
android.content.Context;
import
android.content.Intent;
import
android.content.IntentF
ilter;import
android.os.BatteryManag
er; import
android.os.Bundle;
import android.widget.TextView;
import
androidx.appcompat.app.AppCompatActivity
; public class MainActivity extends
AppCompatActivity {
private TextView
textView;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
l
O
M
o
A
R
c
P
S
D
|
3
5
5
9
4
3
9
5

setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
// Register broadcast receivers for various system broadcasts
registerReceiver(ne
w BootReceiver(), new
IntentFilter(Intent.ACTION_
BOOT_COMPLETED));
registerReceiver(new
PowerConnectedReceiver(), new
IntentFilter(Intent.ACTION_POWER_CONNECTED));
registerReceiver(new
PowerDisconnectedReceiver(), new
IntentFilter(Intent.ACTION_POWER_DISCONNECTED));
registerReceiver(new
BatteryChangedReceiver(), new
IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{String message = "System boot
completed";
textView.setText(message);}
}
private class PowerConnectedReceiver
extendsBroadcastReceiver
{@Override
public void onReceive(Context context, Intent intent)
{String message = "Power
connected";
textView.setText(message);}
}
private class PowerDisconnectedReceiver
extendsBroadcastReceiver {@Override
public void onReceive(Context context, Intent intent)
{String message = "Power
disconnected";
textView.setText(message);}
}
private class BatteryChangedReceiver
extendsBroadcastReceiver
{@Override
public void onReceive(Context context, Intent
intent) { int level =
intent.getIntExtra(BatteryManager.EXTRA_LEVEL,
0);String message = "Battery level: " + level;
textView.setText(message);}
}
}

.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/and
roid"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_
content"
android:layout_height="wrap_
content"
android:textSize="24sp"
android:textStyle="bold"
android:layout_centerInPare
nt="true"
android:textColor="#B02D
C6"
/>
</RelativeLayout>

Output:
M oA Rc P S D| 35 59 439 5
Practical No. 10 : Write a program to create login form for student registration

system.

1. Write a program to create login form for student registration system.

activity_main.xml code :

<?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:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Student Registration System"
android:textSize="24sp"

<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" android:inputType="text"
android:padding="8dp" android:layout_marginTop="16dp"/>

<EditText

android:id="@+id/Enroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enrollment No.: " android:inputType="text"
android:padding="8dp" android:layout_marginTop="16dp"/>

<EditText

android:id="@+id/CRN"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="CRN
No.: " android:inputType="text" android:padding="8dp"
android:layout_marginTop="16dp"/>
<EditText
android:id="@+id/cla" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="class :
" android:inputType="text" android:padding="8dp"
android:layout_marginTop="16dp"/>

<EditText
android:id="@+id/Roll" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Roll No
: " android:inputType="text" android:padding="8dp"
android:layout_marginTop="16dp"/>

<EditText
android:id="@+id/dept" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Department: " android:inputType="text"
android:padding="8dp" android:layout_marginTop="16dp"/>

<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password" android:inputType="textPassword"
android:padding="8dp" android:layout_marginTop="16dp"/>

<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Login"
android:textSize="18sp" android:layout_marginTop="16dp"/>

</LinearLayout>
MainActivity.java code:

package com.example.pr_10_student_registration_system; import

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


import android.view.View; import
android.widget.Button; import
android.widget.EditText; import
android.widget.Toast;

public class MainActivity extends AppCompatActivity { @Override


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

EditText usernameEditText = findViewById(R.id.editTextUsername); EditText


passwordEditText = findViewById(R.id.editTextPassword); EditText Enroll =
findViewById(R.id.Enroll);
EditText CRN = findViewById(R.id.CRN); EditText cla =
findViewById(R.id.cla); EditText Roll =
findViewById(R.id.Roll); EditText dept =
findViewById(R.id.dept);
Button loginButton = findViewById(R.id.buttonLogin);

loginButton.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View v) {

String username = usernameEditText.getText().toString(); String password =


passwordEditText.getText().toString();

if (username.equals("Ajinkya Patil") && password.equals("Student Password"))


{

if (Enroll.equals("2000780314") && CRN.equals("S0220031055") &&


cla.equals("TYCM-II") && Roll.equals("39") && dept.equals("CM")){

Toast.makeText(getApplicationContext(), "Username and Password


and student Details are correct !", Toast.LENGTH_SHORT).show();

}
else {

Toast.makeText(getApplicationContext(), "Student Details are not correct


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

} else {
Toast.makeText(getApplicationContext(), "Invalid username or password",
Toast.LENGTH_SHORT).show();
}
}

});

}
}
Output:
lOM oA R c P S D| 35 59 439 5

lOM oA R c P S D| 35 59 439 5

practical no:23

X. Exercise
1. Write a program to capture an image and display it using image view.

activity_main.xml </LinearLayout>

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


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

xmlns:app="http://schemas.android.com/apk/r
es-auto"
xmlns:tools="http://schemas.android.com/tools

"android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical"

tools:context=".MainActivity">

<ImageView

android:id="@+id/click_image"

android:layout_width="match_parent"

android:layout_height="530dp"

android:layout_marginLeft="0dp"

android:layout_marginTop="70dp"

android:layout_marginBottom="10dp"

/>

<Button

android:id="@+id/camera_button"

android:layout_width="100dp"

android:layout_height="50dp"

android:layout_gravity="center"

android:gravity="center"

android:text="Camera" />
MainActivity.java
package com.example.ex2301;

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

import android.graphics.Bitmap;

import

android.provider.MediaStore;

import android.view.View;

import android.widget.Button;

import

android.widget.ImageView;

import android.os.Bundle;

public class MainActivity


extendsAppCompatActivity {

private static final int pic_id = 123;

Button camera_open_id;

ImageView click_image_id;

@Override
protected void
onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

camera_open_id

=
(Button)findViewById(R.id.camera_button);
click_image_id

=
(ImageView)findViewById(R.id.click_image);

camera_open_id.setOnClickListener(new
lO M o AR cP S D| 3 5 5 9 4 3 9 5

@Override
public void onClick(View v)
{

Intent camera_intent
= new Intent(MediaStore
.ACTION_IMAGE_CAPTURE);

startActivityForResult(camera_intent, pic_id);
}
});
}

protected void onActivityResult(int requestCode,


int resultCode,

Intent data) {

super.onActivityResult(requestCode, resultCode,

data);if (requestCode == pic_id) {

Bitmap photo = (Bitmap) data.getExtras().get("data");

click_image_id.setImageBitmap(photo);
}
}
}
lOM oA R c P S D| 35 59 439 5

Output:
cP S D| 355 94 395
lOM oA R c P S D| 35 59 439 5

Practical No: 18
Develop a program to implement new activity using explicit intent and implicit intent.
Exercise:
1. Write a program to create a create text field and a button “Navigate”. When you enter
www.google.com and press navigate it should open google page.
.java file
package com.example.practical_15;

import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button mSearchButton;
private EditText mEnterText;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSearchButton = (Button) findViewById(R.id.searchbutton);
mEnterText = (EditText) findViewById(R.id.enteredtext);
mSearchButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String etxt = mEnterText.getText().toString();
if(!etxt.isEmpty()) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,etxt);
startActivity(intent);
}
else {
Toast.makeText(MainActivity.this,"Not entered
anything",Toast.LENGTH_LONG).show();
}
}
});
}
}

.xml file
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Search here"
android:id="@+id/enteredtext"
lOM oA R c P S D| 35 59 439 5

android:textColor="@color/black"
android:layout_marginTop="150dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:paddingLeft="20dp"
android:background="@drawable/shape"
/>

<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/searchbutton"
android:text="SEARCH"
android:layout_marginTop="30dp"
android:layout_below="@+id/enteredtext"
android:layout_gravity="center"
/>
</LinearLayout>

Output:
Practical No.4
1. Write a program to display HelloWorld.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

O/P:-
2. Write a program to display student name and marks.

<?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"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/student_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Atharva Agrawal" />
<TextView
android:id="@+id/student_marks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/student_name"
android:layout_centerInParent="true"
android:paddingLeft="16dp"
android:text="99" />
</RelativeLayout>

O/P
Practical No.5
1. Write a program to place Name, Age and mobile number linearly (Vertical) on the display
screen using Linear layout.

<?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:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/student_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile Number:" />
</LinearLayout>

O/P
2. Write a program to place Name, Age and mobile number centrally on the display screen
using Absolute layout.

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


<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/student_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="125dp"
android:layout_y="280dp"
android:text="Name:"
android:textColor="#86AD33"
android:textSize="20dp"
android:textStyle="bold" /
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="125dp"
android:layout_y="304dp"
android:text="Age:" O/P
android:textColor="#86AD33"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="125dp"
android:layout_y="328dp"
android:text="Mobile Number:"
android:textColor="#86AD33"
android:textSize="20dp"
android:textStyle="bold" />
</AbsoluteLayout>
Practical No.6
1. Write a program to display 10 students basic information in a table form using Table layout.

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


<TableLayout 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:collapseColumns="*"
android:shrinkColumns="*"
tools:context=".MainActivity">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="10 Students Basic Information"
android:textColor="#86AD33"
android:textSize="20dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Student Numbers"
android:textColor="#000"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Name"
android:textColor="#000"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="RollNo"
android:textColor="#000"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Age"

android:textColor="#000"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Student 1:"
android:textColor="#86AD33"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Student 2:"
android:textColor="#86AD33"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"

android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Student 3:"
android:textColor="#86AD33"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Student 4:"
android:textColor="#86AD33"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Student 5:"
android:textColor="#86AD33"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Student 6:"
android:textColor="#86AD33"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Student 7:"
android:textColor="#86AD33"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Student 8:"
android:textColor="#86AD33"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Student 9:"
android:textColor="#86AD33"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Student 10:"
android:textColor="#86AD33"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
O/P
2. Write a program to display all the data types in object-oriented programming
using Frame layout.

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


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="66dp"
android:text="Data Types in Object Oriented Programming"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/pd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left|fill_vertical"
android:layout_marginTop="80dp"
android:text="Primitive"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="110dp"
android:foregroundGravity="fill_horizontal|top"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1) Integer" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2) Float" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3) Characters" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4) Boolean" />
</LinearLayout>
<TextView
android:layout_toRightOf="@+id/pd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|fill_vertical"
android:layout_marginTop="80dp"
android:text="Non-Primitive"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" O/P
android:layout_gravity="right"
android:layout_marginTop="110dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1) Class" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2) Array" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3) Interface" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4) Object" />
</LinearLayout>
</FrameLayout>
Practical No.7
1. Write a program to accept username and password from the end user using Text View and
Edit Text.
<div style="white-space: normal; height: auto; visibility: visible; font-size: 14px;">
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="50dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="25dp"
android:text="Login Page" />
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Enter UserName:" />
<EditText
android:id="@+id/user"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="abc@gmail.com" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Enter Password:" />
<EditText
android:id="@+id/pass"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="1234" />
</TableRow>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="172dp"
android:text="Login" />
</TableLayout></div>

O/P
Practical No.12
1. Write a program to show the following output. First two radio buttons are without using radio
group and next two radio buttons are using radio group. Note the changes between these two.
Also toast which radio button has been selected.

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:padding="10dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Single Radio Buttons"
android:textSize="25dp" />
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Radio Button inside RadioGroup"
android:paddingTop="20dp"
android:textSize="25dp" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rbmale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/rbfemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="Show Selected"
android:textSize="25dp" />
</LinearLayout>
MainActivity.java:
package com.blogspot.codingatharva.manualprograms;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioButton rb1, rb2, rg1;
RadioGroup rg;
Button b;
StringBuffer sb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb = new StringBuffer("You Selected: ");
b = findViewById(R.id.btn);
rb1 = findViewById(R.id.rb1);
rb2 = findViewById(R.id.rb2);
rg = findViewById(R.id.rg);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rg1 = findViewById(rg.getCheckedRadioButtonId());
if (rb1.isChecked())
sb.append("\n"+rb1.getText());
if (rb2.isChecked())
sb.append("\n"+rb2.getText());
if (rg1.isChecked())
sb.append("\n"+rg1.getText());
Toast.makeText(getApplicationContext(), sb, Toast.LENGTH_SHORT).show();
sb.delete(13, sb.length() - 1);
}
});
}
}
Practical No.13
1. Write a program to display circular progress bar.
activity_main.xml:
<div style="white-space: normal; height: auto; visibility: visible; font-size: 14px;">
<?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"
android:layout_margin= "16dp"
tools:context= ".MainActivity" >
<ProgressBar
android:id= "@+id/progressBar"
style= "?android:attr/progressBarStyleHorizontal"
android:layout_width= "200dp"
android:layout_height= "200dp"
android:layout_centerInParent= "true"
android:background= "@drawable/circular_shape"
android:indeterminate= "false"
android:max= "100"
android:progress= "0"
android:progressDrawable= "@drawable/circular_progress_bar" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_below="@+id/progressBar"
android:text="Start"/>
</RelativeLayout></div>

MainActivity.java:
package com.blogspot.codingatharva.manualprograms;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
Button b;
ProgressBar pb;
private int progressStatus = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = findViewById(R.id.btn);
pb = findViewById(R.id.progressBar);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startProgress(v);
}
});
}
public void startProgress(View view) {
pb.setProgress(0);
new Thread(new Task()).start();
}
class Task implements Runnable {
@Override
public void run() {
for (int i = 0; i <= 10; i++) {
final int value = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pb.setProgress(value);
}
}
}
}

O/P
Practical No.14
Write a program to display 15 buttons using grid view.

activity_main.xml:
<?xml version= "1.0" encoding= "utf-8" ?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gridview"
android:layout_width="fill_parent" O/P
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
tools:context=".MainActivity"></GridView>

MainActivity.java:
package com.blogspot.codingatharva.manualprograms;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
GridView gridview;
String arr[] = new String[15];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = findViewById(R.id.gridview);
for (int i = 0; i < 15; i++) {
arr[i] = Integer.toString(i + 1);
}
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.activity_listview, R.id.btn, arr);
gridview.setAdapter(ad);
}
}
Write a program to display a text view using vertical scroll view.

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=".MainActivity">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Vertical ScrollView example In computer displays, filmmaking, television
production, and other kinetic \n
displays, scrolling is sliding text, images or video across a monitor or display, vertically or
horizontally. Scrolling \n,
as such, does not change the layout of the text or pictures but moves (pans or tilts) the user's
view across what is \n
apparently a larger image that is not wholly seen.[1] A common television and movie special
effect is to scroll credits, \n
while leaving the background stationary. Scrolling may take place completely without user
intervention (as in film credits) \n
or, on an interactive device, be triggered by touchscreen or a keypress and continue without
further intervention until a \n
further user action, or be entirely controlled by input devices. Scrolling may take place in discrete
increments \n
(perhaps one or a few lines of text at a time), or continuously (smooth scrolling). Frame rate is the
speed at which an \n
entire image is redisplayed. It is related to scrolling in that changes to text and image position can
only happen as often \n
as the image can be redisplayed. When frame rate is a limiting factor, one smooth scrolling
technique is to blur images during \n
movement that would otherwise appear to jump \b
Scrolling texts, also referred to as scrolltexts or scrollers, played an important part in the birth of
the computer demo culture.
The software crackers often used their deep knowledge of computer platforms to transform the
information that accompanied their releases
into crack intros. The sole role of these intros was to scroll the text on the screen in an impressive
way.
Many scrollers were plain horizontal scrollers, but demo coders also paid a lot of attention to
creating new and different types of scrolling.
The characters could, for example, continuously alter their shape, take unusual flying paths or
incorporate color effects such as raster bars.
Sometimes it makes the text nearly unreadable.
" />
</ScrollView>
</RelativeLayout>

MainActivity.java:
package com.blogspot.codingatharva.manualprograms;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

O/P
Practical No.15
1. Write a program to display following toast message.
<?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/button"
android:layout_width="143dp"
android:layout_height="73dp"
android:text="Click Here"
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" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:

package com.blogspot.codingatharva.toastprogram;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import
android.view.View; import
android.widget.Button;import
android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button button = findViewById(R.id.button);


button.setOnClickListener(new View.OnClickListener() {public void

onClick(View v) {

Toast.makeText(getApplicationContext(), "Welcome at CodingAtharvs",


Toast.LENGTH_SHORT).show();
}
});
}
}
Practical NO16. Develop a Program to implement Date and Time Picker

MainActivity.java:
package com.blogspot.codingatharva.manualprograms;import
android.os.Bundle;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity; public class MainActivity
extends AppCompatActivity {
TimePicker tp;
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tp = findViewById(R.id.tp1);
tp.setIs24HourView(true);
}
}

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=".MainActivity">
<TextView
android:id="@+id/tv" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingBottom="20dp" android:text="TimePicker"
android:textSize="30dp" />

<TimePicker

android:id="@+id/tp1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv"
android:timePickerMode="spinner" />

<TimePicker

android:id="@+id/tp2" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tp1" android:timePickerMode="clock"
/>
</RelativeLayout>
PRACTICAL NO 17. PROGRAM TO CREATE A HELLO WORLD ACTIVITY USING
ALL LIFE CYCLES METHOD TO DISPLAY MESSAGES USING LOG.D IN ANDROID
STUDIO

MainActivity.java:
package com.blogspot.codingatharva.manualprograms;import
android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity; public class MainActivity
extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Log.d("Activity:",
"Created");
}
@Override
protected void onStart() { super.onStart();
Log.d("Activity:", "Started");
}
@Override
protected void onResume() { super.onResume();
Log.d("Activity:", "Resume");
}
@Override
protected void onPause() {
super.onPause();

Log.d("Activity:", "Pause");
}
@Override
protected void onStop() { super.onStop();
Log.d("Activity:", "Stop");
}
@Override
protected void onRestart() { super.onRestart();
Log.d("Activity:", "Restart");
}
@Override
protected void onDestroy() { super.onDestroy();
Log.d("Activity:", "Destroy");
}
}
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"
android:paddingTop="80dp"
tools:context=".MainActivity">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"/>

</RelativeLayout
Practical No.26: write a program to insert data in sqlite database using
asynctask
Practical No 27: write a program to create the login form and display login successful/unsuccrssful
toastmessage.

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="com.onlinetutorialspoint.official.simplelogin.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_centerInParent="true">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"android:hint="UserName"
android:id="@+id/username"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password" android:id="@+id/password"
android:inputType="textPassword"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"android:text="Login"
android:background="#3f76ff" android:textColor="#fff"
android:id="@+id/login"/>

</LinearLayout>

</RelativeLayout>
MainActivity.java

package com.onlinetutorialspoint.official.simplelogin;

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;


import android.view.View; import
android.widget.Button; import
android.widget.EditText;import
android.widget.Toast;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {EditText username,password;


Button login;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); username=findViewById(R.id.username);
password=findViewById(R.id.password); login=findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(Objects.equals(username.getText().toString(),
"admin")&&Objects.equals(password.getText().toString(),"admin"))
{
Toast.makeText(MainActivity.this,"You haveAuthenticated
Successfully",Toast.LENGTH_LONG).show();

}else
{

Toast.makeText(MainActivity.this,"AuthenticationFailed",Toast.LENGTH_LONG).show();
}

}
});

}
}
Giving invalid credentials:
Practical No 9: write a program to create a simple calculator in android
<?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="#8BC34A"
android:backgroundTint="@android:color/darker_gray"
tools:context=".MainActivity">

<!-- Text View to display our basic heading of "calculator"-->

<TextView
android:layout_width="194dp"
android:layout_height="43dp"
android:layout_marginStart="114dp"
android:layout_marginLeft="114dp"
android:layout_marginTop="58dp"
android:layout_marginEnd="103dp"
android:layout_marginRight="103dp"
android:layout_marginBottom="502dp"
android:scrollbarSize="30dp"
android:text=" Calculator"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText

android:id="@+id/num1"
android:layout_width="364dp"
android:layout_height="28dp"
android:layout_marginStart="72dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="71dp"
android:layout_marginBottom="416dp"
android:background="@android:color/white"
android:ems="10"
android:onClick="clearTextNum1"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /
<EditText

android:id="@+id/num2"
android:layout_width="363dp"
android:layout_height="30dp"
android:layout_marginStart="72dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="71dp"
android:layout_marginBottom="374dp"
android:background="@android:color/white"
android:ems="10"
android:onClick="clearTextNum2"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView

android:id="@+id/result"
android:layout_width="356dp"
android:layout_height="71dp"
android:layout_marginStart="41dp"
android:layout_marginTop="151dp"
android:layout_marginEnd="48dp"
android:layout_marginBottom="287dp"
android:background="@android:color/white"
android:text="result"
android:textColorLink="#673AB7"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button

android:id="@+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="307dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light
android:onClick="doSum"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button

android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="210dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="113dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doSub"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.507" />

<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="307dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doDiv"
android:text="/"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="356dp"
android:layout_marginEnd="307dp"
android:layout_marginBottom="199dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doMul"
android:text="x"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="103dp"
android:layout_height="46dp"
android:layout_marginStart="113dp"
android:layout_marginTop="356dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="356dp"
android:layout_marginEnd="307dp"
android:layout_marginBottom="199dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doMul"
android:text="x"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="103dp"
android:layout_height="46dp"
android:layout_marginStart="113dp"
android:layout_marginTop="356dp"
android:layout_marginEnd="206dp"
android:layout_marginBottom="199dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doMod"
android:text="%(mod)"
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.515" />

<Button
android:id="@+id/pow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="113dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="210dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doPow"
android:text="n1^n2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.507" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java

package com.example.calculator2;
import android.os.Bundle;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.calculator2.databinding.ActivityMainBinding;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {

private AppBarConfiguration appBarConfiguration;


private ActivityMainBinding binding;
public EditText e1, e2;
TextView t1;
int num1, num2;
public boolean getNumbers() {
//checkAndClear();
// defining the edit text 1 to e1
e1 = (EditText) findViewById(R.id.num1);
// defining the edit text 2 to e2
e2 = (EditText) findViewById(R.id.num2);
// defining the text view to t1
t1 = (TextView) findViewById(R.id.result);
// taking input from text box 1
String s1 = e1.getText().toString();
// taking input from text box 2
String s2 = e2.getText().toString();
if(s1.equals("Please enter value 1") && s2.equals(null))
{
String result = "Please enter value 2";
e2.setText(result);
return false;
}
if(s1.equals(null) && s2.equals("Please enter value 2"))
{
String result = "Please enter value 1";
e1.setText(result);
return false;
}
if(s1.equals("Please enter value 1") || s2.equals("Please enter value 2"))
{
return false;
}
if((!s1.equals(null) && s2.equals(null))|| (!s1.equals("") && s2.equals("")) ){
String result = "Please enter value 2";
e2.setText(result);

return false;
}
if((s1.equals(null) && !s2.equals(null))|| (s1.equals("") && !s2.equals("")) ){
//checkAndClear();
String result = "Please enter value 1";
e1.setText(result);
return false;
}
if((s1.equals(null) && s2.equals(null))|| (s1.equals("") && s2.equals("")) ){
//checkAndClear();
String result1 = "Please enter value 1";
e1.setText(result1);
String result2 = "Please enter value 2";
e2.setText(result2);
return false;
}
else {
// converting string to int.
num1 = Integer.parseInt(s1);
// converting string to int.
num2 = Integer.parseInt(s2);
}
return true;

}
public void doSum(View v) {
// get the input numbers
if (getNumbers()) {
int sum = num1 + num2;
t1.setText(Integer.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}
public void clearTextNum1(View v) {
// get the input numbers
e1.getText().clear();
}
public void clearTextNum2(View v) {
// get the input numbers
e2.getText().clear();
}
public void doPow(View v) {
//checkAndClear();
// get the input numbers
if (getNumbers()) {

double sum = Math.pow(num1, num2);

}
t1.setText(Double.toString(sum));
else

t1.setText("Error Please enter Required Values");

}
// a public method to perform subtraction
public void doSub(View v) {
//checkAndClear();
// get the input numbers
if (getNumbers()) {
int sum = num1 - num2;
t1.setText(Integer.toString(sum));

else

t1.setText("Error Please enter Required Values");

// a public method to perform multiplication


public void doMul(View v) {

// get the input numbers


if (getNumbers()) {
int sum = num1 * num2;
t1.setText(Integer.toString(sum));

else

t1.setText("Error Please enter Required Values");

}
// a public method to perform Division
public void doDiv(View v) {
//checkAndClear();
// get the input numbers
if (getNumbers()) {
// displaying the text in text view assigned as t1
double sum = num1 / (num2 * 1.0);
t1.setText(Double.toString(sum));

else

t1.setText("Error Please enter Required Values");

// a public method to perform modulus function


public void doMod(View v) {
//checkAndClear();
// get the input numbers
if (getNumbers()) {
double sum = num1 % num2;
t1.setText(Double.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");

}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.num1);
// defining the edit text 2 to e2
e2 = (EditText) findViewById(R.id.num2);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override

public boolean onSupportNavigateUp() {


NavController navController = Navigation.findNavController(this,
R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
}
Practical No 24: write a program to turn on get visible list devices and turn off bluetooth in
android

activity_main.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"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:id="@+id/btnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn
On" android:layout_marginLeft="100dp" android:layout_marginTop="200dp" />
<Button
android:id="@+id/btnOFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnOn"
android:layout_toRightOf="@+id/btnOn"
android:text="Turn OFF" />
</RelativeLayout>

MainActivity.java:
package com.tutlane.bluetoothexample;
import android.bluetooth.BluetoothAdapter;
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.Toast;
public class MainActivity extends AppCompatActivity {
@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btntOn = (Button)findViewById(R.id.btnOn);
Button btntOff = (Button)findViewById(R.id.btnOFF);
final BluetoothAdapter bAdapter =
BluetoothAdapter.getDefaultAdapter();
btntOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(bAdapter == null)
{
Toast.makeText(getApplicationContext(),"Bluetooth Not
Supported",Toast.LENGTH_SHORT).show();
}
else{
if(!bAdapter.isEnabled()){
startActivityForResult(new Intent(BluetoothAdapter.ACT
ION_REQUEST_ENABLE),1);
Toast.makeText(getApplicationContext(),"Bluetooth
Turned ON",Toast.LENGTH_SHORT).show();
}
}
}
});
btntOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bAdapter.disable();
Toast.makeText(getApplicationContext(),"Bluetooth Turned
OFF", Toast.LENGTH_SHORT).show();
}
});
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.tutlane.bluetoothexample">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
Output of Android Bluetooth Turn ON / OFF Example:
Practical No 30: Write a program to send email
<?xml version="1.0" encoding="utf-8"?>

<!-- Relative Layout -->

<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=".MainActivity">

<!-- Edit text for email id -->

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="18dp"
android:layout_marginRight="22dp" />

<!-- Edit text for email subject -->

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_alignLeft="@+id/editText1"
android:layout_marginTop="20dp" />

<!-- Edit text for email body -->

<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_marginTop="30dp" />

<!-- text Views for label -->


<TextView

android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true" android:text="Send
To:" android:textColor="#0F9D58" />
<TextView

android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true" android:text="Email
Subject:" android:textColor="#0F9D58" />
<TextView

android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
android:text="Email Body:" android:textColor="#0F9D58" />

<!-- Button to send email -->

<Button

android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_alignLeft="@+id/editText3"

<TextView

android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true" android:text="Email
Subject:" android:textColor="#0F9D58" />
<TextView

android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
android:text="Email Body:" android:textColor="#0F9D58" />
<!-- Button to send email -->

<Button

android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_alignLeft="@+id/editText3"
android:layout_marginLeft="76dp"
android:layout_marginTop="20dp"
android:text="Send email!!" />
</RelativeLayout>
MainActivity Java:

import android.content.Intent;
import android.os.Bundle; import
android.widget.Button; import
android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity; public
class MainActivity extends AppCompatActivity {
// define objects for edit text and button
Button button;
EditText sendto, subject, body;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting instance of edittext and button
sendto = findViewById(R.id.editText1); subject
= findViewById(R.id.editText2); body =
findViewById(R.id.editText3); button =
findViewById(R.id.button);

// attach setOnClickListener to button with Intent object define in itbutton.setOnClickListener(view ->


{
String emailsend = sendto.getText().toString(); String
emailsubject = subject.getText().toString();String
emailbody = body.getText().toString();

// define Intent object with action attribute as ACTION_SENDIntent intent


= new Intent(Intent.ACTION_SEND);

// add three fields to intent using putExtra function intent.putExtra(Intent.EXTRA_EMAIL, new


String[]{emailsend});intent.putExtra(Intent.EXTRA_SUBJECT, emailsubject);
intent.putExtra(Intent.EXTRA_TEXT, emailbody);

// set type of intent


intent.setType("message/rfc822");

// startActivity with intent with chooser as Email client usingcreateChooser


function

startActivity(Intent.createChooser(intent, "Choose an Email client :"));

});

}
Practical No 32: Write a program To draw a route between two locations.

public class MapsActivity extends FragmentActivity implements


OnMapReadyCallback {

private GoogleMap mMap;


ArrayList markerPoints= new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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;
LatLng sydney = new LatLng(-34, 151);
//mMap.addMarker(new
MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 16));

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {

if (markerPoints.size() > 1) {
markerPoints.clear();
mMap.clear();
}

// Adding new item to the ArrayList


markerPoints.add(latLng);

// Creating MarkerOptions
MarkerOptions options = new MarkerOptions();

// Setting the position of the marker


options.position(latLng);

if (markerPoints.size() == 1) {

options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory
.HUE_GREEN));
} else if (markerPoints.size() == 2) {

options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory
.HUE_RED));
}

// Add new marker to the Google Map Android API V2


mMap.addMarker(options);

// Checks, whether start and end locations are captured


if (markerPoints.size() >= 2) {
LatLng origin = (LatLng) markerPoints.get(0);
LatLng dest = (LatLng) markerPoints.get(1);

// Getting URL to the Google Directions API


String url = getDirectionsUrl(origin, dest);

DownloadTask downloadTask = new DownloadTask();

// Start downloading json data from Google Directions


API
downloadTask.execute(url);
}

}
});

private class DownloadTask extends AsyncTask {

@Override
protected String doInBackground(String... url) {

String data = "";

try {
data = downloadUrl(url[0]);
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

ParserTask parserTask = new ParserTask();

parserTask.execute(result);

}
}

private class ParserTask extends AsyncTask<String, Integer,


List<List<HashMap>>> {
// Parsing the data in non-ui thread
@Override
protected List<List<HashMap>> doInBackground(String... jsonData) {

JSONObject jObject;
List<List<HashMap>> routes = null;

try {
jObject = new JSONObject(jsonData[0]);
DirectionsJSONParser parser = new DirectionsJSONParser();

routes = parser.parse(jObject);
} catch (Exception e) {
e.printStackTrace();
}
return routes;
}

@Override
protected void onPostExecute(List<List<HashMap>> result) {
ArrayList points = null;
PolylineOptions lineOptions = null;
MarkerOptions markerOptions = new MarkerOptions();

for (int i = 0; i < result.size(); i++) {


points = new ArrayList();
lineOptions = new PolylineOptions();

List<HashMap> path = result.get(i);

for (int j = 0; j < path.size(); j++) {


HashMap point = path.get(j);

double lat = Double.parseDouble(point.get("lat"));


double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);

points.add(position);
}

lineOptions.addAll(points);
lineOptions.width(12);
lineOptions.color(Color.RED);
lineOptions.geodesic(true);

// Drawing polyline in the Google Map for the i-th route


mMap.addPolyline(lineOptions);
}
}

private String getDirectionsUrl(LatLng origin, LatLng dest) {


// Origin of route
String str_origin = "origin=" + origin.latitude + "," +
origin.longitude;

// Destination of route
String str_dest = "destination=" + dest.latitude + "," +
dest.longitude;

// Sensor enabled
String sensor = "sensor=false";
String mode = "mode=driving";

// Building the parameters to the web service


String parameters = str_origin + "&" + str_dest + "&" + sensor +
"&" + mode;

// Output format
String output = "json";

// Building the url to the web service


String url = "https://maps.googleapis.com/maps/api/directions/" +
output + "?" + parameters;

return url;
}

private String downloadUrl(String strUrl) throws IOException {


String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);

urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.connect();

iStream = urlConnection.getInputStream();

BufferedReader br = new BufferedReader(new


InputStreamReader(iStream));

StringBuffer sb = new StringBuffer();

String line = "";


while ((line = br.readLine()) != null) {
sb.append(line);
}

data = sb.toString();

br.close();
} catch (Exception e) {
Log.d("Exception", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
}

You might also like