0% found this document useful (0 votes)
477 views5 pages

Practical No 32

The document describes code for a map-based Android application. The XML layout file defines the user interface with edit texts for entering a source and destination and a button to display locations. The Java code gets the entered text, checks for empty values, and opens the Google Maps app with directions from the source to the destination location. If the Maps app is not installed, the user is directed to download it from the Play Store.

Uploaded by

gamingshadow243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
477 views5 pages

Practical No 32

The document describes code for a map-based Android application. The XML layout file defines the user interface with edit texts for entering a source and destination and a button to display locations. The Java code gets the entered text, checks for empty values, and opens the Google Maps app with directions from the source to the destination location. If the Maps app is not installed, the user is directed to download it from the Play Store.

Uploaded by

gamingshadow243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical No 32

AIM:- Deploy map based application. Part II

CODE:-
.xml file

<?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:orientation="vertical" android:padding="16dp"
android:gravity="center_horizontal" tools:context=".MainActivity">
<EditText android:id="@+id/et_source" android:layout_width="317dp"
android:layout_height="56dp" android:layout_marginTop="220dp"

android:background="@android:drawable/editbox_background"

android:hint="Enter Source Location"android:padding="12dp"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"
/>
<EditText android:id="@+id/et_destination"

android:layout_width="317dp" android:layout_height="52dp"
android:layout_marginTop="36dp"

android:background="@android:drawable/editbox_background"
android:hint="Enter DestinationLocation"
android:padding="12dp"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/et_source" />
<Button
android:id="@+id/bt_track" android:layout_width="wrap_content"
android:layout_height="wrap_content"android:layout_marginTop="56dp"
android:text="Display Location" app:backgroundTint="#3F51B5"

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/et_destination" />
</androidx.constraintlayout.widget.ConstraintLayout>

.java file
package com.example.coexp32;

import android.content.ActivityNotFoundException
;
import android.content.Intent;import android.net.Uri; import
android.os.Bundle; import android.widget.Button;
import android.widget.EditText;import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extendsAppCompatActivity {
EditText etSource,etDestination;Button btTrack;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);etSource =
findViewById(R.id.et_source);etDestination =
findViewById(R.id.et_destination);btTrack =
findViewById(R.id.bt_track); btTrack.setOnClickListener(v -> {
String sSource = etSource.getText().toString().trim();
String sDestination = etDestination.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) {try{

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);
}catch (ActivityNotFoundException
e){
Uri uri = Uri.parse("https://play.google.com/store/app
s/details?id=com.google.android.apps.maps"
);
Intent intent= new Intent(Intent.ACTION_VIEW,uri);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}}}

OUTPUT:-

You might also like