17

I'm new to web applications for Android..

How can you add a bookmark on the home screen from a web page using Javascript from the click of a button?

If we make an easy way for users to bookmark pages, they will show some interest in bookmarking them. This is one of the requirement for my projects.

How can this be done?

manidhar mulaparthi
  • 1,062
  • 3
  • 18
  • 30
  • I doubt this is even possible. If it were then that would mean a webpage could issue intents to your phone, and I doubt that functionality is baked into the stock browser. You might be able to do it with a custom browser or with a browser plugin. – slayton Sep 22 '11 at 21:22

4 Answers4

13

Some of the people answering here seem to not understand that you want something for mobile, not IE, Firefox, etc. They also don't seem to read very well where you said "website", not "mobile app". However, I believe I have read your question properly.

There's extremely high odds that you'll probably also want this for iPhone, so I'll answer this for iPhone and Android as well.

For iPhone, it's as simple as using this script:

http://code.google.com/p/mobile-bookmark-bubble/

For Android, it's not so pretty, unfortunately. You'll have to make a button or hyperlink that redirects to an instructions page on your site. The instructions will tell web visitors to do "Settings > More > Add Shortcut to Home" and hope they "get it".

One option for Android is that you can fork mobile-bookmark-bubble, make it load at the bottom of the page without the bottom triangle, and make it read, "Click the Settings button > More > Add Shortcut to Home". As well, you might want to put the 2 bars icon of what the Settings button looks like right next to "Settings button".

As for how to customize the icon that gets created, I'm still researching that and will update this answer when I find out.

Volomike
  • 23,743
  • 21
  • 113
  • 209
  • The about link is broken, I used the below link to download the source file. Safari solution still worked. Only noticed the bookmark bubble first time, refreshing cache doesn't help to show up again. https://github.com/h5bp/mobile-boilerplate/wiki/Mobile-Bookmark-Bubble – S.Roshanth Jul 16 '21 at 15:37
5

Here is a possible workaround, and while it isn't exactly what you want, I think it is the easiest solution as I'm highly skeptical that android allows web pages to automatically issue intents to a device, as this would be a potential security problem. Someone please correct me if I'm wrong

I recommend that you create an app that is simply a wrapper around a bookmark. When the user clicks on your app, your app creates an intent that simply opens the device's default web browser to your page. While this will require the users install a app it has a few advantages over a plain bookmark. You will be able to track how many people have your app/bookmark installed, how often they use it etc. You can also provide a nice looking icon instead of using the stock "bookmark" icon.

Upload the app to the market as a free app and place a "bookmark this" link on your webpage that simply directs the user to download your free app.

slayton
  • 20,123
  • 10
  • 60
  • 89
  • While none of these answer the question directly I believe this is as close as we're going to get given the current SDK. Though it doesn't meet 100% of my stated objective I'm awarding the +100 reputation bounty as a good faith gesture. Thanks to those that submitted answers, comments, etc. – Bill Mote Sep 28 '11 at 02:01
  • One could also just open the web app in a view of the native application as well, no? – Tyler Biscoe Aug 02 '13 at 17:55
4

You can create a web page shortcut on home screen using this code. Provide the necessary info like url, title etc..

  final Intent in = new Intent();
  final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  long urlHash = url.hashCode();
  long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
  shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId));
  in.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
  in.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
  in.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(
                            BrowserBookmarksPage.this,
                            R.drawable.ic_launcher_shortcut_browser_bookmark));
  in.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
//or   in.setAction(Intent.ACTION_CREATE_SHORTCUT); 

  sendBroadcast(in);

Update

The browser does not recognise the intent scheme, so there is no way you can add a shortcut to your webpage from your webpage.

Ron
  • 24,175
  • 8
  • 56
  • 97
  • That's a great answer, but I guess I should have been more clear. I'm talking about from Android's native browser without installing an application. On my plain old web site I want to create an add bookmark button that can be used by Android users to add a bookmark to their home screen. Thanks for the response though! – Bill Mote Sep 22 '11 at 23:47
  • is it possible for a webpage to issue an intent via the browser to the system? – slayton Sep 26 '11 at 15:19
  • No we cannot issue intent via browser. We can define our own scheme and launch our activity though. – Ron Sep 26 '11 at 15:28
  • Easy-peasy once "our" app is installed. I get that. I was hoping to find similar functionality on Android that exists on the iPhone. Thank you for responding! – Bill Mote Sep 28 '11 at 01:56
0

This can help

<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("a.jQueryBookmark").click(function(e){
    e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
    var bookmarkUrl = this.href;
    var bookmarkTitle = this.title;

    if (window.sidebar) { // For Mozilla Firefox Bookmark
        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
    } else if( window.external || document.all) { // For IE Favorite
        window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
    } else if(window.opera) { // For Opera Browsers
        $("a.jQueryBookmark").attr("href",bookmarkUrl);
        $("a.jQueryBookmark").attr("title",bookmarkTitle);
        $("a.jQueryBookmark").attr("rel","sidebar");
    } else { // for other browsers which does not support
         alert('Your browser does not support this bookmark action');
         return false;
    }
});
});
</script>

<h2>Click on the respective links below to bookmark the same</h2>
<a href="http://www.developersnippets.com" title="developersnippets.com" class="jQueryBookmark">DeveloperSnippets</a>, <a href="http://www.techvideobytes.com" title="Tech Video Bytes" class="jQueryBookmark">Tech Video Bytes</a>, <a href="http://www.wittysparks.com" title="Witty Sparks" class="jQueryBookmark">Witty Sparks</a>, <a href="http://www.snehah.com/" title="Snehah.com" class="jQueryBookmark">Snehah.com</a>

For more info, go to this link ( http://www.developersnippets.com/2009/05/10/simple-bookmark-script-using-jquery/)

Ron
  • 24,175
  • 8
  • 56
  • 97
Rahul Arora
  • 1,036
  • 9
  • 8
  • oh, someone edited by question. Its actually for android web application.. I did rollback now.. Please read my question again.. Thank you.. – manidhar mulaparthi Feb 28 '11 at 03:04
  • The JQuery example here doesn't trigger any bookmark action on the Android. – Vindberg Feb 11 '13 at 09:11
  • ^ Before commenting please read comments mentioned. Somebody edited the question and there was no mention of android application. This works in web-browser. Manidhar did a rollback. – Rahul Arora Feb 13 '13 at 09:11