Android - Error while trying to add app shortcuts
Asked Answered
G

1

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.

Gavan answered 9/10, 2017 at 10:19 Comment(1)
You probably need to use a string resourcePampero
O
14

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.

Octameter answered 9/10, 2017 at 10:25 Comment(4)
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 testingGavan
Manifest prefer the reference from Project for any ID(when working with fb_id,fabric_id or map_id) / String(when assigning string directly).Octameter
What is the difference between shortLable and longLable?Burnisher
@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. medium.com/@andreworobator/…Octameter

© 2022 - 2024 — McMap. All rights reserved.