+1 Button with Google Play Services


Today I’m talking about integration Google’s +1 button with Android application.

This let’s improve rating of your application in Play Store.

Firstly need to add library or add a dependency to gradle file.

dependencies {
    compile 'com.google.android.gms:play-services:7.3.0'
}

After it need to add +1 Button to your layout. Size of PlusOneButton may be standard, tall, small or medium. For changing size of button need to change size attribute.
size of +1 button

<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="none" />

Next step is updating an activity.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.PlusOneButton;

public class MainActivity extends AppCompatActivity {
    private static final int PLUS_ONE_REQUEST_CODE = 0;
    private static final String APP_URL = "https://play.google.com/store/apps/details?id=app_package";
    private PlusOneButton mPlusOneButton;
    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plus_one);

        mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .useDefaultAccount()
                .build();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPlusOneButton.initialize(APP_URL, PLUS_ONE_REQUEST_CODE);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mGoogleApiClient.disconnect();
    }
}

You must change app_package to your application package.
+1 button application demo

CategoriesUncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *