mad pr cmp1 (1)
mad pr cmp1 (1)
14
Develop a program to implement List View, Grid View, Image View and Scroll View.
Java Code android:padding="16dp"
import android:layout_height="wrap_content"
androidx.appcompat.app.AppCompatActivity;
android:text="LIST OF COLORS"
public class MainActivity extends
android:textSize="24sp"
AppCompatActivity {
android:textStyle="bold"
@Override
android:layout_marginBottom="16dp"/>
protected void onCreate(Bundle
savedInstanceState) { <ListView
super.onCreate(savedInstanceState); android:id="@+id/listView"
setContentView(R.layout.activity_main); android:layout_width="match_parent"
}}
Xml code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res
/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
Practical 15
Develop a Program to Implement Custom Toast alert
XML File:-
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World.Toast Example"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show Toast"
android:onClick="Popup"/>
</LinearLayout>
Java:-
package com.example.myapplication;
import
androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
}
public void Popup(View view)
{
Practical 16
Develop a Program to implement Date and Time Picker
Xml code
<AbsoluteLayout android:textSize="20sp"/>
xmlns:android="http://schemas.android.com/apk/res
</AbsoluteLayout>
/android"
android:layout_width="match_parent"
Java code
android:layout_height="match_parent">
package com.example.practical16;
<TextView
import android.os.Bundle;
android:id="@+id/textView"
import android.view.View;
android:layout_width="376dp"
import android.widget.Button;
android:layout_height="118dp"
import android.widget.TextView;
android:layout_x="22dp"
import android.widget.TimePicker;
android:layout_y="29dp"
import android.widget.Toast;
android:gravity="center"
import
android:text="@string/time_picker_example"
androidx.appcompat.app.AppCompatActivity;
android:textSize="36sp"/>
public class MainActivity extends
<TimePicker AppCompatActivity {
android:layout_x="29dp" @Override
<LinearLayout @Override
xmlns:android="http://schemas.android.com/apk/res
protected void onPause() {
/android"
super.onPause();
xmlns:tools="http://schemas.android.com/tools"
Log.d("lifecycle", "onPause invoked"); }
android:layout_width="match_parent"
@Override
android:layout_height="match_parent"
protected void onStop() {
tools:context=".MainActivity">
super.onStop();
</LinearLayout>
Log.d("lifecycle", "onStop invoked"); }
Java Code
@Override
package com.example.pr17;
protected void onRestart() {
import
androidx.appcompat.app.AppCompatActivity; super.onRestart();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
super.onStart();
android:layout_height="match_parent" startDialerButton.setOnClickListener(new
View.OnClickListener() {
tools:context=".MainActivity">
@Override
<Button
public void onClick(View v) {
android:id="@+id/startDialerButton"
Intent intent = new
android:layout_width="wrap_content"
Intent(Intent.ACTION_DIAL);
android:layout_height="wrap_content"
intent.setData(Uri.parse("tel:"));
android:layout_centerInParent="true"
startActivity(intent);
android:text="Start Dialer" />
} });
</RelativeLayout>
}}
Java Code
package com.example.pr_18_02;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import
androidx.appcompat.app.AppCompatActivity;
<resources> android:id="@+id/loadButton"
<string android:layout_width="wrap_content"
name="app_name">Accessing_Content_Provider</ android:layout_height="wrap_content"
string>
android:onClick="onClickShowDetails"
<string name="heading">Accessing data of Content
android:text="@string/loadButtonText" />
Provider</string>
<TextView
<string name="loadButtonText">Load
android:id="@+id/res"
Data</string>
android:layout_width="match_parent"
</resources>
android:layout_height="match_parent"
Activity_main.xml
android:textSize="18sp"
<?xml version="1.0" encoding="utf-8"?>
android:textStyle="bold" />
<LinearLayout
</LinearLayout>
xmlns:android="http://schemas.android.com/apk/res
/android" MainActivity.java
android:textAlignment="center" @Override
android:textSize="24sp" protected void onCreate(Bundle
savedInstanceState) {
android:textStyle="bold" />
super.onCreate(savedInstanceState); else {
} }
findViewById(R.id.res);
Cursor cursor
=getContentResolver().query(Uri.parse("content://c
om.demo.user.
if(cursor.moveToFirst()) {
d"))+ "-"+
cursor.getString(cursor.getColumnIndex("name")))
;
cursor.moveToNext();
resultView.setText(strBuild);
}
Practical 20
Develop a Program to implement service
Xml code
<RelativeLayout }
xmlns:android="http://schemas.android.com/apk/res
public void startWifiService(View view) {
/android"
Intent serviceIntent = new Intent(this,
xmlns:tools="http://schemas.android.com/tools"
WifiService.class);
android:layout_width="match_parent"
startService(serviceIntent);
android:layout_height="match_parent"
}
tools:context=".MainActivity">
}
<Button
AndroidManifest.xml
android:layout_width="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
android:layout_height="wrap_content"
<manifest
android:text="Start Wi-Fi Service" xmlns:android="http://schemas.android.com/apk/res
/android"
android:onClick="startWifiService"
xmlns:tools="http://schemas.android.com/tools">
android:layout_centerInParent="true" />
<!-- Permission to change WiFi state -->
</RelativeLayout>
<uses-permission
Java code
android:name="android.permission.CHANGE_WIF
package com.example.practical_20_ex_01; I_STATE"/>
<application
import android:icon="@mipmap/ic_launcher"
androidx.appcompat.app.AppCompatActivity;
android:label="@string/app_name"
public class MainActivity extends android:roundIcon="@mipmap/ic_launcher_round"
AppCompatActivity {
android:supportsRtl="true"
@Override android:theme="@style/Theme.Practical_20_ex_01
"
protected void onCreate(Bundle
savedInstanceState) { tools:targetApi="31">
super.onCreate(savedInstanceState); <activity
android:name=".MainActivity" }
<intent-filter> @Override
<service android:name=".WifiService"
android:exported="false"/>
</application>
</manifest>
WifiService.java
package com.example.practical_20_ex_01;
import android.app.Service;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.IBinder;
@Override
super.onCreate();
wifiManager = (WifiManager)
getSystemService(WIFI_SERVICE);
}
@Override
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
Practical 21
Develop a Program to implement broadcast receiver
Xml code super.onStart();
}
}
return Settings.System.getInt(
context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0
) != 0;
}
}
Practical 22
Develop a Program to implement Sensors
Xml code
android:layout_height="match_parent" super.onCreate(savedInstanceState);
android:gravity="center" setContentView(R.layout.activity_main);
android:orientation="vertical" mainLayout =
findViewById(R.id.mainLayout);
tools:context=".MainActivity">
sensorManager = (SensorManager)
</LinearLayout>
getSystemService(Context.SENSOR_SERVICE);
java code
accelerometerSensor =
package com.example.practical_22; sensorManager.getDefaultSensor(Sensor.TYPE_AC
CELEROMETER);
import android.annotation.SuppressLint;
lastShakeTime = System.currentTimeMillis();
import android.app.Activity;
}
import android.content.Context;
@Override
import android.graphics.Color;
protected void onResume() {
import android.hardware.Sensor;
super.onResume();
import android.hardware.SensorEvent;
sensorManager.registerListener(this,
import android.hardware.SensorEventListener;
accelerometerSensor,
import android.hardware.SensorManager; SensorManager.SENSOR_DELAY_NORMAL);
import android.os.Bundle; }
android:layout_height="50dp" @Override
android:layout_marginStart="150dp" protected void onCreate(Bundle
savedInstanceState) {
android:text="Camera" />
super.onCreate(savedInstanceState);
<!-- ImageView to display captured image -->
setContentView(R.layout.activity_main);
<ImageView
camera_open_id =
android:id="@+id/click_image"
findViewById(R.id.camera_button);
android:layout_width="350dp"
click_image_id =
android:layout_height="450dp" findViewById(R.id.click_image);
super.onActivityResult(requestCode,
resultCode, data);
click_image_id.setImageBitmap(photo);
} }}
Practical 24
Develop a Program for Providing Bluetooth Connectivity
Java code turnOnBtn.setOnClickListener(v ->
turnOnBluetooth());
package com.example.madpracticalno24;
getVisibleBtn.setOnClickListener(v ->
import android.bluetooth.BluetoothAdapter; makeVisible());
import android.bluetooth.BluetoothDevice; listDevicesBtn.setOnClickListener(v ->
import android.content.Intent; listDevices());
import android.widget.Toast;
private void turnOnBluetooth() {
import
androidx.appcompat.app.AppCompatActivity; if (!bluetoothAdapter.isEnabled()) {
<Button
if (!pairedDevices.isEmpty()) {
android:id="@+id/turn_on_btn"
for (BluetoothDevice device :
pairedDevices) { android:layout_width="match_parent"
devicesList.append("Device: android:layout_height="wrap_content"
").append(device.getName()).append("\n")
android:text="Turn On Bluetooth" />
.append("MAC Address:
<!-- Button to make Bluetooth discoverable -->
").append(device.getAddress()).append("\n\n");
<Button
}
android:id="@+id/get_visible_btn"
Toast.makeText(this, devicesList.toString(),
Toast.LENGTH_LONG).show(); android:layout_width="match_parent"
} else { android:layout_height="wrap_content"
} }} android:layout_width="match_parent"
<LinearLayout </LinearLayout>
Android Manifest
xmlns:android="http://schemas.android.com/apk/res
<?xml version="1.0" encoding="utf-8"?>
/android"
android:layout_width="match_parent"
<manifest </intent-filter>
xmlns:android="http://schemas.android.com/apk/res
</activity>
/android"
</application>
package="com.example.madpracticalno24">
</manifest>
<!-- Bluetooth permissions for Android 12+ -->
<uses-permission
android:name="android.permission.BLUETOOTH_
ADVERTISE"/>
<uses-permission
android:name="android.permission.BLUETOOTH_
CONNECT"/>
<uses-permission
android:name="android.permission.BLUETOOTH"
/>
<uses-permission
android:name="android.permission.BLUETOOTH_
ADMIN"/>
<application
android:allowBackup="true"
android:supportsRtl="true"
android:theme="@style/Theme.MadPracticalNo24"
>
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHE
R" />
Practical 25
Develop a Program for Animation
Xml code app:layout_constraintStart_toStartOf="parent"
<androidx.constraintlayout.widget.ConstraintLayou
t
<ImageView
android:id="@+id/img"
xmlns:android="http://schemas.android.com/apk/res
/android" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res- android:layout_height="wrap_content"
auto"
android:src="@drawable/img" />
xmlns:tools="http://schemas.android.com/tools"
<Button
android:layout_width="match_parent"
android:onClick="b1"
android:layout_height="match_parent"
android:layout_width="wrap_content"
tools:context=".MainActivity">
android:layout_height="wrap_content"
<EditText
android:layout_gravity="center_horizontal"
android:id="@+id/editTextTextPersonName"
android:layout_marginTop="15dp"
android:layout_width="162dp"
android:text="Clockwise / Anti Clockwise"
android:layout_height="48dp" />
android:ems="10" <Button
android:inputType="textPersonName" android:onClick="b2"
android:text="Atharva Butte" android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_gravity="center_horizontal"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="15dp"
/>
android:text="Zoom In / Out" />
<LinearLayout
<Button
android:layout_width="319dp"
android:onClick="b3"
android:layout_height="549dp"
android:layout_width="wrap_content"
android:layout_marginTop="112dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="15dp"
app:layout_constraintHorizontal_bias="0.63"
android:text="Fade In / Out" /> public void onAnimationStart(Animation
animation) { }
</LinearLayout>
@Override
</androidx.constraintlayout.widget.ConstraintLayo
ut> public void onAnimationEnd(Animation
animation) {
img.startAnimation(ant_rot);
Java code
}
package com.example.practical25;
@Override
import
androidx.appcompat.app.AppCompatActivity; public void onAnimationRepeat(Animation
animation) { }
import android.os.Bundle;
});
import android.view.View;
img.startAnimation(rot);
import android.view.animation.Animation;
}
import android.view.animation.AnimationUtils;
public void b2(View v) {
import android.widget.ImageView;
Animation zom_in =
public class MainActivity extends
AnimationUtils.loadAnimation(this,
AppCompatActivity {
R.anim.zoom_in);
ImageView img;
Animation zom_out =
@Override AnimationUtils.loadAnimation(this,
R.anim.zoom_out);
protected void onCreate(Bundle
savedInstanceState) { zom_in.setAnimationListener(new
Animation.AnimationListener() {
super.onCreate(savedInstanceState);
@Override
setContentView(R.layout.activity_main);
public void onAnimationStart(Animation
img = findViewById(R.id.img);
animation) { }
}
@Override
public void b1(View v) {
public void onAnimationEnd(Animation
Animation rot = animation) {
AnimationUtils.loadAnimation(this, R.anim.rotate);
img.startAnimation(zom_out);
Animation ant_rot =
}
AnimationUtils.loadAnimation(this,
R.anim.anti_rotate); @Override
@Override });
img.startAnimation(zom_in);
} }
Animation fad_in =
AnimationUtils.loadAnimation(this,
R.anim.fade_in);
Animation fad_out =
AnimationUtils.loadAnimation(this,
R.anim.fade_out);
fad_in.setAnimationListener(new
Animation.AnimationListener() {
@Override
img.startAnimation(fad_out);
}
@Override
});
img.startAnimation(fad_in);
Practical 26
Perform Async task using SQLite
Xml code Java code
<RelativeLayout import
xmlns:android="http://schemas.android.com/apk/res androidx.appcompat.app.AppCompatActivity;
/android"
import android.database.Cursor;
android:layout_width="match_parent"
import android.os.Bundle;
android:layout_height="match_parent"
import android.view.View;
android:padding="16dp">
import android.widget.ArrayAdapter;
<EditText
import android.widget.Button;
android:id="@+id/editTextName"
import android.widget.EditText;
android:layout_width="match_parent"
import android.widget.ListView;
android:layout_height="wrap_content"
import android.widget.Toast;
android:hint="Enter Name"
import java.util.ArrayList;
android:inputType="text" />
import java.util.List;
<Button
public class MainActivity extends
android:id="@+id/buttonInsert" AppCompatActivity {
android:id="@+id/listViewData" @Override
onCreate(db); }
} @Override
@Override
if (listener != null) {
listener.onDataFetched(cursor);
}}}
void onDataInserted();
}
public interface DataFetchListener {
}
Practical 27
Create sample application with login module. (Check username and password) On
successful login, Change Text View “Login Successful” and on login fail, alert user using
Toast “Login fail”.
Xml code
android:layout_below="@id/editTextUsername"
<?xml version="1.0" encoding="utf-8"?>
android:layout_marginTop="20dp"
<RelativeLayout
android:layout_marginHorizontal="20dp" />
xmlns:android="http://schemas.android.com/apk/res android:id="@+id/btnLogin"
/android"
android:layout_width="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Login"
android:layout_height="match_parent"
tools:context=".MainActivity"> android:layout_below="@id/editTextPassword"
<EditText android:layout_centerHorizontal="true"
android:layout_width="match_parent" <TextView
android:layout_height="wrap_content" android:id="@+id/textViewLoginStatus"
android:hint="Username" android:layout_width="wrap_content"
android:layout_marginTop="50dp" android:layout_height="wrap_content"
<EditText android:layout_below="@id/btnLogin"
android:id="@+id/editTextPassword" android:layout_centerHorizontal="true"
android:hint="Password"
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername =
findViewById(R.id.editTextUsername);
editTextPassword =
findViewById(R.id.editTextPassword);
btnLogin = findViewById(R.id.btnLogin);
textViewLoginStatus =
findViewById(R.id.textViewLoginStatus);
btnLogin.setOnClickListener(new
View.OnClickListener() {
@Override
validateLogin();
}});}
private void validateLogin() {
String username =
editTextUsername.getText().toString().trim();
String password =
editTextPassword.getText().toString().trim();
if (username.equals(validUsername) &&
password.equals(validPassword)) {
textViewLoginStatus.setText("Login
Successful");
editTextUsername.getText().clear();
Practical 28
Create login application where you will have to validate username and password till the
username and password is not validated, login button should remain disabled.
Xml code
editTextUsername.addTextChangedListener(loginTe
package com.example.pr_28_01; xtWatcher);
import android.os.Bundle;
super.onCreate(savedInstanceState); btnLogin.setEnabled(!editTextUsername.getText().t
setContentView(R.layout.activity_main); oString().isEmpty() &&
editTextUsername =
findViewById(R.id.editTextUsername); !editTextPassword.getText().toString().isEmpty());
editTextPassword = } };
findViewById(R.id.editTextPassword); private void validateLogin() {
btnLogin = findViewById(R.id.btnLogin); String username =
btnLogin.setEnabled(false); editTextUsername.getText().toString().trim();
String password = editTextUsername =
editTextPassword.getText().toString().trim(); findViewById(R.id.editTextUsername);
} }}
editTextPassword.addTextChangedListener(loginTe
Java code
xtWatcher);
package com.example.pr_28_01;
btnLogin.setOnClickListener(new
import android.os.Bundle; View.OnClickListener() {
String username =
editTextUsername.getText().toString().trim();
String password =
editTextPassword.getText().toString().trim();
if (username.equals(validUsername) &&
password.equals(validPassword)) {
Toast.makeText(MainActivity.this, "Login
Successful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Login
Unsuccessful", Toast.LENGTH_SHORT).show();
} }}
Practical 30
Develop a program to send and receive Email
Xml Code android:gravity="top"
<RelativeLayout android:layout_marginTop="8dp"/>
<Button
xmlns:android="http://schemas.android.com/apk/res
android:id="@+id/buttonSend"
/android"
android:layout_width="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Send"
android:layout_height="match_parent"
android:layout_below="@id/editTextBody"
android:padding="16dp"
android:layout_centerHorizontal="true"
tools:context=".MainActivity">
android:layout_marginTop="16dp"/>
<EditText
</RelativeLayout>
android:id="@+id/editTextTo"
Java code
android:layout_width="match_parent"
package com.example.madpracticalno30;
android:layout_height="wrap_content"
import android.content.Intent;
android:hint="To" />
import android.net.Uri;
<EditText
import android.os.Bundle;
android:id="@+id/editTextSubject"
import android.view.View;
android:layout_width="match_parent"
import android.widget.Button;
android:layout_height="wrap_content"
import android.widget.EditText;
android:hint="Subject"
import
android:layout_below="@id/editTextTo"
androidx.appcompat.app.AppCompatActivity;
android:layout_marginTop="8dp"/>
public class MainActivity extends
<EditText AppCompatActivity {
setContentView(R.layout.activity_main);
editTextTo = findViewById(R.id.editTextTo);
editTextSubject =
findViewById(R.id.editTextSubject);
editTextBody =
findViewById(R.id.editTextBody);
buttonSend = findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new
View.OnClickListener() {
@Override
}});
String to =
editTextTo.getText().toString().trim();
String subject =
editTextSubject.getText().toString().trim();
String body =
editTextBody.getText().toString().trim();
intent.setData(Uri.parse("mailto:")); // Ensures
only email apps handle this intent
intent.putExtra(Intent.EXTRA_EMAIL, new
String[]{to});
intent.putExtra(Intent.EXTRA_SUBJECT,
subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
if
(intent.resolveActivity(getPackageManager()) !=
null) {
startActivity(intent);