Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We need to make sure to follow the attribution requirements for our API. Let's add a clickable link to our app.
-
0:00
We're super close to having the bottom of our layout done.
-
0:03
There's just one more thing we need to add.
-
0:05
If we go back to the developer page in Dark Sky and look here in the overview.
-
0:12
Scroll down here to terms of service.
-
0:16
I'm not going to read this all, or go through it section by section.
-
0:20
But before you use it in your app,
-
0:22
you should familiarize yourself with any terms of service an API has.
-
0:26
The main one we need to concern ourselves with for
-
0:29
Stormy is this attribution section.
-
0:32
In a nutshell, it states that we need to display a Powered by Dark Sky message and,
-
0:38
if possible, make the text open a given webpage.
-
0:41
Let's head back over to Android Studio, add in the text view with the required
-
0:46
message and make the text a link to their site.
-
0:50
So let's grab a new text view.
-
0:55
We can strain it to the bottom.
-
0:58
To the left, or to the right.
-
1:02
Let's make this a little bit bigger.
-
1:06
We'll call this darkSkyAttribution.
-
1:13
For our default text we want it to be Powered by Dark Sky.
-
1:23
And for the text color, we want it to be our 50% white.
-
1:34
Okay, now we need to make this a link to open up the Dark Sky website.
-
1:39
It's fairly simple to do but there are some steps involved, so stick with me.
-
1:44
First, we need to add the URL to our text and
-
1:47
the best way to do that is by making this text a string resource.
-
1:53
So, let's go back up here.
-
1:58
We'll add a new resource.
-
2:02
We'll call it dark_sky_message, and we'll put Powered by Dark SKy in there.
-
2:12
Now we need to head over to the strings.xml file and
-
2:15
add the URL information to our resource.
-
2:18
So values, strings.
-
2:21
We see our string resource that we just named dark_sky_message in here.
-
2:26
We need to wrap our message text in an HTML anchor tag and
-
2:30
give the href value of the URL.
-
2:33
If you aren't familiar with HTML anchor tags and
-
2:37
href values, I've put some information in the teacher's notes about them.
-
2:41
To start with, we need to wrap our anchor tag around the text.
-
2:49
We'll add an anchor tag there, and our closing anchor tag.
-
2:55
Now we need to add the href and
-
2:57
url information from Dark Sky to the opening of our anchor tag.
-
3:01
Come in here.
-
3:04
Href, and their URL is
-
3:08
https://darksky.net/poweredby.
-
3:15
Great!
-
3:17
That wasn't too painful.
-
3:19
And if we go back and run our app, we should see our text as a link now.
-
3:31
And if we click on it, nothing happens.
-
3:34
Also looks like our degree symbol got out of alignment here, so we'll fix that too.
-
3:41
Let's tackle that degree symbol first.
-
3:45
There it is, there isn't a constraint.
-
3:47
So we'll put the left to the right of the temperature value.
-
3:52
Okay, so what's going on with our link?
-
3:55
Well, we still need to tell Android to do something when we click on it.
-
4:00
There are a few different ways we could go about this.
-
4:04
We could write an onClick listener for instance.
-
4:07
But let me show you a way that only takes a couple of lines of code
-
4:10
in main activity.
-
4:11
Let's head over there to set that up.
-
4:13
So we go over to MainActivity.
-
4:19
So here in our onCreate method, right after we call the setContentView,
-
4:24
let's define a new TextView named darkSky.
-
4:32
We want to find the view by the ID that we set a little bit ago, so findViewById.
-
4:40
I think it was darkSkyAttribution, there it is.
-
4:45
Now we need to tell Android to enable the links inside this text view object.
-
4:50
We can do that with the set movement method.
-
4:54
So darkSkySetMovementMethod();.
-
4:59
This method takes a parameter, a movement method.
-
5:02
There is a movement method named link movement method that supports
-
5:06
clicking on links, and we'll want to get that instance should we click on the link.
-
5:11
So LinkMovementMethod.getInstance, and that's it.
-
5:17
Now we can run our app.
-
5:23
And when we click on the link, We're taken to the Dark Sky site.
-
5:30
Pretty slick, eh?
-
5:32
Our layout is just about wrapped up.
-
5:34
And it's great that we have added that attribution to Dark Sky.
-
5:38
That'll make the attorneys happy.
-
5:40
We're nearing completion of our Stormy app and you're doing great.
-
5:44
The next step is to connect or
-
5:46
bind the data we are getting from the Dark Sky API to our new interface.
-
5:50
Let's see how to do that in the next stage.
You need to sign up for Treehouse in order to download course files.
Sign up