30

I was just wondering if there was anyway to add a Google +1 button inside my Android app. I have seen a +1 on the Android Market so I would think there would be some way to do this.

Kara
  • 6,115
  • 16
  • 50
  • 57
Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66

3 Answers3

23

With the Google+ platform for Android, you are now able to integrate a native +1 button in your Android app.

1) You first need to initialize the PlusClient object in your Activity.

2) Include the PlusOneButton in your layout:

    <com.google.android.gms.plus.PlusOneButton
        xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
        android:id="@+id/plus_one_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        plus:size="standard"
        plus:annotation="inline" />

3) Assign the PlusOneButton to a member variable in your Activity.onCreate handler.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPlusClient = new PlusClient(this, this, this);
    mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
}

4) Refresh the PlusOneButton's state each time the activity receives focus in your Activity.onResume handler.

protected void onResume() {
    super.onResume();
    // Refresh the state of the +1 button each time the activity receives focus.
    mPlusOneButton.initialize(mPlusClient, URL);
}

For more information, see https://developers.google.com/+/mobile/android/#recommend_content_with_the_1_button

Chirag Shah
  • 3,654
  • 22
  • 29
  • 2
    You should remove the Scopes.PLUS_PROFILE according to the last update – Ilya Gazman Feb 06 '13 at 16:04
  • 3
    The PlusClient Constructor is no longer supported, you should use the PlusClient.Builder$build – Kirill Kulakov May 29 '13 at 13:38
  • 4
    what is the URL variable? – Cuong Thai Sep 04 '13 at 09:02
  • @CuongThai that is the actual URL that you want the user to add +1 – Darko Petkovski Oct 23 '13 at 12:10
  • 2
    In my case, I put the link to the play store: https://play.google.com/store/apps/details?id=com.package.app – Cuong Thai Oct 24 '13 at 07:24
  • Kirill Kulakov: can you edit the code or give a correct answer with PlusClient.Builder ? I have the error explained in the following post http://stackoverflow.com/questions/19713451/google-plus-android-api-the-method-setactionsstring-is-undefined-for-the-t# when using PlusClient.Builder or when trying to run the Google+ sample app by Google. Thanks for your help ! – toto_tata Oct 31 '13 at 18:12
  • There is no method in PlusOneButton which takes (PlusClient,URL) as arguments. Also, is a PlusClient really required now? It worked for me without the PlusClient for 2-3 weeks for me until all of a sudden it stopped working. The button is always frozen for no reason. – Rahim Dec 27 '13 at 14:41
  • The button is frozen because with the new update of google play services library, you can only +1 when the user is signed in.The previous library was better as it signed in the user when clicking +1 button but with the new one you have to first sign in the user then the google plus button will be activated. Fellow googlers told me they are working on the fix but we still dont know when the update will be ready. – user632905 Dec 28 '13 at 08:43
  • 3
    This answer is outdated. mPlusClient is no longer required and anyways mPlusOneButton.initialize() won't take it. Take a look [here](https://developers.google.com/+/mobile/android/recommend) for new approach – Atul Goyal Apr 16 '14 at 12:39
10

the accepted answer is outdated....

XML :

<com.google.android.gms.plus.PlusOneButton
  xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
  android:id="@+id/plus_one_button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  plus:size="standard"
  plus:annotation="inline" />

Activity :

// The request code must be 0 or greater.

    private static final int PLUS_ONE_REQUEST_CODE = 0;

protected void onResume() {
    super.onResume();
    // Refresh the state of the +1 button each time the activity receives focus.
    mPlusOneButton.initialize(URL, PLUS_ONE_REQUEST_CODE);
}

and even before that fallow this link :

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

Jesus Dimrix
  • 4,378
  • 4
  • 28
  • 62
1

With new android studio(2.2.2 that's what i'm using) you can do it more easily. There is built in feature to create fragment with +1 button. You can use the layout or initialization code for PlusOneButton in an activity or anywhere you want. Check the following image: enter image description here

Edit: Don't forget to configure your app in Google api console

Alvi
  • 767
  • 8
  • 20