9

My app has minSdk = 21 and targetSdk = 26

I wanted to set up the app shortcuts

I've added the <meta-data> tag to the first activity that's launched when the app starts.

<activity android:name=".SignInActivity"
            android:theme="@style/SubscriptionsTheme.NoActionBar">
            <intent-filter>
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.MAIN"/>
            </intent-filter>
            <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
        </activity>

Then I created the xml-v25 directory and within it this shortcuts.xml file:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
    <shortcut
        android:shortcutId="test"
        android:icon="@drawable/plus"
        android:shortcutShortLabel="test"
        android:shortcutLongLabel="long test" >

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.xxxxx.xxxxx.xxxxx"
            android:targetClass="com.xxxxx.xxxxx.xxxxxx.main" />

    </shortcut>
</shortcuts> 

when I try to build the app I get the following errors:

Error:error: 'long test' is incompatible with attribute android:shortcutLongLabel (attr) reference. Error:error: 'test' is incompatible with attribute android:shortcutShortLabel (attr) reference. Error:'long test' is incompatible with attribute android:shortcutLongLabel (attr) reference.

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
Daniele
  • 4,163
  • 7
  • 44
  • 95

1 Answers1

13

Hope this helps.

strings.xml

 <string name="long_shortcut">long test</string>
  <string name="short_shortcut">test</string>

Use reference of this strings in Manifest.xml

<shortcut
        android:shortcutId="test"
        android:icon="@drawable/plus"
        android:shortcutShortLabel="@string/short_shortcut"
        android:shortcutLongLabel="@string/long_shortcut" >

In java you can also assign this by using setLongLabel (CharSequence longLabel) and setShortLabel(CharSequence) of ShortcutInfo.Builder.

Dipali Shah
  • 3,742
  • 32
  • 47
  • Thanks for the answer. This worked. Even if I don't get why I shouldn't be able to hard code them, at least for testing – Daniele Oct 09 '17 at 10:26
  • 3
    Manifest prefer the reference from Project for any `ID`(when working with fb_id,fabric_id or map_id) / `String`(when assigning string directly). – Dipali Shah Oct 09 '17 at 10:31
  • What is the difference between shortLable and longLable? – Bulma Nov 25 '19 at 09:44
  • @miho39, Both `android:shortcutLongLabel` and `android:shortcutShortLabel` are labels shown to the user displaying the name of the shortcut. It’s recommended that the short label be less than or equal to 10 characters, and the long label be less than or equal to 25 characters. Android will pick the appropriate one to display based on the space available, but the short label is usually used when placed on the home screen. The long label is usually used whenever there is space in the app shortcut menu. https://medium.com/@andreworobator/implementing-android-app-shortcuts-74feb524959b – Dipali Shah Nov 25 '19 at 10:32