Google plusone (+1) button in Android application integration guide

Google Plus button images in Android app
Google Plus buttons in Android app

1) Initialize the android app in Developer console & enable Google+ API.

2) Create a sample project & provide ID. You can change auto generated one but make sure it should be unique

3) Go to API’s & Auth section present at left side of dashboard.

4) Out of all the API’s, look for Google+ API & turn it on.

5) Go to credentials, click on create new client ID, fill up the values. Choose installed application type as Android  provide the Package name & SHA1 fingerprint.

6) Client ID successfully created.

Perform below changes in Eclipse to display +1 in app.

1) Create a application project in Eclipse,

2) Include the “google-play-services.jar” in Libs folder,

3) Create a new Layout XML file,

 <relativelayout  
 xmlns:android="http://schemas.android.com/apk/res/android"  
 xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"  
 xmlns:tools="http://schemas.android.com/tools"  
 android:layout_height="match_parent"  
 android:layout_width="match_parent">  
 <textview android:id="@+id/id_helloword_text"  
 android:layout_height="wrap_content"  
 android:layout_width="wrap_content"  
 android:text="@string/hello_world">  
 <com .google.android.gms.plus.plusonebutton  
 android:id="@+id/id_plus_one_button_standard"  
 android:layout_centerinparent="true"  
 android:layout_centervertical="true"  
 android:layout_height="wrap_content"  
 android:layout_width="wrap_content"  
 plus:annotation="inline"  
 plus:size="standard">  
 </com></textview></relativelayout>  

Make sure to add xmlns:plus=”http://schemas.android.com/apk/lib/com.google.android.gms.plus” at the beginning of Relative layout. com.google.android.gms.plus.PlusOneButton is the Google+ button class

4) Now get the the plusonebutton object in Oncreate method, in “OnResume” method always refresh the plus one button. URL parameter will be play store app location code param will be 0 or above.

 public class MainActivity extends ActionBarActivity {  
  PlusOneButton mPbutton_standard;  
  private static final int REQUEST_CODE = 0;  
  //app's Google plus page URL  
  private static final String APPURL = "https://play.google.com/store/apps/details?id=YOUR_APPLICATION_PACKAGE_NAME";  
  @Override  
  protected void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.activity_main);  
  mPbutton_standard = (PlusOneButton)findViewById(R.id.id_plus_one_button_standard);  
  }  
  protected void onResume() {  
    super.onResume();  
    // Refresh the state of the +1 button each time the activity receives focus.  
    mPbutton_standard.initialize(APPURL, REQUEST_CODE);  
  }  
 }  

6) No extra permissions required in Manifest file, just connect phone to internet and try. If no net is available in mobile, then button will be grayed out.

To customize Google +1 button in android app, here are some examples.

for tall plusone button with bubble annotation use the below example

 <com .google.android.gms.plus.plusonebutton  
 android:id="@+id/id_plus_one_button_tall"  
 android:layout_centerinparent="true"  
 android:layout_centervertical="true"   
 android:layout_height="wrap_content"   
 android:layout_margintop="30dp"  
 android:layout_width="wrap_content"  
 plus:annotation="bubble"  
 plus:size="tall">  
 </com>  

For medium size +1 use the below code

  <com .google.android.gms.plus.plusonebutton  
 android:id="@+id/id_plus_one_button_medium"  
 android:layout_centerinparent="true"  
 android:layout_centervertical="true"   
 android:layout_height="wrap_content"  
 android:layout_margintop="30dp"  
 android:layout_width="wrap_content"  
 plus:annotation="bubble"  
 plus:size="medium">  
 </com>  

plus:size takes , “small”, “medium”, “standard”, “tall”. parameters.
plus:annotation works with  “inline”, “bubble”, “none” options