MAD Pratical 26 To End
MAD Pratical 26 To End
DatabaseHelper.java: onCreate(db);
package com.example.test; }
MainActivity.java:
package com.example.test;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import
androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName =
findViewById(R.id.editTextName);
editTextEmail =
findViewById(R.id.editTextEmail);
buttonInsert =
findViewById(R.id.buttonInsert);
buttonInsert.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
activity_main.xml: android:id="@+id/editTextEmail"
android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?> android:layout_height="62dp"
<LinearLayout android:hint="Enter Email" />
xmlns:android="http://schemas.android.com/ap
k/res/android" <Button
android:layout_width="match_parent" android:id="@+id/buttonInsert"
android:layout_height="match_parent" android:layout_width="wrap_content"
android:orientation="vertical" android:layout_height="wrap_content"
android:padding="16dp"> android:text="Insert Data" />
</LinearLayout>
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="57dp"
android:hint="Enter Name" />
<EditText
27. Write a program to send email.
MainActivity.java: String subject =
package com.example.test; emailSubject.getText().toString().trim();
String message =
import android.content.Intent; emailMessage.getText().toString().trim();
import android.net.Uri;
import android.os.Bundle; Intent intent = new
import android.view.View; Intent(Intent.ACTION_SENDTO);
import android.widget.Button; intent.setData(Uri.parse("mailto:"));
import android.widget.EditText; intent.putExtra(Intent.EXTRA_EMAIL,
import new String[]{to});
androidx.appcompat.app.AppCompatActivity; intent.putExtra(Intent.EXTRA_SUBJECT,
subject);
public class MainActivity extends intent.putExtra(Intent.EXTRA_TEXT,
AppCompatActivity { message);
EditText emailTo, emailSubject,
emailMessage; if
Button sendEmailButton; (intent.resolveActivity(getPackageManager())
!= null) {
@Override startActivity(intent);
protected void onCreate(Bundle }
savedInstanceState) { }
super.onCreate(savedInstanceState); }
setContentView(R.layout.activity_main);
activity_main.xml:
emailTo = findViewById(R.id.emailTo); <?xml version="1.0" encoding="utf-8"?>
emailSubject = <LinearLayout
findViewById(R.id.emailSubject); xmlns:android="http://schemas.android.com/ap
emailMessage = k/res/android"
findViewById(R.id.emailMessage); android:layout_width="match_parent"
sendEmailButton = android:layout_height="match_parent"
findViewById(R.id.sendEmailButton); android:orientation="vertical"
android:padding="16dp">
sendEmailButton.setOnClickListener(new
View.OnClickListener() { <EditText
@Override android:id="@+id/emailTo"
public void onClick(View v) { android:layout_width="match_parent"
sendEmail(); android:layout_height="wrap_content"
} android:hint="Recipient Email" />
});
} <EditText
android:id="@+id/emailSubject"
private void sendEmail() { android:layout_width="match_parent"
String to = android:layout_height="wrap_content"
emailTo.getText().toString().trim(); android:hint="Subject" />
<EditText
android:id="@+id/emailMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message"
android:minHeight="100dp" />
<Button
android:id="@+id/sendEmailButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Email" />
</LinearLayout>
31. Write a program to locate user current location.
AndroidManifest.xml: protected void onCreate(Bundle
<uses-permission savedInstanceState) {
android:name="android.permission.ACCESS_F super.onCreate(savedInstanceState);
INE_LOCATION"/> setContentView(R.layout.activity_main);
<uses-permission
android:name="android.permission.ACCESS_C locationText =
OARSE_LOCATION"/> findViewById(R.id.locationText);
getLocationButton =
MainActivity.java: findViewById(R.id.getLocationButton);
package com.example.test; fusedLocationClient =
LocationServices.getFusedLocationProviderCli
import android.Manifest; ent(this);
import android.content.pm.PackageManager;
import android.location.Location; getLocationButton.setOnClickListener(v -
import android.os.Bundle; > getCurrentLocation());
import android.widget.Button; }
import android.widget.TextView;
import android.widget.Toast; private void getCurrentLocation() {
import androidx.annotation.NonNull; if
import (ActivityCompat.checkSelfPermission(this,
androidx.appcompat.app.AppCompatActivity; Manifest.permission.ACCESS_FINE_LOCATI
import androidx.core.app.ActivityCompat; ON)
import !=
com.google.android.gms.location.FusedLocatio PackageManager.PERMISSION_GRANTED) {
nProviderClient; ActivityCompat.requestPermissions(this,
import new
com.google.android.gms.location.LocationServi String[]{Manifest.permission.ACCESS_FINE_
ces; LOCATION},
import LOCATION_PERMISSION_REQUEST);
com.google.android.gms.tasks.OnSuccessListen return;
er; }
import com.google.android.gms.tasks.Task;
Task<Location> task =
public class MainActivity extends fusedLocationClient.getLastLocation();
AppCompatActivity { task.addOnSuccessListener(new
private static final int OnSuccessListener<Location>() {
LOCATION_PERMISSION_REQUEST = 1; @Override
private FusedLocationProviderClient public void onSuccess(Location
fusedLocationClient; location) {
private TextView locationText; if (location != null) {
private Button getLocationButton; double latitude =
location.getLatitude();
@Override double longitude =
location.getLongitude();
locationText.setText("Latitude: " + }
latitude + "\nLongitude: " + longitude);
} else { activity_main.xml:
Toast.makeText(MainActivity.this, <?xml version="1.0" encoding="utf-8"?>
"Failed to get location", <LinearLayout
Toast.LENGTH_SHORT).show(); xmlns:android="http://schemas.android.com/ap
} k/res/android"
} android:layout_width="match_parent"
}); android:layout_height="match_parent"
} android:orientation="vertical"
android:padding="16dp"
@Override android:gravity="center">
public void onRequestPermissionsResult(int
requestCode, @NonNull String[] permissions, <TextView
@NonNull int[] grantResults) { android:id="@+id/locationText"
android:layout_width="wrap_content"
super.onRequestPermissionsResult(requestCode android:layout_height="wrap_content"
, permissions, grantResults); android:text="Your Location Will Appear
if (requestCode == Here"
LOCATION_PERMISSION_REQUEST) { android:textSize="18sp"
if (grantResults.length > 0 && android:textStyle="bold" />
grantResults[0] ==
PackageManager.PERMISSION_GRANTED) { <Button
getCurrentLocation(); android:id="@+id/getLocationButton"
} else { android:layout_width="wrap_content"
Toast.makeText(this, "Permission android:layout_height="wrap_content"
denied!", Toast.LENGTH_SHORT).show(); android:text="Get Location"
} android:layout_marginTop="20dp"/>
} </LinearLayout>
}
32. Write a program to make a route between 2 locations.
MainActivity.java: sourceEditText.getText().toString().trim();
package com.example.test; String destination =
destinationEditText.getText().toString().trim();
import android.content.Intent;
import android.net.Uri; // Use Google Maps intent
import android.os.Bundle; Uri gmmIntentUri =
import android.view.View; Uri.parse("google.navigation:q=" + destination
import android.widget.Button; + "&mode=d");
import android.widget.EditText;
import android.widget.Toast; Intent mapIntent = new
import Intent(Intent.ACTION_VIEW, gmmIntentUri);
androidx.appcompat.app.AppCompatActivity;
mapIntent.setPackage("com.google.android.app
public class MainActivity extends s.maps");
AppCompatActivity {
if
private EditText sourceEditText, (mapIntent.resolveActivity(getPackageManager
destinationEditText; ()) != null) {
private Button findRouteButton; startActivity(mapIntent);
} else {
@Override // Open in web browser if Google Maps
protected void onCreate(Bundle is not available
savedInstanceState) { Uri webUri =
super.onCreate(savedInstanceState); Uri.parse("https://www.google.com/maps/dir/?a
setContentView(R.layout.activity_main); pi=1&origin=" + origin + "&destination=" +
destination);
// Initialize views Intent webIntent = new
sourceEditText = Intent(Intent.ACTION_VIEW, webUri);
findViewById(R.id.sourceEditText); startActivity(webIntent);
destinationEditText = }
findViewById(R.id.destinationEditText); }
findRouteButton = }
findViewById(R.id.findRouteButton);
activity_main.xml:
findRouteButton.setOnClickListener(new <?xml version="1.0" encoding="utf-8"?>
View.OnClickListener() { <LinearLayout
@Override xmlns:android="http://schemas.android.com/ap
public void onClick(View v) { k/res/android"
openGoogleMaps(); android:layout_width="match_parent"
} android:layout_height="match_parent"
}); android:orientation="vertical"
} android:padding="16dp">