Professional Documents
Culture Documents
Practical 17
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:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="25dp"/>
</LinearLayout>
MainActivity.java
package com.example.activitylifecyleapp;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("INFORMATION","onCreate() called");
Toast.makeText(this, "onCreate() called", Toast.LENGTH_SHORT).show();
}
protected void onStart()
{
super.onStart();
Log.d("INFORMATION","onStart() called");
Toast.makeText(this, "onStart() called", Toast.LENGTH_SHORT).show();
}
protected void onResume()
{
super.onResume();
Log.d("INFORMATION","onResume() called");
Bhavesh Nitin Patil 43
Practical 18
1) 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:orientation="vertical"
android:gravity="center">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:hint="Enter URL"
android:id="@+id/etext">
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate"
android:layout_marginTop="15dp"
android:onClick="navigate">
</Button>
</LinearLayout>
MainActivity.java
package com.example.navigationlikebrowser;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
EditText etext;
@Override
Bhavesh Nitin Patil 43
etext=findViewById(R.id.etext);
}
public void navigate(View v)
{
Uri uri= Uri.parse("https://"+etext.getText().toString());
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
}
2) 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"
Bhavesh Nitin Patil 43
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Dialer"
android:onClick="Dial">
</Button>
</LinearLayout>
MainActivity.java
package com.example.startdailerapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Dial(View v)
{
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}
}
Bhavesh Nitin Patil 43
3) 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:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter number"
android:textSize="25dp">
</EditText>
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="factorial"
android:onClick="factorial"
></Button>
</LinearLayout>
MainActivity.java
package com.example.factorial;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
EditText e;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
Bhavesh Nitin Patil 43
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e=findViewById(R.id.e);
b=findViewById(R.id.b);
}
public void factorial(View v)
{
Intent intent = new Intent(this,MainActivity2.class);
intent.putExtra("num",Integer.parseInt(e.getText().toString()));
startActivity(intent);
}
}
activity_main2.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=".MainActivity3"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="Factorial"
android:textSize="25dp">
</TextView>
</LinearLayout>
MainActivity2.java
package com.example.factorial;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
TextView tv;
@Override
Bhavesh Nitin Patil 43
tv=findViewById(R.id.tv);
Intent intent = getIntent();
int num=intent.getIntExtra("num",0);
int fact = 1;
int i = 1;
while( i <= num )
{
fact = fact * i;
i++;
}
tv.setText("Factorial = "+fact);
}
}
Practical 21
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:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BroadCastMessages"
android:textSize="25dp"
/>
</LinearLayout>
MainActivity.java
package com.example.broadcastreciver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerBroadcastReceiver();
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterBroadcastReceiver();
Bhavesh Nitin Patil 43
}
private void registerBroadcastReceiver() {
mReceiver = new SystemBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mReceiver, filter);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_BATTERY_CHANGED:
Toast.makeText(context, "Battery changed " ,
Toast.LENGTH_SHORT).show();
break;
case Intent.ACTION_SCREEN_ON:
Toast.makeText(context, "Screen turned on",
Toast.LENGTH_SHORT).show();
break;
case Intent.ACTION_SCREEN_OFF:
Toast.makeText(context, "Screen turned off",
Toast.LENGTH_SHORT).show();
break;
}
}
}
}
MyReciever.java
package com.example.broadcastreciver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_BATTERY_CHANGED:
break;
case Intent.ACTION_SCREEN_ON:
break;
case Intent.ACTION_SCREEN_OFF:
break;
}
}
}
ActivityManifest.xml
<receiver android:name=".MyReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.BATTERY_CHANGED" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.AIRPLANE_MODE" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Bhavesh Nitin Patil 43
Bhavesh Nitin Patil 43
Practical 22
1) 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:gravity="center"
android:id="@+id/ll">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate to see magic"
android:textSize="25dp"/>
</LinearLayout>
MainActivity.java
package com.example.sensordeviceshuffled;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll=findViewById(R.id.ll);
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT)
ll.setBackgroundColor(Color.parseColor("#2196F3"));
else if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE)
ll.setBackgroundColor(Color.parseColor("#E16C12"));
}
}
Bhavesh Nitin Patil 43
2) activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"
></ListView>
</LinearLayout>
MainActivity.java
package com.example.listofsensors;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
SensorManager smm;
List<Sensor> sensor;
Bhavesh Nitin Patil 43
ListView lv;
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
lv = findViewById(R.id.lv);
sensor = smm.getSensorList(Sensor.TYPE_ALL);
lv.setAdapter(new
ArrayAdapter<Sensor>(this, android.R.layout.simple_list_item_1,
sensor));
}
}
Bhavesh Nitin Patil 43
Bhavesh Nitin Patil 43
Practical 24
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:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:textSize="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn on"
android:layout_margin="25dp"
android:onClick="on">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Off"
android:layout_margin="25dp"
android:onClick="off"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Visible"
android:layout_margin="25dp"
android:onClick="getvisible"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List"
android:layout_margin="25dp"
android:onClick="getlist"/>
Bhavesh Nitin Patil 43
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/l1"/>
</LinearLayout>
MainActivity.java
package com.example.bluetoothapp;
imports…….;
public class MainActivity extends AppCompatActivity {
ListView lv;
@Override
.
return;
}
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), "Turn On",
Toast.LENGTH_LONG).show();
} else {
}
public void off(View v) {
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
BA.disable();
}
public void getvisible(View v) {
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.BLUETOOTH_ADVERTISE) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
startActivityForResult(getv, 0);
}
return;
}
pairedDevices = BA.getBondedDevices();
ArrayList list=new ArrayList();
for(BluetoothDevice bt:pairedDevices){
list.add(bt.getName());
}
Toast.makeText(getApplicationContext(),"Showing Paired
Devices",Toast.LENGTH_LONG).sow();
ArrayAdapter adpt=new
ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
lv.setAdapter(adpt);
}
Bhavesh Nitin Patil 43
Bhavesh Nitin Patil 43
Practical 23
1) 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:orientation="vertical">
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:id="@+id/iv"
></ImageView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="30dp"
android:text="camera"></Button>
</LinearLayout>
MainActivity.java
package com.example.cameraapp;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bhavesh Nitin Patil 43
iv = findViewById(R.id.iv);
}
ActivityResultLauncher mStartCamera =
registerForActivityResult(new
ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult
result) {
if(result.getResultCode()==RESULT_OK) {
Intent i = result.getData();
Bundle bundle = i.getExtras();
Bitmap bitmap = (Bitmap)
bundle.get("data");
iv.setImageBitmap(bitmap);
}
else
Toast.makeText(getApplicationContext(),"Photo does not
captured",Toast.LENGTH_LONG).show();
}});
ActivityResultLauncher mStartPermission =
registerForActivityResult(new
ActivityResultContracts.RequestPermission(),
new ActivityResultCallback<Boolean>() {
@Override
public void onActivityResult(Boolean result) {
if(result){
Intent i = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mStartCamera.launch(i);
}else Toast.makeText(getApplicationContext(),"Camera
permission is denied",Toast.LENGTH_LONG).show();
}});
public void openCamera(View v) {
mStartPermission.launch(android.Manifest.permission.CAMERA);}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA">
</uses-permission>
Bhavesh Nitin Patil 43
2) activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="captureVideo"
android:text="Video"
/>
</RelativeLayout>
MainActivity.java
package com.example.pr232;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
}
Bhavesh Nitin Patil 43
Practical 27
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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USER LOGIN"
android:textSize="30dp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_margin="50dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="ExtraText">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USERNAME"
android:id="@+id/textuser"
android:textStyle="bold"
android:layout_margin="30dp"
/> >
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="30dp"
android:id="@+id/edituser"
android:hint="username"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
Bhavesh Nitin Patil 43
android:layout_height="wrap_content"
android:id="@+id/textpass"
android:text="PASSWORD"
android:textStyle="bold"
android:layout_margin="30dp"
/> >
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="30dp"
android:id="@+id/editpass"
android:hint="password"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGIN"
android:id="@+id/btn"
android:layout_gravity="center"/>
</LinearLayout>
MainActivity.java
package com.example.pr271;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.pr271.R;
EditText user,pass;
Button login;
int c=3;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bhavesh Nitin Patil 43
user=(EditText) findViewById(R.id.edituser);
pass=(EditText) findViewById(R.id.editpass);
login=(Button) findViewById(R.id.btn);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(user.getText().toString().trim().equals("Bhavesh") &&
pass.getText().toString().trim().equals("Bhavesh123"))
{
Toast.makeText(MainActivity.this, "Login Successfull",
Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(MainActivity.this, "Login Unsuccessfull",
Toast.LENGTH_SHORT).show();
}
});
}
}
Bhavesh Nitin Patil 43
Bhavesh Nitin Patil 43
Practical 28
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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USER LOGIN"
android:textSize="30dp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_margin="50dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="ExtraText">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USERNAME"
android:id="@+id/textuser"
android:textStyle="bold"
android:layout_margin="30dp"
/> >
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="30dp"
android:id="@+id/edituser"
android:hint="username"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
Bhavesh Nitin Patil 43
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textpass"
android:text="PASSWORD"
android:textStyle="bold"
android:layout_margin="30dp"
/> >
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="30dp"
android:id="@+id/editpass"
android:hint="password"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGIN"
android:id="@+id/btn"
android:layout_gravity="center"/>
</LinearLayout>
MainActivity.java
package com.example.pr281;
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;
EditText user,pass;
Button login;
int c=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bhavesh Nitin Patil 43
user=(EditText) findViewById(R.id.edituser);
pass=(EditText) findViewById(R.id.editpass);
login=(Button) findViewById(R.id.btn);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(user.getText().toString()=="")
{
user.setError("Please Enter Username");
}
else if(pass.getText().toString()=="")
{
pass.setError("Please Enter Password");
}
else if (user.length()<=8) {
user.setError("Username is greater the 7 characters");
}
else if(pass.length()<=6){
pass.setError("Password is greater tha 5 characters");
}
else if(!user.getText().toString().trim().equals("Bhavesh1234"))
{
if(c==0)
Toast.makeText(MainActivity.this, "You entered username or
password many times", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "Login Unsuccessfull "+(c-
1)+" attempts remaining", Toast.LENGTH_SHORT).show();
c--;
}
else if (!pass.getText().toString().trim().equals("Bhavesh"))
{
if(c==0)
Toast.makeText(MainActivity.this, "You entered username or
password many times", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "Login Unsuccessfull "+(c-
1)+" attempts remaining", Toast.LENGTH_SHORT).show();
c--;
}
else if(user.getText().toString().trim().equals("123") &&
pass.getText().toString().trim().equals("Bhavesh") && c>=0)
{
Toast.makeText(MainActivity.this, "Login Successfull",
Toast.LENGTH_SHORT).show();
}
Bhavesh Nitin Patil 43
}
});
}
}
Bhavesh Nitin Patil 43
Practical 29
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:background="@color/white"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Phone Number"
android:textColor="@color/black"
android:layout_marginTop="50dp"
android:textColorHint="@color/black"
android:inputType="phone"
android:id="@+id/phone"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="SMS Mesage"
android:textColorHint="@color/black"
android:textColor="@color/black"
android:layout_marginTop="50dp"
android:inputType="textMultiLine"
android:minLines="4"
android:id="@+id/meg"
/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Send SMS"/>
</LinearLayout>
MainActivity.java
package com.example.pr291;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
Bhavesh Nitin Patil 43
import androidx.core.content.ContextCompat;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
`
Bhavesh Nitin Patil 43
Bhavesh Nitin Patil 43
Practical 30
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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginRight="20dp"
android:text="To:"
android:textSize="20dp"
/>
<EditText
android:id="@+id/edit_text_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:inputType="textEmailAddress"
android:minHeight="48dp"
android:hint="To"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Subject:"
android:textSize="20dp"
/>
<EditText
android:id="@+id/edit_text_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:inputType="textEmailSubject"
android:minHeight="48dp"
android:hint="Subject"
/>
<TextView
android:id="@+id/textView3"
Bhavesh Nitin Patil 43
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Message:"
android:textSize="20dp"
/>
<EditText
android:id="@+id/edit_text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="start|top"
android:hint="Type Message"
android:lines="10"
/>
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="send"
android:layout_gravity="center"
/>
</LinearLayout>
MainActivity.java
package com.example.pr301;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
Practical 32
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"
tools:context=".MainActivity">
<EditText
android:id="@+id/source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:background="@android:drawable/editbox_background"
android:hint="Start Location"
android:textColorHint="#2C2A2A"
android:textColor="#2C2A2A"
android:padding="12dp"
/>
<EditText
android:id="@+id/dest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_gravity="center"
android:ems="15"
android:background="@android:drawable/editbox_background"
android:hint="Destination Location"
android:textColorHint="#2C2A2A"
android:textColor="#2C2A2A"
android:padding="12dp"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Display Route"
android:layout_gravity="center"
/>
</LinearLayout>
Bhavesh Nitin Patil 43
MainActivity.java
package com.example.pr321;
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;
import android.widget.Toast;
EditText Source,Dest;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
setTitle("Loaction App");
Source = findViewById(R.id.source);
Dest = findViewById(R.id.dest);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sSource = Source.getText().toString().trim();
String sDestination = Dest.getText().toString().trim();
if(sSource.equals("") && sDestination.equals(""))
Toast.makeText(MainActivity.this, "Enter both location",
Toast.LENGTH_SHORT).show();
else
DisplayTrack(sSource,sDestination);
}
});
}
private void DisplayTrack(String sSource, String sDestination) {
Uri uri = Uri.parse("https://www.google.co.in/maps/dir/" + sSource + "/"
+ sDestination);
Intent intent= new Intent(Intent.ACTION_VIEW,uri);
intent.setPackage("com.google.android.apps.maps");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Bhavesh Nitin Patil 43
Bhavesh Nitin Patil 43
Bhavesh Nitin Patil 43
Practical 31
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">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
MainActivity.java
package com.example.pr31;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
public class MainActivity extends FragmentActivity implements
OnMapReadyCallback {
Location currentLocation;
FusedLocationProviderClient fusedLocationProviderClient;
private static final int REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
Bhavesh Nitin Patil 43
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(this);
fetchLastLocation();
}
private void fetchLastLocation() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new
String[]
{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
return;
}
Task<Location> task =
fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location!=null){
currentLocation = location;
Toast.makeText(getApplicationContext(), "Latitude:
"+currentLocation.getLatitude()+"\n"+"Longitude:
"+currentLocation.getLongitude(),Toast.LENGTH_SHORT).show();
SupportMapFragment supportMapFragment =
(SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(MainActivity.this);
}
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng latLng = new
LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
MarkerOptions options = new
MarkerOptions().position(latLng).title("I am Here");
Bhavesh Nitin Patil 43
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,5));
googleMap.addMarker(options);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
switch (requestCode){
case REQUEST_CODE:
if(grantResults.length>0 &&
grantResults[0]==PackageManager.PERMISSION_GRANTED){
fetchLastLocation();
}
break;
}
}
}
MyLocationListener.java
package com.example.pr31;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
@Override
Bhavesh Nitin Patil 43
Practical 26
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">
<EditText
android:id="@+id/editTextRecordNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_marginTop="71dp"
android:ems="10"
android:inputType="number"
android:minHeight="48dp"
android:singleLine="true"
tools:ignore="SpeakableTextPresentCheck">
<requestFocus />
</EditText>
<Button
android:id="@+id/buttonNormalInsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextRecordNum"
android:layout_alignLeft="@+id/editTextRecordNum"
android:layout_marginLeft="27dp"
android:layout_marginTop="179dp"
android:text="Normal Insert" />
<Button
android:id="@+id/buttonFastInsert"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonNormalInsert"
android:layout_alignBottom="@+id/buttonNormalInsert"
android:layout_marginLeft="46dp"
android:layout_marginBottom="0dp"
android:layout_toRightOf="@+id/buttonNormalInsert"
android:text="Fast Insert" />
Bhavesh Nitin Patil 43
<TextView
android:id="@+id/textViewStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonNormalInsert"
android:layout_alignLeft="@+id/buttonNormalInsert"
android:layout_marginLeft="12dp"
android:layout_marginTop="143dp"
android:padding="10dp"
android:text="Status"
tools:ignore="DuplicateIds" />
</RelativeLayout>
MainActivity.java
package com.example.pr26;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.opengl.GLDebugHelper;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
final String TAG = "MainActivity.java";
EditText editTextRecordNum;
TextView tvStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener handler = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonNormalInsert:
new AsyncInsertData("normal").execute();
break;
Bhavesh Nitin Patil 43
case R.id.buttonFastInsert:
new AsyncInsertData("fast").execute();
break;
}
}
};
editTextRecordNum = (EditText) findViewById(R.id.editTextRecordNum);
findViewById(R.id.buttonNormalInsert).setOnClickListener(handler);
findViewById(R.id.buttonFastInsert).setOnClickListener(handler);
tvStatus = (TextView) findViewById(R.id.textViewStatus);
}
class AsyncInsertData extends AsyncTask<String, String, String> {
DBHelper databaseHandler;
String type;
long timeElapsed;
protected AsyncInsertData(String type){
this.type = type;
this.databaseHandler = new DBHelper(MainActivity.this);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
tvStatus.setText("Inserting " + editTextRecordNum.getText() + "
records...");
}
@Override
protected String doInBackground(String... aurl) {
try {
// get number of records to be inserted
int insertCount =
Integer.parseInt(editTextRecordNum.getText().toString());
// empty the table
databaseHandler.deleteRecords();
// keep track of execution time
long lStartTime = System.nanoTime();
if (type.equals("normal")) {
databaseHandler.insertNormal(insertCount);
} else {
databaseHandler.insertFast(insertCount);
}
// execution finised
long lEndTime = System.nanoTime();
// display execution time
timeElapsed = lEndTime - lStartTime;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String unused) {
Bhavesh Nitin Patil 43
DBHelper.java
package com.example.pr26;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
public class DBHelper extends SQLiteOpenHelper {
// for our logs
public static final String TAG = "DatabaseHandler.java";
// database version
private static final int DATABASE_VERSION = 7;
// database name
protected static final String DATABASE_NAME = "NinjaDatabase2";
// table details
public String tableName = "locations";
public String fieldObjectId = "id";
public String fieldObjectName = "name";
public String fieldObjectDescription = "description";
// constructor
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// creating table
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "";
sql += "CREATE TABLE " + tableName;
sql += " ( ";
sql += fieldObjectId + " INTEGER PRIMARY KEY AUTOINCREMENT, ";
sql += fieldObjectName + " TEXT, ";
sql += fieldObjectDescription + " TEXT ";
sql += " ) ";
db.execSQL(sql);
String INDEX = "CREATE UNIQUE INDEX locations_index ON "
+ tableName + " (name, description)";
db.execSQL(INDEX);
}
Bhavesh Nitin Patil 43
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
String sql = "DROP TABLE IF EXISTS " + tableName;
db.execSQL(sql);
onCreate(db);
}
db.close();
return recCount;
}
public void deleteRecords(){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("delete from "+ tableName);
db.close();
}
}
Bhavesh Nitin Patil 43
Practical 16
1)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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="45dp"
android:text="Pushpak"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="280dp"
android:layout_height="140dp"
android:layout_gravity="center"
android:timePickerMode="spinner"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TimePicker
android:id="@+id/timePicker2"
android:layout_width="269dp"
android:layout_height="155dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:timePickerMode="spinner"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.451"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/timePicker1" />
<TimePicker
android:id="@+id/timePicker3"
android:layout_width="267dp"
android:layout_height="357dp"
Bhavesh Nitin Patil 43
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="68dp"
android:timePickerMode="clock"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/timePicker2" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.pr16_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TimePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TimePicker t2 = findViewById(R.id.timePicker2);
t2.setIs24HourView(true);
}
}
Bhavesh Nitin Patil 43
2)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/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:text=" Date and Time"
android:textSize="25dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.472" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="59dp"
android:background="#979292"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/btn"
app:layout_constraintEnd_toStartOf="@+id/btn"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="155dp"
android:text="Get Date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="59dp"
android:layout_marginTop="112dp"
android:background="#9C9797"
Bhavesh Nitin Patil 43
android:textSize="20sp"
app:layout_constraintEnd_toStartOf="@+id/btn2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Get Time"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn"
app:layout_constraintVertical_bias="0.804" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.pr16_2;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
});
}
}
Bhavesh Nitin Patil 43