Tuesday, 10 November 2015

Facebook App Deep Linking - Andoid

One of the most interesting aspects of sharing to Facebook from your app is that when people engage with the feed stories posted from your app, those stories can send people to your app or your app's Google Play listing, driving traffic and app installs. You can implement this behavior using App Links.

   App link Flow


Facebook's Mobile Hosting API for App Links:


If your application doesn't have a website for content you want to share to Facebook, you don't have public web URLs which you can annotate to support App Links.

For these types of apps, Facebook provides an App Links Hosting API that will host App Links for you. With the Hosting API you can create and manage your App Links for all the mobile environments you support.

This is the typical flow to configure an App Link for a piece of content:
  1. Create a new App Link object. You can set up an object for one platform or several at the same time.
  2. Save the App Link object ID that's returned.
  3. Use that ID to get the URL you can use to share the content.
  4. Configure additional platforms that you support.
  5. If supporting Android, configure your app's manifest file for the configured URLs.

Please follow below steps for generate app linking.


For web service, In request Params we need to send below parameters.

iOS: url, name
Android: url, name

Step #1: Generate App Access Token (Ref. https://developers.facebook.com/docs/facebook-login/access-tokens#apptokens)

GET /oauth/access_token?     client_id={app-id}    &client_secret={app-secret}    &grant_type=client_credentials


Step #2: Generate App Link using generated App Access Token (Ref. https://developers.facebook.com/docs/applinks/hosting-api)


You create a new App Link object for a content targeted to iOS & Android  like this

curl https://graph.facebook.com/app/app_link_hosts \
-F access_token="APP_ACCESS_TOKEN" \
-F name="iOS App Link Object Example" \
-F ios=' [
    {
      "url" : "applinkFB://showApp/54eee6292658c7df25000004",
      "app_store_id" : 570281083,
      "app_name" : “appName”,
    },
  ]' \
-F android=' [
    {
      "url" : "applinkFB://showApp/54eee6292658c7df25000004",
      "package" : "com.packagename”,
      "app_name" : “appName”,
    },
  ]' \

-F web=' {
    "should_fallback" : false,
  }'


A successful call returns an ID that represents the App Link object hosted on Facebook,
for example:

Return : {"id":"643402985734254"}

Step #3: Using generated ID(YOUR_APP_LINK_HOST_ID) which represents the App Link, you need to call another API which will return "canonical_url"


You can retrieve the canonical URL that points to your new App Link object through this API call:

curl -G https://graph.facebook.com/YOUR_APP_LINK_HOST_ID \
-d access_token="APP_ACCESS_TOKEN" \
-d fields=canonical_url \
-d pretty=true

Where YOUR_APP_LINK_HOST_ID represents the id returned from creating your App Link object. Your response will look like this:

Return:
{
   "canonical_url": "https://fb.me/643402985734254",   "id": "643402985734254"
}


Support sharesample URLs in Android

When a shared link is tapped your Android app is launched through the URL specified in the App Link object you just created (if you didn't specify a url, then it will use the canonical URL - i.e. the https://fb.me/xxxxx URL). To set up your app to respond to this URL, you need to add an intent filter in your app's manifest file.

The filter should respond to the applinkFB scheme (if you didn't specify a URL, then your filter should respond to the fb.me host and https scheme), handle implicit intents, and accept the ACTION_VIEW action. The example below adds an intent filter to the MainActivity that handles this:



    ...
    
        
        
        
        
    


Once you've set up your web pages based App Links, 
you're ready to handle incoming links to your app.

Handling incoming links

To ensure an engaging user experience, you should process the incoming intent information when your app is activated and direct the person to the object featured in the story they're coming from. 

For example, if I see a story on my Facebook feed about one of my friends completing this share tutorial and I tap on it, I will expect to be redirected to a view in your app that features this tutorial and not to your app's main activity.

If you use the App Links Hosting API, the Intent data will look like this:


data: "applinkFB://showApp/54eee6292658c7df25000004?target_url=https%3A%2F%2Ffb.me%2F643402985734299"
extras:
  al_applink_data: <Bundle containing App Link data>

In the code sample below, we're simply logging the target URL, but you should direct people through the appropriated flow for your app:


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this); 
    ...
    Uri targetUrl =
      AppLinks.getTargetUrlFromInboundIntent(this, getIntent());
    if (targetUrl != null) {
        Log.i("Activity", "App Link Target URL: " + targetUrl.toString());
    }
}

Wednesday, 4 November 2015

Twitter- Deep Linking with Android

Twitter - Deep Linking for Mobile Developers

The App Card provides the ability for users to download your app (if the user doesn’t already have it installed), or deep-link into your own app (if the app is already installed on the user’s mobile device).
We can achieve this things with Twitter Card.

https://dev.twitter.com/cards/mobile

We need to make dynamic Web page and host this page on any server which will be use for Sharing using Twitter from app. 

For demonstration purpose, we have implemented the same functionality in PHP with some hard-coded data but you need to convert in your back-end technology with dynamic data.

Please find PHP source code which we used for testing purpose.

Web Page Part
<html>
<head> 
<title>App Name</title>
<meta name="twitter:card" content="summary">

<meta name="twitter:site" content="App Name">
<meta name="twitter:title" content="App Name">

<meta name="twitter:description" content="App Name is a 100% free social sharing app that allows sharing of video, photo, audio and text all in one network.">
<meta name="twitter:image" content="http://app_name.vm39.sa92.info/ico.png">
<meta name="twitter:url" content="http://appname.com/">
<meta name="twitter:app:name:iphone" content="appname"/>
<meta name="twitter:app:id:iphone" content="570281083"/>
<meta name="twitter:app:name:googleplay" content="appname"/>
<meta name="twitter:app:id:googleplay" content="com.appname"/>
<meta name="twitter:app:url:iphone" content="appname://show?bid=54e58b4360afd71a53001f5e" />
<meta name="twitter:app:url:googleplay" content="appname://show?bid=54e58b4360afd71a53001f5e" />
</head>
<body>
<h1>Have something interesting here for particular Post, becasue when user click on link from Twitter it he/she will come to this page.</h1>
</body>
</html>

We have used summary card here.
https://dev.twitter.com/cards/types/summary
https://dev.twitter.com/cards/types/app

After making card, you have to validate your card with below URL.
https://cards-dev.twitter.com/validator

Android Part

At Android side, we need to do below things.
We need to add host and scheme in activity tag of Manifest file based on url defined in twitter app link url above.

Please find below code snippets below for reference.

AndroidManifest.xml

 <activity

            android:name=".ui.activities.SplashActivity"

            android:label="@string/app_name"

            android:launchMode="singleTop"

            android:screenOrientation="sensorPortrait"

            android:windowSoftInputMode="adjustResize" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

            <intent-filter android:label="@string/app_name" >

                <action android:name="android.intent.action.VIEW" >
                </action>
                <category android:name="android.intent.category.DEFAULT" >
                </category>
                <category android:name="android.intent.category.BROWSABLE" >
                </category>


                <!-- Accepts URIs that begin with "example://action" -->

                <data

                    android:host="show"
                    android:scheme="appname" >

                </data>

            </intent-filter>
        </activity> 



In Activity, You can retrieve app link url in oncreate() method.
With url, you can redirect user to our app's screen based on data retrieved from URL.

Please find below code snippets below for reference.

onCreate() 
if (getIntent().getData() != null

                && getIntent().getData().toString().length() > 0) {


            Logs.e(TAG, "Data:" + getIntent().getData());

        if (getIntent().getData().toString().startsWith("appname")) {


                twitterCallbackUrl = getIntent().getData().toString()
                        .substring(getIntent().getData().toString().lastIndexOf("?bid=") + 5,
                                getIntent().getData().toString().length());



                Logs.e(TAG, "twitterCallbackUrl:" + twitterCallbackUrl);
            }
        }




 
 

Sunday, 16 August 2015

Material design for Android

  Material design is a comprehensive guide for visual, motion, and interaction design across platforms and devices. Android now includes support for material design apps. To use material design in your Android apps, follow the guidelines defined in the material design specification and use the new components and functionality available in Android 5.0 (API level 21) and above.

Android provides the following elements for you to build material design apps:

  • A new theme
  • New widgets for complex views
  • New APIs for custom shadows and animations

For more information about implementing material design on Android, see Creating Apps with Material Design.

Material Theme

The material theme provides a new style for your app, system widgets that let you set their color palette, and default animations for touch feedback and activity transitions.












         Dark material theme                                                                            Light material theme  


Lists and Cards

Android provides two new widgets for displaying cards and lists with material design styles and animations:


The new RecyclerView widget is a more pluggable version of ListViewthat supports different layout types and provides performance improvements.



The new CardView widget lets you display important pieces of information inside cards that have a consistent look and feel.
For more information, see Creating Lists and Cards.

View Shadows
In addition to the X and Y properties, views in Android now have a Z property. This new property represents the elevation of a view, which determines:
  • The size of the shadow: views with higher Z values cast bigger shadows.
  • The drawing order: views with higher Z values appear on top of other views.
For more information, see Defining Shadows and Clipping Views.

Animations
The new animation APIs let you create custom animations for touch feedback in UI controls, changes in view state, and activity transitions.

These APIs let you:
  • Respond to touch events in your views with touch feedbackanimations.
  • Hide and show views with circular reveal animations.
  • Switch between activities with custom activity transitionanimations.
  • Create more natural animations with curved motion.
  • Animate changes in one or more view properties with view state change animations.
  • Show animations in state list drawables between view state changes.

Touch feedback animations are built into several standard views, such as buttons. The new APIs let you customize these animations and add them to your custom views.

For more information, see Defining Custom Animations.

Drawables

These new capabilities for drawables help you implement material design apps:
  • Vector drawables are scalable without losing definition and are perfect for single-color in-app icons.
  • Drawable tinting lets you define bitmaps as an alpha mask and tint them with a color at runtime.
  • Color extraction lets you automatically extract prominent colors from a bitmap image
For more information, see Working with Drawables.

References:

https://developer.android.com/design/get-started/principles.html
http://www.google.com/design/spec/material-design/introduction.html
https://developer.android.com/design/material/index.html 
https://www.youtube.com/watch?v=p4gmvHyuZzw
https://www.youtube.com/watch?v=XOcCOBe8PTc
https://www.youtube.com/watch?v=YaG_ljfzeUw

Friday, 17 October 2014

This blog useful when we have to update status on user's fan page/friend page instead of user's wall using Facebook sdk.

This kind of functionality implemented in Instagram.
After a lots of search I  have find some useful things which is useful to me.
I would like to share this thing with you. This might be useful to u also.

Parameter list Used in Request:
  • name
  • caption
  • description
  • picture
  • link
  • to
  • message
This is parameter required when we want to post on user's fan page. You can add or remove more parameter as per requirement.

We have to pass bundle in parameter so bundle created for parameter.
Bundle requestParams =new Bundle (); 

requestParams.putString(

                        "picture", filePath);

requestParams.putString("name",

                         TheApplication.getInstance().getString(R.string.app_name));

requestParams.putString("link",
                           "http://url");
Permission:
  <string-array name="fb_advance_permissions">

        <item>manage_pages</item>

   </string-array> 

1.) Retrieve login user's page list

This method retrieve page list created by login user. manage_page permission required to fetch user's page list.
/**

     * retrieve facebook user page list and ask for permission

     * 

     * @param pageListener

     *            - call success, error

     */

    public static void getUserPageList(

            final IFacebookCommonListener<String> pageListener) {

        Session session = Session.getActiveSession();

        sFBRedirectionState = FbRedirectionState.GET_PAGE_LIST;

        getPagesCallback = pageListener;



        if (getPagesCallback == null) {

            return;

        }

        if (session != null) {

            // if (!session.isOpened()) {

            // openSession();

            // }



            if (!session.isOpened()) {

                Logs.d(DEBUG_FACEBOOK_PUBLISH,

                        "Page error: !session.isOpened()");

                closeSession();

                openSession();

              

                return;

            }



            if (!canRetrievePages() && !isPagePermissionCalled) {

                getPagesPermissions(mStatusCallback);

                isPagePermissionCalled = true;

                return;

            }



            if (!canRetrievePages() && isPagePermissionCalled) {

                if (pageListener != null) {

                    pageListener.onError("Facebook can't retrieve pages");

                }

                session.removeCallback(statusCallback);

                sFBRedirectionState = FbRedirectionState.NONE;

                isPagePermissionCalled = false;

                return;

            }



            try {

                /* make the API call */

                Request request = new Request(session, "/me/accounts", null,

                        HttpMethod.GET, new Request.Callback() {

                            public void onCompleted(Response response) {

                                /* handle the result */

                                Logs.e(TAG, "Response:" + response);

                                if (response.getError() == null) {

                                    Logs.d("Pages",

                                            "Page success"

                                                    + response.getRawResponse());

                                    if (pageListener != null) {

                                        

                                        pageListener.onSuccess(response

                                                .getRawResponse());

                                    }

                                } else {

                                    Logs.d("Pages", "Page error: "

                                            + response.getError()

                                                    .getErrorMessage());

                                    if (pageListener != null) {

                                        pageListener

                                                .onError("Facebook can't retrieve pages");

                                    }

                                }

                            }

                        });



                Bundle requestParams = request.getParameters();

                requestParams.putString("access_token",

                        session.getAccessToken());

                request.setParameters(requestParams);

                request.executeAsync();

            } catch (Exception e) {

                // TODO: handle exception

                if (pageListener != null)

                    pageListener.onError("Facebook can't retrieve pages");

            }

            sFBRedirectionState = FbRedirectionState.NONE;

            getPagesCallback = null;

            isPagePermissionCalled = false;

        } else {

            startLogin();

        }

    }

2.) This method check for permission.
Check user has authorize permission or not.
/**

     * check for user's has taken manage_pages permission

     * 

     * @return

     */

    private static boolean canRetrievePages() {

        Session session = FacebookHelper.getSession();

        if (session != null) {

            try {

                if (!session.isOpened()) {

                    openSession();

                } else {

                    List<String> grantedPermissions = session.getPermissions();

                    String[] publishPermissions = mActivity.getResources()

                            .getStringArray(R.array.fb_advance_permissions);

                    return grantedPermissions.containsAll(Arrays

                            .asList(publishPermissions));

                }

            } catch (FacebookException e) {

                Logs.d(TAG, e);

            } catch (Exception e) {

                Logs.d(TAG, e);

            }

        }

        return false;

    }

3.) Ask user for permission
If user has not authorize permission. we have to authorize user for manage_page permission. We can use below method to ask for permission from user.
    /**

     * ask user for page permission.

     * 

     * @param callback

     */

    public static synchronized void getPagesPermissions(StatusCallback callback) {

        Session session = FacebookHelper.getSession();

        if (session != null) {

            try {

                if (!session.isOpened()) {

                    openSession();

                }

                List<String> grantedPermissions = session.getPermissions();

                List<String> neededPermissions = new ArrayList<String>();

                String[] publishPermissions = mActivity.getResources()

                        .getStringArray(R.array.fb_advance_permissions);

                for (String p : publishPermissions) {

                    if (!grantedPermissions.contains(p)) {

                        neededPermissions.add(p);

                    }

                }

                if (!neededPermissions.isEmpty()) {

                    if (callback != null) {

                        session.removeCallback(callback);

                        session.addCallback(callback);

                    }

                    Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(

                            mActivity, neededPermissions);

                    session.requestNewPublishPermissions(newPermissionsRequest);

                }

            } catch (FacebookException e) {

                Logs.e(TAG, e);

            } catch (Exception e) {

                Logs.e(TAG, e);

            }

        }

    }
4.) Post on Fan page in background.
We require page token which is we got from page list data. we get this token from page related data. This token used for publish status on user's page. Page id required on which we want to post data.
/**

     *  This method publish status on fan page

     * @param requestParams - requested param

     * @param uploadListener - for success, failure callback

     */

    public static void postOnFanPage(Bundle requestParams,

            final IFacebookCommonListener<Void> uploadListener) {



        requestParams.putString("access_token",

                Preferences.getFaceBookPageToken());



        Request request = new Request(Session.getActiveSession(),

                Preferences.getFacebookPageId() + "/feed", requestParams,

                HttpMethod.POST, new Request.Callback() {

                    public void onCompleted(Response response) {

                        /* handle the result */

                        if (response != null) {

                            Logs.d(TAG,

                                    "Page: publish reponse:"

                                            + response.toString());



                            FacebookRequestError error = response.getError();

                            if (error != null) {

                                Logs.d(TAG,

                                        "Page: publish Errror:"

                                                + error.getErrorMessage());



                                if (uploadListener != null) {

                                    uploadListener

                                            .onError("Facebook can't publish your content");

                                }



                            } else {



                                if (uploadListener != null) {

                                    uploadListener.onSuccess(null);

                                }



                            }

                        }



                    }

                });

        requestParams.remove("message");

        request.setParameters(requestParams);

        request.executeAsync();

    }
5.) Post on Fan page using feed dialog.
 Some time we need to post on user's fan page using feed dialog. for that we can use below method.

requestParams.putString("access_token",

                             Preferences.getFaceBookPageToken()); 


private static void publishPageFeedDialog(Bundle params) {

        WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(mActivity,

                Session.getActiveSession(), params)).setOnCompleteListener(

                new WebDialog.OnCompleteListener() {

                    @Override

                    public void onComplete(Bundle values,

                            FacebookException error) {

                        if (error != null) {

                            if (!(error instanceof FacebookOperationCanceledException)) {

                                mActivity.informUser(mActivity

                                        .getString(R.string.bback_shared_facebook_failed));

                            }

                            Logs.e("FACEBOOK_ANDROID", "error", error);

                        } else if (values != null

                                && values.containsKey("post_id")) {

                            mActivity.informUser(mActivity

                                    .getString(R.string.bback_shared_facebook_success));

                        }

                    }

                }).setFrom(String.valueOf(Preferences.getFacebookPageId())).build();

        feedDialog.show();

    }

I have put method here from project. So might be some reference class or not there.   
You can get basic idea how we can integrate.


Screen with FanPage list.




References:

https://developers.facebook.com/docs/graph-api/reference/v2.1/page
https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens https://developers.facebook.com/docs/graph-api/reference/v2.0/user/accounts https://developers.facebook.com/docs/sharing/reference/feed-dialog/v2.1
Please share in comment if you are facing any problem.

Thursday, 16 October 2014

This blog useful for display album list for image and video

Some times we need to display album wise image and video in android application.
I have made one sample example for this functionality.
I would like to share this example.This might be useful too you.

Below Android api used for display album wise images and videos.

Image API:

MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

Video API:
MediaStore.Video.Media.EXTERNAL_CONTENT_URI;


Features included in example:
  • Album list fetch and display with count
  • Video thumbnail display with duration
  • Image and Video thumbnail display for selected album
  • Get selected image/video path





Code Snippets:

1.) This method used for fetch image album list.

    private void getPhotoList() {

       

        String[] PROJECTION_BUCKET = { ImageColumns.BUCKET_ID,

                ImageColumns.BUCKET_DISPLAY_NAME, ImageColumns.DATE_TAKEN,

                ImageColumns.DATA };

      

        String BUCKET_GROUP_BY = "1) GROUP BY 1,(2";

        String BUCKET_ORDER_BY = "MAX(datetaken) DESC";



      

        Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;



        Cursor cur = getContentResolver().query(images, PROJECTION_BUCKET,

                BUCKET_GROUP_BY, null, BUCKET_ORDER_BY);



     

        GalleryPhotoAlbum album;



        if (cur.moveToFirst()) {

            String bucket;

            String date;

            String data;

            long bucketId;



            int bucketColumn = cur

                    .getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);



            int dateColumn = cur

                    .getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);

            int dataColumn = cur.getColumnIndex(MediaStore.Images.Media.DATA);



            int bucketIdColumn = cur

                    .getColumnIndex(MediaStore.Images.Media.BUCKET_ID);



            do {

                // Get the field values

                bucket = cur.getString(bucketColumn);

                date = cur.getString(dateColumn);

                data = cur.getString(dataColumn);

                bucketId = cur.getInt(bucketIdColumn);



                if (bucket != null && bucket.length() > 0) {

                    album = new GalleryPhotoAlbum();

                    album.setBucketId(bucketId);

                    album.setBucketName(bucket);

                    album.setDateTaken(date);

                    album.setData(data);

                    album.setTotalCount(photoCountByAlbum(bucket));

                    arrayListAlbums.add(album);

                    // Do something with the values.

                    Log.v("ListingImages", " bucket=" + bucket

                            + "  date_taken=" + date + "  _data=" + data

                            + " bucket_id=" + bucketId);

                }



            } while (cur.moveToNext());

        }

        cur.close();

          } 




2.) This method used for retrieve image count album wise.

    private int photoCountByAlbum(String bucketName) {

        try {

            final String orderBy = MediaStore.Images.Media.DATE_TAKEN;

            String searchParams = null;

            String bucket = bucketName;

            searchParams = "bucket_display_name = \"" + bucket + "\"";



            // final String[] columns = { MediaStore.Images.Media.DATA,

            // MediaStore.Images.Media._ID };

            Cursor mPhotoCursor = getContentResolver().query(

                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,

                    searchParams, null, orderBy + " DESC");



            if (mPhotoCursor.getCount() > 0) {

                return mPhotoCursor.getCount();

            }

            mPhotoCursor.close();

        } catch (Exception e) {

            e.printStackTrace();

        }



        return 0;



    }
 
3.) This method used for retrieve albumwise image list.
private void initPhotoImages(String bucketName) {

        try {

            final String orderBy = MediaStore.Images.Media.DATE_TAKEN;

            String searchParams = null;

            String bucket = bucketName;

            searchParams = "bucket_display_name = \"" + bucket + "\"";



            // final String[] columns = { MediaStore.Images.Media.DATA,

            // MediaStore.Images.Media._ID };

            mPhotoCursor = getContentResolver().query(

                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,

                    searchParams, null, orderBy + " DESC");



            if (mPhotoCursor.getCount() > 0) {



                cursorData = new ArrayList<MediaObject>();



                cursorData.addAll(Utils.extractMediaList(mPhotoCursor,

                        MediaType.PHOTO));         

            }

            // setAdapter(mImageCursor);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }
 
For Video you have to change only video uri in above queries.

You can download source code from Github.


Monday, 5 August 2013

Customn DropDown Spinner in Android

Hello everyone!

Here’s another cool Android tutorial that talks about creating your own DropDown Spinner widget in Android. From the Android developers page, a Spinner is a view that displays one child at a time and lets the user pick among them.Our Default Spinner provide Drop Down above 4.0 version. In this tutorial you will create a customized Spinner like Gmail app using library project. This is also work in 4.0 below version. You can create spinner like this:




Using this Library you can set following features:

Methods:

  • setItemTextColor(int color) - Set the item text font color in drop down.
  • setItemBackgroundColor(int color) - Set the item background color in drop down.
  • setItemTextSize (int size) - Set the item text size
  • setVisibleItemNo (int no) - Set the height of DropDown spinner equal to number of visible rows
  • addItem(String item) - Add the single string item in spinner
  • addItem(String item, int resId) - Add the single string item and image in spinner
  • setItems(String[] arr) - Set the string array in spinner
  • setItems(String[] arr, int[] ico) - Set the string array & image array in list. size of string array and image array required equal.
  • getSelectedPosition() - Return the position of currently selected item within the adapter's data set.
  • setSelectedPosition(int selectedPosition)- Set the currently selected item.

Listener : 

  setOnItemClickListener

  setOnItemSelectedListener


 You can set custom spinner control in xml like this:

<com.anky.dropdownspinner.DropdownSpinner
            android:id="@+id/my_spinner1" 
            android:layout_width="40dp" 
            android:layout_height="40dp" 
            android:background="@drawable/standard_menu_button_bg" 
            android:text="" />
You can set property like this:

private DropdownSpinner spinner1  = (DropdownSpinner) findViewById(R.id.my_spinner1); 

private String[] strings = { "Inter Milan", "AC Mila", "Manchesterb",
            "Barcelona", "Valencia", "Juventus" }; 

spinner1.setItems(strings);

spinner1.addItem("Test1"); 

spinner1.addItem("Test2");  

spinner1.addItem("Test3",R.drawable.ic_menu_moreoverflow_normal_holo_light);
 
spinner1.addItem("", R.drawable.ic_menu_moreoverflow_normal_holo_light);  

spinner1.addItem("Test4", -1); 

spinner1.setVisibleItemNo(5);  

spinner1.setItemTextColor(Color.BLACK); 

spinner1.setItemPadding(18, 15, 18, 15); 

spinner1.setItemTextSize(20); 

spinner1.setItemBackgroundColor(Color.WHITE);  

spinner1.setOnItemClickListener(new OnItemClickListener()  { @Override public void onItemClick(AdapterView paramAdapterView,  View paramView, int paramInt, long paramLong) 
{ // TODO Auto-generated method stub

 clickItem = "Center: Click item:" + paramInt; txtClick.setText(clickItem); 

 }  
});

 You can download source code from here.

Sunday, 5 May 2013

Google Plus Integration in Android


Before you can start integrating Google+ features in your own app, you must create a Google APIs Console project and initialize the PlusClient within your app.

The Google+ platform for Android has the following requirements:
  • A physical device to use for developing and testing because Google Play services cannot be installed on an emulator.
  • The latest version of the Android SDK, including the SDK Tools component. The SDK is available from the Android SDK Manager.
  • Your project to compile against Android 2.2 (Froyo) or higher.
  • Eclipse configured to use Java 1.6
  • The Google Play services SDK:
    1. Launch Eclipse and select Window > Android SDK Manager or run android from the command line.
    2. Scroll to the bottom of the package list and select Extras > Google Play services. The package is downloaded to your computer and installed in your SDK environment at <android-sdk-folder>/extras/google/google_play_services.

Step 1: Enable the Google+ API

To authenticate and communicate with the Google+ APIs, you must first register your digitally signed .apk file's public certificate in the Google APIs Console:

Step 2: Add Google play service library project in your project

Location of library project is here... <android-sdk-folder>/extras/google/google_play_services/libproject

Step 3: How do I check to see if the Google+ app is installed on the device?

int errorCode = GooglePlusUtil.checkGooglePlusApp(this);

if (errorCode != GooglePlusUtil.SUCCESS) {

GooglePlusUtil.getErrorDialog(errorCode, this, 0).show();

} 

Step 4: Initialize the PlusClient

The PlusClient object is used to communicate with the Google+ service and becomes functional after an asynchronous connection has been established with the service. Because the client makes a connection to a service, you want to make sure the PlusClient.disconnect method is called when appropriate to ensure robustness.

Your activity will listen for when the connection has established or failed by implementing the ConnectionCallbacks and OnConnectionFailedListener interfaces.

You have to create PlusClient object in Activity's onCreate() method.

mPlusClient = new PlusClient(this, this, this, Scopes.PLUS_PROFILE);

You can handle google plus sign in & sign out in your app like this.

if (v.getId() == R.id.sign_in_button) {

            if (!mPlusClient.isConnected()
                    && btnSignIn.getText().equals(
                            getString(R.string.btn_signin))) {

                mPlusClient.connect();

            } else if (mPlusClient.isConnected()
                    && btnSignIn.getText().equals(
                            getString(R.string.btn_signout))) {
                {
                    mPlusClient.clearDefaultAccount();
                    mPlusClient.disconnect();
                    btnSignIn.setText(getString(R.string.btn_signin));
                    textUserName.setText("");
                    txtlogin.setVisibility(View.GONE);

                }
            }

        }
When the PlusClient object is unable to establish a connection, your implementation has an opportunity to recover inside your implementation of onConnectionFailed, where you are passed a connection status that can be used to resolve any connection failures.
@Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub
        if (result.hasResolution()) {
            // The user clicked the sign-in button already. Start to resolve
            // connection errors. Wait until onConnected() to dismiss the
            // connection dialog.
            try {
                result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                mPlusClient.disconnect();
                mPlusClient.connect();
            }
        }
    }

    @Override
    public void onConnected() {
        // TODO Auto-generated method stub
        String accountName = mPlusClient.getAccountName();
        Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG)
                .show();
        btnSignIn.setText(getString(R.string.btn_signout));
        textUserName.setText(accountName);
        txtlogin.setVisibility(View.VISIBLE);

    }

Because the resolution for the connection failure was started with startActivityForResult and the code REQUEST_CODE_RESOLVE_ERR, we can capture the result inside Activity.onActivityResult. 

@Override
    protected void onActivityResult(int requestCode, int responseCode,
            Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, responseCode, data);
        if (requestCode == REQUEST_CODE_RESOLVE_ERR
                && responseCode == RESULT_OK) {
            mPlusClient.disconnect();
            mPlusClient.connect();
        }

    }

 

Features:

Sharing to Google+ from your Android app

The Share dialog provides a means for users to share rich content from your app into the Google+ stream, including text, photos, URL attachments and location. In addition, your app can use two advanced sharing options: interactive posts and deep linking.

you can share on google plus like this

if (v.getId() == R.id.share_button) {

            if (mPlusClient.isConnected()) {
                Intent shareIntent = PlusShare.Builder
                        .from(this)
                        .setText(
                                "Check out: http://example.com/cheesecake/lemon")
                        .setType("text/plain")
                        .setContent(
                                Uri.parse("http://example.com/cheesecake/lemon"))
                        .getIntent();
                startActivity(shareIntent);
            } else {
                Toast.makeText(this, "Please Sign-in with google Account", Toast.LENGTH_LONG)
                .show();
            }
}

 

 Getting people and profile information

After you have signed in a user with Google, you can access the user's age range, language, public profile information, and people that they have circled.

Please find attached demo for Google plus integration.

Demo Example:

You can download source code from here. Source Code Download


Refrences:

https://developers.google.com/+/mobile/android/getting-started#step_1_enable_the_google_api

http://developer.android.com/google/play-services/plus.html