Android Tutorial Add shortcuts to your Android application

Discussion in 'Tutorials & Examples' started by Erel, Sep 12, 2011.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Android allows the user to add desktop shortcuts to any application.
    Pressing on such a shortcut is exactly the same as starting your application from the applications list.

    However it is also possible to create custom shortcuts which will have a custom behavior. For example start a different activity or start your application in a different mode.
    In order to add a custom shortcut you should create a dedicated "shortcut Activity". This activity will be started when the user adds your shortcut from the shortcuts list. The activity should return an intent as the result which will later start the application.
    The following screenshots demonstrate the shortcut behavior on a Honeycomb device. It should be similar to regular devices:

    Shortcuts list:


    Our application launched from the shortcut:

    You should edit the manifest file and set an intent filter to the shortcut activity. This tells the OS that your application includes a custom activity.
    Please read the section about manual modification of the manifest file here.

    This is the shortcut activity declaration in the manifest file of the example program:
    <activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name="shortcutactivity" 
    label="Add Shortcut" android:screenOrientation="unspecified">
                    <action android:name=
                    <category android:name=
    android:name is the lower cased name of the shortcut activity.
    android:label is the title that will appear in the shortcuts list.

    The code of the shortcut activity is:
    Sub Activity_Create(FirstTime As Boolean)
    Dim shortcutIntent As Intent
    "anywheresoftware.b4a.samples.test/.main"'<--- change to match your package name / and target activity.
    Dim in As Intent
    in.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
    in.PutExtra("android.intent.extra.shortcut.NAME""Shortcut Test")
    in.PutExtra("android.intent.extra.shortcut.ICON"LoadBitmap(File.DirAssets, "small_logo.png"))
    End Sub
    You should change the line with the comment to match your package name and target activity. Note that the syntax is: package_name/.activity and activity should be lower cased.

    The last step that should be done is to check in our main activity whether our program was launched from the shortcut:
    Sub Activity_Resume
    Dim in As Intent
    in = Activity.GetStartingIntent
    If in.HasExtra("from_shortcut"AND in.GetExtra("from_shortcut") = True Then
    Msgbox("Launched from shortcut""")
    End If
    End Sub

    Attached Files:

    cxbs, Mashiane, mobah and 2 others like this.
  2. hackhack

    hackhack Active Member Licensed User

    Astro can make a shortcut to say a picture, and that shortcut keeps working even after you uninstall astro.

    Can this do that? You write for the 'setcomponent' to set it match "your package name" - if we instead set it to the system handler for pictures, would that launch the picture viewer instead?
  3. Erel

    Erel Administrator Staff Member Licensed User

    I do not know how Astro shortcut mechanism works. The inner Intent will be sent when the shortcut is clicked. You can create any Intent that you like as the inner intent.

    Once your application is uninstalled then the shortcut will be removed as well.
  4. hackhack

    hackhack Active Member Licensed User

    Well neither do I unless we disassemble it, but as I wrote elsewhere I'm assuming they just put in a shortcut intent to another app than their own.

    No it isn't. Not here anyway. its left behind when i uninstall the app.

    If the intent refers to the app I'm told the app doesn't exist (as it should)

    But if I do

    Dim in2 As Intent
    Dim pm As PackageManager
    in2 = m.GetApplicationIntent(
    And pass in2 in in.PutExtra("android.intent.extra.shortcut.INTENT",in2) then the shortcut launches the youtube app, whether or not my app is installed.

    The one thing which is annoying to me if you move the shortcut creation routine out to the main app and run it then it doesn't work - it has to be created on call initiated from Android shortcut menu (the Honeycomb "more") - can that be avoided?
  5. Erel

    Erel Administrator Staff Member Licensed User

    Not that I know of.
  6. splatt

    splatt Active Member Licensed User

    Start app from widget

    Can this process be used to start an my application at a specific place if the user clicks on the apps widget?
  7. Erel

    Erel Administrator Staff Member Licensed User

    Pressing on the shortcut will start the chosen activity.
  8. splatt

    splatt Active Member Licensed User

    I'm developing an app that also has a widget, showing time, date and the next event due from a diary.

    What I should have asked is if this is the preferred way to launch the underlying application at say the Diary screen, if the user clicks on the widget?
  9. Erel

    Erel Administrator Staff Member Licensed User

    This is a different approach. If you are using a home screen widget then you should not use this method. I think that a widget is preferable in most cases.
  10. splatt

    splatt Active Member Licensed User

    I'm only just getting my head around widgets and services.

    As I understand it the widget is created and maintained by a service. So if the user clicks on a label on the widget, how can I then launch the underlying application, which may or may not be running and start it at a specific screen.

    I read in the help files that you shouldn't use StartApplication from a service, so what is the recommended way?

    Thanks for any help.

    EDIT: Answered my own question. I've added a global Boolean variable called 'StartedFromWidget' to the Service Module and set it to true in the Click event. I then start the app and test this variable and if it is true, show the required screen. It's still confusing when the Help says you should't use StartActivity, but the first widget example does just that|
    Last edited: Jan 31, 2012
  11. rnunez81

    rnunez81 New Member Licensed User


    I got this example to run, it creates an activity that gets listed in the "Shortcuts" from the phone home screen (I mean: if I long press on any empty space of the home screen, I get a screen that says "Add to home screen" and I can chose to add a shortcut, widget, folder, wallpaper or theme. By doing what is indicated in your post, I get the application listed in the shortcuts that can be added to the home screen, but the shortcut is not automatically added to the home screen at the time of install (which is what I wanted to do).

    Do you know how to do that extra step?

    From information I've found around, it seems like shortcuts are added automatically to the home screen ONLY if the application is installed using Google Play AND ONLY if the user selected the "Auto-add shortcuts" option in Google Play's setting. This would mean that if I distribute my App via email among my co-workers (because my app is very focused to our business), then there is no way to have this shortcut added automatically at time of instalation. Is this so?

    In other words, if the user can long click on the app icon from the App Drawer and drag it to the home screen to create a shortcut there... what benefit does this programming add? Please help me understand.


    Last edited: Aug 8, 2012
  12. Erel

    Erel Administrator Staff Member Licensed User

    This solution doesn't add the shortcut automatically. Shortcuts allow you to start your application in a different mode.

    For example you can define a shortcut that automatically does some action when starting.
  13. JesseW

    JesseW Active Member Licensed User

    Erel, can I programmaticly add a shortcut? It'd be nice if my B4Script app could place shortcuts on the homescreen to launch the app in run mode, executing a script :)

  14. Erel

    Erel Administrator Staff Member Licensed User

    It cannot be done without user intervention. You can create a widget (which the user will add to the home screen) and then automatically customize based on the available programs.
  15. thedesolatesoul

    thedesolatesoul Expert Licensed User

    While I havent tried it, this thread here suggests you can make a shortcut from within the app.
  16. JesseW

    JesseW Active Member Licensed User

    actually, both mintoris basic and rfo basic allow the user to create shortcuts with no system user intervention. x11-basic uses the widget approach you mention, and that would have been my last resort. thanks for the reply.

    thank you so much! I took a look at that thread, and it appears to be exactly what I was looking for! :D
  17. Rusty

    Rusty Well-Known Member Licensed User

    In one of your posts, you indicate that you can add a shortcut to the user's home screen with permission.
    Can you point me to an example where I can create a shortcut for my app on the home screen with permission (or without)?
    I would like to have my program install and programatically add its shortcut to the user's home screen so they don't have to find it and drag/drop it there.
  18. Erel

    Erel Administrator Staff Member Licensed User

    It is not possible to automatically add a shortcut after installation, unless the user selects this option in Google Play settings. In that case shortcuts are added automatically for all apps.
  19. Rusty

    Rusty Well-Known Member Licensed User

    Thanks Erel.
    Does this mean that if I "sideload" applications it is not possible, only possible if an app is installed from Google Play?
    How is this set in the Google Play settings?
  20. Erel

    Erel Administrator Staff Member Licensed User


  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice