Exercise 05 - Launch a Map with Implicit Intent
Project name : E05 Launch a Map
Introduction
The purpose of this exercise is to learn how to use implicit intent in an Android application. In this exercise you will create an application, where user can give latitude and longitude values and this place is shown in the Google Maps application. You will use an implicit intent to launch a Google Maps application from the emulator or the device.
An Intent is a messaging object you can use to request an action from another app component. Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it.
Project
Create a new project for this tutorial – name it as E05 Launch a Map. Remember save your project in your android-exercises folder.
Application UI
Application UI will contain a text information, two edit text fields and one button. User can give latitude and longitude values and a Google Maps application will be launched, when a button is clicked. Edit text fields are only accepting numerical values.
First modify your string.xml string resources file add a new string resources for the text and button objects. Remember, it is always good to separate string’s from the code and UI. This helps you create a localization of your app.
1 2 3 4 5 |
|
Modify your project activity_main.xml layout file. Add a new TextView component and use above string resources to show a “Give a location (lat and lng):” text.
1 2 3 4 5 6 |
|
Add two EditText
components, which user will use to give latitude and longitude values. Remember use id
attributes to give a unique id’s for the EditText’s, so they can be found from the Kotlin code. Device’s keyboard will show only numbers, if you set inputType to numberDecimal
.
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 |
|
Add one Button component and use onClick
attribute define showMap
function, which will be called, when a button is clicked.
1 2 3 4 5 6 7 |
|
Launch a Google Maps application
When user presses a button, create an URI from latitude and longitude values and show a Google Maps with implicit intent.
Add a showMap
function inside your MainActivity
class. You need to use View based variable in parameters even you are not using that. This is how Android’s event system is working from the XML.
Coding:
- Get latitude and longitude values from EditText fields
- Use
Uri.parse
to create a location object with above values and usegeo:
protocol - Build the intent for
Intent.ACTION_VIEW
and above location - Start a new activity to show a map
Test
Your application should work now. Test it with a different latitude and longitude values. Notice how easily you can launch a different application from the device with implicit intents.
Test and push
Test your application in emulator, take screenshots and commit/push your project and screenshots back to JAMKIT/GitLab. Remember move your exercise/issue ticket from Doing to In Review in Issues Board and write your learning comments to issue comments.