Practical No 32
Practical No 32
Practical No 32
CODE:-
.xml file
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"
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{
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:-