Now that we've got some contact details on our emulators, we can have a look now at how to access those contacts
using a Content Provider.
Now the Contacts app has registered a Content Provider that allows apps to access the phone's contacts,
provided they request the necessary permissions and the user grants them of course.
And we're going to be seeing how to do that as we write this app. So I'm going to start a new Android
Studio project,
so let's call this one Content Provider Example.
As always, include Kotlin support, Next.
We're going to make sure we've got API 17, Next. For this example app we're not going to bother to create our own buttons.
We're going to keep things really simple,
and we'll use the floating action button to load the contacts.
So I'm going to choose a basic activity and click on Next, and the suggested names on this last configuration
screen are OK so I'm going to click on Finish. Now once it finishes creating the project, we're going
to start work on the Content Main
xml file. And the only thing we really want to add to this content_main.xml
file is a ListView, and that ListView's going to be used to display the contacts that we're going to grab
from the Content Provider.
OK so you can see that content_main.xml has loaded automatically. For some reason
it hadn't
then just open it as you would normally open it from here, from the resources section in the layout folder.
And I'm going to just close down the project pane to make a bit more space.
And the other thing I'll do is I'll just open both the design and the blueprint so you can see both.
Now what you're going to do first is delete the Hello World,
the TextView widget, so we won't be using that.
And as I mentioned we're going to be using a ListView widget,
so let's grab that. ListView appears down here under legacy. That's probably an indication, by the way,
that Google are planning to deprecate it at some point. But that's not something I'm worried about
for this simple app, but you may want to consider that possibility though when writing your own apps.
Alright so we're going to grab a ListView,
bring it over here and release.
So we want the ListView to fill the available space.
These buttons up here in the inspector make it easy to add the constraints for us automatically.
So we're going to use all the available space, and the other thing we need to do then, is to set both the
layout width and layout height.
Set those both to match constraint.
So obviously we just set constraints. Alright, then next we want to check the scrollbars and we want to make sure that
they're set to vertical.
So I'm going to come over here and click on scrollbars. They're not set for me in this case so I'm going to click
on vertical,
so we've got a vertical scroll bar there for our ListView.
OK.
So we've got our layout width and layout height both set to match constraint.
So the next thing to do is to change the ID.
We want to actually cause one contact_names because we'll be needing to refer to that in our code.
And now there's one other thing to check, and you can tell this by looking at the screen whether it's
set or not.
And that's the layout_behavior property of the constraint layout.
So obviously we want to come over here and click on the constraint layout. That would be where you set
it.
But if there's a toolbar size space at the top of the layout, which we can see there obviously is in both the
design and the blueprint, then we know that everything's fine.
But if for some reason you need to set that and it's not showing for you, make sure you've clicked on the constraint
layout. Then come over here, view all attributes, and you can do a quick search for this or you can just scroll
down to the relevant section.
So search and I'm going to search for layout behavior. Layout
behavior's in layout_margin there,
so there's the layout behavior. And you can see in this case - you can't quite read it all -
but it is actually set to appbar scrolling, or appbar underscore scrolling underscore view underscore
behavior.
So if it's not set for some reason just click there, and then click on the little ellipses button and you
could choose the appbar_scrolling_view_behavior.
But in my case, because we can see the toolbar at the top we know that that is set.
So that's good to go.
Alright so that's the layout finished at this point.
So I'm going to come back in here and just,
I'll just make sure that they're set correctly.
We got that weird error. I'm not sure if
that's a bug or what's going on there, but we seem to have two versions of match constraint showing there.
Let's go back and select the other one,
and that seems to fix it. So I'm not sure what's going on there,
but if you do see something like that - that looks like a bug to me -
just make sure you set the other match constraints so that it actually works and fixes the text, fixes the
xml rather.
Alright, select that xml now and click on Code and select Reformat Code, even though there wasn't
really much to format. It's there so that when you get to the stage of needing to compare your code with ours, it's
going to be the same. Alright so now that I've reformatted the code,
we can move onto the layout for the individual items that'll appear in the ListView.
Now if you want to experiment with displaying more fields from the Contacts database once we finish this
app, then please do. It's always a good idea to experiment.
That's a great way to further your understanding of Android app development or any programming language
or environment for that matter.
And it really will help you make sure that you, really will help make sure you understand these videos.
But I'm just going to display a single field though,
the Contact's name.
So in this next layout we're about to create, I'm going to open the project pane first. Then we're going to right
click on the layout, New, I'm going to select Layout resource file. So because we're displaying just a single
contacts name,
we only need the one field and we're just going to use a TextView.
So I'm going to call this one contact_detail.
So the root element I'm going to change is not going to be a constraint layout - it's literally just going to
be a TextView.
So I'm just going to select TextView because it's the single name, and that just saves us having to change it again
anytime later.
Alright, so first thing we want to do is come over here and click on the TextView, and I'm just going
to close the project pane so we've got a bit more space back. So selecting TextView over here to the left under the component tree - the
ID initially - we'll call that name. We're going to also add some sample text - TextView - in the tools text property.
We talked about what that does before. So that way they'll still show up in the layout but the TextView won't
contain the sample text
when the app runs. So the layout width and height - layout width is correct on match parent but layout height should be changed.
We want that to be wrap content, and the reason we're doing that is to allow very long names to
still fill OK.
So that's all we really need.
But what I will do is do one other thing and change the text size to 18 - make it a bit bigger than normal.
Now text size doesn't appear straight away. Make sure you're expanding the textAppearance section
here so that you can see that. Alright so that's it.
As you can see it's a very basic layout.
What I'll do now is, I'm just going to get access to the text, to the xml again, select that. I'm going to
reformat the code. Go back to design again, and I'm going to close this layout file now. OK so I want
to close that. I'm going to close this other content_main as well, just leaving just purely our Kotlin code.
OK, so we're now ready to add the Kotlin code to retrieve the contact's data, using a Content Provider and
we'll do that in the next video.