Adding a Google +1 button in Android App

家住魔仙堡 提交于 2019-11-27 11:55:33

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

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

To add google plus one, first you need to enable the API in developer console, then register your app with package name, then include the same in your app.

Here is the full example with detailed explanation.

http://www.feelzdroid.com/2014/09/google-plusone-1-button-in-android-application-integration-guide.html

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:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!