-
Notifications
You must be signed in to change notification settings - Fork 0
Lab 17
In this lab, we'll gain familiarity using Android's Intent
system to perform various actions.
- We'll review an "explicit intent" to show the
TwitterActivity
- We'll use an "implicit intent" to send a tweet
- We'll use another implicit intent to select an image and save it as part of a
Note
Β
Β
-
Review navigation to
TwitterActivity
via explicit intent- How are we creating the intent?
- What makes this intent "explicit" vs "implicit"?
- How do we use the
Intent
to actually show theTwitterActivity
?
-
When
TwitterActivity
opens, launch an ImplicitIntent
to send a Tweet (see hints)
Β
We're going to add support for selecting an image to go along with our Note
.
-
Add an
ImageView
tofragment_create_note.xml
- This
ImageView
will show a selected image if/when one is selected - This
ImageView
can be laid out anywhere you want withinfragment_create_note.xml
; just as long as it's visible
- This
-
Use an implicit
Intent
to select an image- Within
CreateNoteFragment
, add a click handler for your newly addedImageView
- When the
ImageView
is clicked, create anIntent
for selecting an image (see hints).
- Within
-
Respond to image selection results
- Override
CreateNoteFragment.onActivityResult()
to respond to the selected image URI - Check that the
requestCode
matches the request used instartActivityForResult()
. This ensures we're only handling results for things we requested. - Check that the
resultCode == Activity.RESULT_OK
. This ensures the selection didn't fail and that our desired data should be available. - If both codes look correct, access the selected image URI by calling
data.data
- Override
-
Display the selected image
- Using the image data provided by
data.data
, callbinding.imageView?.setImageURI(data.data)
to display the image
- Using the image data provided by
-
Save the selected Image URI as part of the
Note
- When a
Note
is saved, it should include the selected URI if one exists - Add
val imageUri: String? = null
to yourNote
model to store this data - This is a database schema change, and will require either uninstalling/reinstalling your app, or providing a database migration (outside the scope of this lab).
- When a
Β
- Update
NoteDetailFragment
UI to display the selected image - Change the
scaleType
on yourImageView
so that the aspect ratio is not changed when the image URI is set
Display feedback to the user if the "select photo" Intent returns a result other than Activity.RESULT_OK
Β
Β
- Twitter's Web Intent Documentation
- ImageView Documentation
- Intents and Intent Filters
- Common Intents
Β
To do this, we need two things:
- We need an Intent with the action
Intent.ACTION_VIEW
- We need a valid Twitter url to resolve
https://twitter.com/intent/tweet?text=%s
private fun createTweetIntent(): Intent {
// Create intent using ACTION_VIEW and a normal Twitter url:
val tweetUrl = String.format(
"https://twitter.com/intent/tweet?text=%s",
URLEncoder.encode("I'm learning a lot about #androiddev today!", "UTF-8")
)
return Intent(Intent.ACTION_VIEW, Uri.parse(tweetUrl))
}
Β
There are many ways to do this; depending on how general or specific your want to be.
You can customize your message for emails, text messages, note apps, etc.
For a general Share Intent that could send a text or an email, we can use something like the following:
private fun sendShareIntent() {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_SUBJECT, "Android Bootcamp w/ Kotlin")
putExtra(Intent.EXTRA_TEXT, "I'm learning a lot about Android development today!!")
}
startActivity(intent)
}
Β
There are a lot of different ways to accomplish this. Try the following
private fun selectImage() {
val intent = Intent().apply {
type = "image/*"
action = Intent.ACTION_OPEN_DOCUMENT
}
startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CHOOSE_IMAGE)
}
Β
If we launch an "image selection" Intent
using startActivityForResult()
, then we can respond to the returned result by overriding onActivityResult()
.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode != REQUEST_CHOOSE_IMAGE || resultCode == Activity.RESULT_CANCELED) return
if (resultCode == Activity.RESULT_OK && data != null) {
// these 3 lines ensure our app has permissions to access/use this file in the future; beyond just this selection
// this is needed due to more restrictive permissions changes in recent versions of Android
val contentResolver = requireContext().contentResolver
val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION
contentResolver.takePersistableUriPermission(data.data!!, takeFlags)
// hold on to the reference to the URI so we can save it later
uriToImage = data.data.toString()
binding.imageView?.setImageURI(data.data)
}
}
Β
ImageView
has a method specifically for this.
Locate the URI to an image in the local filesystem, and call ImageView.setImageURI()