This project adds Explicit and Implicit Intents to the sample app for the Super Sweet Android Time course. The sample app is a candy store app called Candy Coded that displays a list of the store's candy and details about each candy. There is one Intent already implemented that launches the DetailActivity when a user selects a candy in the candy list in the MainActivity.
It's your job to add 4 more Intents to finish the functionality for the app!
http://www.viddler.com/v/a2575089
http://www.viddler.com/v/9e00d024
https://github.com/pluralsight-projects/AndroidProject
http://www.viddler.com/v/5fd51d49
TBD
http://www.viddler.com/v/65700ff2
We have already added an Information MenuItem to the MainActivity. Now we'd like to launch the InfoActivy class when that MenuItem is selected.
- We want to override the
onOptionsItemSelected(MenuItem item)
method inMainActivity.java
. This is where will create the Intent to open the InfoActivity. (Hint: Ctrl+O will list the Overrride methods. Then, type in the box to search and select the one you want.) - Our method needs to return a boolean, we will return the default call to the super class
return super.onOptionsItemSelected(item);
. - On the first line of the
onOptionsItemSelected()
method, create the Intent for launching the InfoActivity. Call theIntent infoIntent
and the two parameters to thenew Intent()
constructor will bethis
for the Context andInfoActivity.class
for the Activity we want to start. - Next, call
startActivity()
with theinfoIntent
as a parameter to start our Intent.
Our InfoActivy has a TextView with the address of our store. We want to launch Google Maps at that address when that TextView is clicked.
- In
InfoActivity.java
, create a method calledpublic void createMapIntent(View view)
. This is the method we'll attach to the Click Listener on the TextView later. - Create a
Uri
from the address by calling theUri.parse()
method and passing in the String with the geo location of our store"geo:0,0?q=618 E South St Orlando, FL 32801"
. - Create an
Intent
calledmapIntent
and pass two parameters to the constructor - first is the action which will beIntent.ACTION_VIEW
, second is the Uri we just created. - Make the Intent explicit by setting the Google Maps package. We can do this with the Intent's
setPackage()
method and pass in the String"com.google.android.apps.maps"
. - We will attempt to start an activity that can handle the Intent. To do this create an if statement, inside call
mapIntent.resolveActivity(getPackageManager())
and check that the result is not equal tonull
. - Inside the if statement we can call
startActivity()
with ourmapIntent
. - Now we need to connect this method to the TextView's click event. Go into the layout file
activity_info.xml
and add the following properties to the TextView with the idtext_view_address
-android:clickable="true"
andandroid:onClick="createMapIntent"
.
Our InfoActivity also has a TextView with the phone number of our store. We want to launch a dial Intent with that phone number when that TextView is clicked.
- In
InfoActivity.java
, create a method calledpublic void createPhoneIntent(View view)
. This is the method we'll attach to the Click Listener on the TextView later. - Create an
Intent
with actionIntent.ACTION_DIAL
. - Use the Intent
setData()
method and pass in aURI
of the telephone number"tel:0123456789"
. You can create aURI
with theUri.parse()
method. - Start the Activity with the Intent.
- Now we need to connect this method to the button click. Go into
activity_info.xml
and add the following properties to the TextView with the idtext_view_phone
-android:clickable="true"
andandroid:onClick="createPhoneIntent"
.
We have already added a Share MenuItem to the DetailActivity so that the user can share the selected Candy. Now we need to add the functionality to launch an Intent with action ACTION_SEND when that MenuItem is selected.
- Override the
public boolean onOptionsItemSelected(MenuItem item)
method and have it return the default call to the super classreturn super.onOptionsItemSelected(item);
. (Hint: Ctrl+O will list the Overrride methods. Then, type in the box to search and select the one you want.) - Since there is a big chunk of code to create the Intent let's do it in a separate method. Create a method called
private void createShareIntent()
. - In
onOptionsItemSelected()
, let's call the method we just wrotecreateShareIntent()
. - Now inside our new
createShareIntent()
method, let's create an Intent calledshareIntent
with actionACTION_SEND
. - Use the Intent's
setType()
method to set the type to"text/plain"
. - To create the String that we want to share, we can use the String variables created at the top of this file -
SHARE_DESCRIPTION
,mCandyImageUrl
, andHASHTAG_CANDYCODED
. We can concatenate them into one String variable set equal toSHARE_DESCRIPTION + mCandyImageUrl + HASHTAG_CANDYCODED
. - Use the Intent's
putExtra()
method to add the text we want to share. The first parameter is the type of contentIntent.EXTRA_TEXT
, and the second is our concatenatedString
. - Finally, we can finish off the
createShareIntent()
method by callingstartActivity()
with ourshareIntent
.