Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
Lots of the mostly used apps eat App Shortcuts to supply customers with quick access to its most necessary options. For instance, Instagram offers shortcuts to Digicam, New Submit, View Exercise, and Direct messages, whereas WhatsApp offers Digicam and 4 conversations.
All these apps eat the ShortcutManager
API for straightforward shortcut creations.
On this tutorial, you’ll learn to handle three completely different shortcut varieties:
Obtain the starter mission by clicking on the Obtain Supplies button on the prime or backside of the tutorial. Then, open the starter mission in Android Studio 3.5 or later.
Check out the code. You’ll discover the starter mission with actions, a format, a shortcut wrapper, and lots of TODOs.
Construct and run the starter mission. You’ll see an empty display screen with a FabButton
that launches a brand new Exercise
the place you may create a brand new Word.
Strive utilizing the app. Faucet the FabButton
and create a brand new word. A brand new word shall be seen within the listing. Lengthy press the word, and BottomSheetDialogFragment
will present with two actions.
Now you’ll see what varieties of shortcuts are current and see what sort it’s best to use through which case.
Shortcut performs particular motion within the particular app, triggered from the:
Android app shortcuts might make your app extra environment friendly in the event that they’re used appropriately.
A developer has an obligation to maintain which options will present within the app shortcut listing, however a consumer wants to find them and discover them useful.
Even for individuals like us, Android builders, it’s onerous to recollect what number of highly effective instruments Android offers for the better use of the machine. Considered one of these is app shortcuts performance.
Though app shortcuts are already straightforward to entry by long-pressing the icon on the house display screen or app listing, a consumer can pin all of the shortcuts to the house display screen for even simpler entry.
Builders ought to at all times take into account supporting shortcuts, even when it’s onerous for customers to search out them.
The following part gives you an outline of the app you’ll create on this article.
The way you ship content material with shortcuts depends upon your use case and whether or not the shortcut’s context is app-driven or user-driven. Regardless of if the shortcut’s context adjustments, each static and dynamic shortcuts are pushed by your app.
In circumstances the place a consumer chooses how they need your app to ship content material to them, equivalent to with a pinned shortcut, a consumer defines the context.
The next eventualities present just a few use circumstances for every shortcut sort:
Though we have now various kinds of shortcuts that cowl all use circumstances, there are some limitations that we want to concentrate on.
Although customers can create as many dynamic shortcuts as they need, most supported launchers show as much as 4 shortcuts at a time.If the app helps dynamic shortcuts, select static shortcuts rigorously, so there’s nonetheless some house for a consumer to create dynamic ones.
But, for the dynamic shortcuts used with Google Assistant, use Google Shortcuts Integration Library to keep away from the limitation.
For those who select to not use the Google Shortcuts Integration Library, your app will help a restricted variety of shortcuts at a time.
Generally you’ll must resolve whether or not you need extra static shortcuts or let the consumer create dynamic ones.
To find out what number of shortcuts the launcher helps, use getMaxShortcutCountPerActivity()
technique offered in ShortcutManagerCompat
class.
However, in case your app helps pinned shortcuts, you’re secure from these limitations. Launchers don’t have a most variety of pinned shortcuts.
It’s time to leap in on the actions used to handle shortcuts!
As you already know, there are three various kinds of shortcuts, and the consumer manages dynamic ones.
The consumer is ready to do a number of actions with the shortcuts:
Reordering is an motion that occurs as a aspect impact of Push or Replace actions. Entry these motion on the ShortcutManagerCompat
API, they usually’re tremendous straightforward to make use of.
On this part, you’ll discover all of the actions out there for managing the app shortcuts and implement them.
Now, you’re going to begin by creating static shortcuts with predefined and unchangeable actions.
Firstly, within the res package deal, beneath xml subfolder, discover the shortcuts.xml file and open it.
On this file, you’ll discover TODO 1:
which you’ll exchange with the next code:
<!-- 1 -->
<shortcut
android:shortcutId="new_note"
android:enabled="true"
android:icon="@drawable/ic_new_note_shortcut"
android:shortcutLongLabel="@string/new_note_long_label"
android:shortcutShortLabel="@string/new_note_short_label">
<!-- 2 -->
<intent
android:motion="android.intent.motion.VIEW"
android:targetClass="com.yourcompany.quicknotes.display screen.notes.NotesActivity"
android:targetPackage="com.yourcompany.quicknotes" />
<intent
android:motion="android.intent.motion.VIEW"
android:targetClass="com.yourcompany.quicknotes.display screen.newnote.NoteActivity"
android:targetPackage="com.yourcompany.quicknotes" />
<!-- 3 -->
<classes android:identify="com.yourcompany.quicknotes" />
<!-- 4 -->
<capability-binding android:key="actions.intent.CREATE_NOTE" />
</shortcut>
That is what you’re code does:
It’s important to outline the shortcut motion correctly. Once you create a shortcut, permit customers to open the display screen stream that defines the characteristic they want.
The next move is to provide an app directions to create static shortcuts. Go to the manifests folder and open AndroidManifest.xml
file and exchange TODO 2:
with the next code snippet:
<meta-data
android:identify="android.app.shortcuts"
android:useful resource="@xml/shortcuts" />
Your shortcut is able to use. Construct and run the app, go to launcher and lengthy press on the app icon. Press the shortcut, and also you’re able to create a brand new word!
Okay, that was straightforward. Time to maneuver on to the logic of making dynamic shortcuts.
Dynamic shortcuts are a little bit bit tough however nonetheless very straightforward to implement.
As earlier than talked about, you’ll want to make use of the ShortcutManager
API. It’s a part of the Jetpack Libraries
that allows you to handle dynamic shortcuts inside your app. It reduces boilerplate code and, most significantly, ensures that shortcuts behave the identical throughout Android variations.
So as to add the performance which can create a brand new dynamic shortcut, open the ShortcutManagerWrapper.kt
file and exchange TODO 3:
with these traces of code:
return ShortcutInfoCompat.Builder(context, word.id)
.setShortLabel(word.title)
.setLongLabel("See ${word.title}")
.setIcon(IconCompat.createWithResource(context, R.drawable.ic_note))
.setIntents(
arrayOf(
NotesActivity.createIntent(context).apply { motion = Intent.ACTION_VIEW },
NoteActivity.createIntent(context, word.id).apply { motion = Intent.ACTION_VIEW }
)
)
.construct()
Word: It’s possible that you need to make some imports for the code to work.
You may see {that a} dynamic shortcut acts the identical because the static one. The distinction is that now you will have logic for making the shortcut extra context-sensitive.
To totally allow creating dynamic shortcuts, exchange TODO 4:
with these two traces of code:
val shortcut = createShortcutInfo(word)
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
The tactic pushDynamicShortcut(...)
will do:
Superior, you’re now able to create your very first dynamic shortcut. Construct and run the app, long-press the created word, after which long-press the app icon to point out the shortcuts.
The following step is enabling updates for the dynamic shortcut you’ve added.
Updating a dynamic shortcut is identical as creating a brand new one.
Within the QuickNotes app, your shortcut will replace if you open an current word and modify the content material by urgent the replace motion within the prime proper nook.
Subsequent process for you is to implement it so you may replace the shortcut when wanted.
Within the ShortcutManagerWrapper.kt
file, discover TODO 5:
and exchange it with the next:
addNoteShortcut(word)
That’s it! Straightforward, proper? 🙂
Attempt to replace your current word. Open it from the shortcut you’ve added, replace it and ensure that the app has up to date the word. Now go and examine the shortcut you beforehand created.
You’ll create one other word and the shortcut for it. Shut the app, open the shortcut listing, and confirm that the bottom-most shortcut is the one you created first.
Open the primary word, change the title, and press replace. For those who examine the shortcuts, you’ll see that you simply’ve modified the order. The one that you simply’ve edited is now on the prime. Updating the shortcut additionally adjustments the shortcut rank.
Good job, you’re subsequent step is to delete the shortcut.
To delete a dynamic shortcut, the very first thing you’ll have to know is that if the shortcut already exists.
Discover TODO 6:
and exchange it with the next block of code:
override enjoyable isShortcutCreated(noteId: String): Boolean {
return ShortcutManagerCompat.getDynamicShortcuts(context)
.discover { it.id == noteId } != null
}
Test if the shortcut with the requested word exists within the listing of dynamic shortcuts.
Because you don’t need your consumer to have the choice to entry a word as soon as it will get deleted from the database or service, you’ll have to delete the shortcut too.
Within the ShortcutManagerWrapper.kt
discover TODO 7:
and exchange it with these traces of code:
ShortcutManagerCompat.removeDynamicShortcuts(context, listOf(noteId))
Nice, a consumer is now in a position to create, replace and delete a dynamic shortcut, go forward and check out it. Construct and run the app, and observe the subsequent steps with a purpose to examine that all the pieces works:
Congratulations! You’ve applied the dynamic shortcut characteristic! Now, you’re transferring to the final sort of shortcut.
There is just one sort of shortcut left to implement. Making a pinned shortcut is a little bit bit completely different than the others.
Discover TODO 8:
and exchange it with this code snippet:
// 1
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
// 2
val pinShortcutInfo = ShortcutInfoCompat.Builder(context, word.id)
.setShortLabel(word.title)
.setIcon(IconCompat.createWithResource(context, R.drawable.ic_note))
.setIntents(
arrayOf(
NotesActivity.createIntent(context).apply { motion = Intent.ACTION_VIEW },
NoteActivity.createIntent(context, word.id).apply { motion = Intent.ACTION_VIEW }
)
)
.construct()
// 3
ShortcutManagerCompat.requestPinShortcut(context, pinShortcutInfo, null)
}
It’s simpler to know if it’s damaged down into smaller items:
ShortcutInfo
with the quick label, icon and intent which ends up inExercise
.Construct and run the app, long-press the word, and create your first Fast Notes pinned shortcut.
Go to the launcher and examine if it’s there.
You’re only one press away from the duties you’ll want for that day, procuring cart, or no matter it’s you wish to word. 🙂
Congratulations! You’re able to make your app extra accessible for the consumer. Take into consideration an important options you wish to present and don’t overlook concerning the restricted variety of shortcuts.
You may obtain the whole mission by clicking Obtain Supplies on the prime or backside of this tutorial.
On this article, you’ve realized create various kinds of app shortcuts and use them.
As talked about earlier, you can also make your app simpler to entry with Google Assistant. Yow will discover right here an superior article on create Actions for Google Assistant.
We hope you loved this tutorial. When you have any questions or feedback, please be part of the discussion board dialogue beneath!
[ad_2]