Android

+1 Button with Google Play Services

Alex Zhukovich 1 min read
+1 Button with Google Play Services
Table of Contents

Today, I'm talking about integrating Google's "+1" button to the Android application. This allows users to share your application in "Google+".

Firstly, you need to add a dependency to the gradle file.

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

Afterward, we need to add a "+1" button to your layout. A size of the PlusOneButton button may be standard, tall, small or medium. We need to change the size attribute for changing the size of the button.

Size of the "+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" />

The next step is updating an activity.

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();
    }
}

The last step is to change the app_package to your application package.

+1 button application demo

Mobile dev & testing with Alex Zhukovich

A blog about Android development & testing, Best Practices, Tips and Tricks

Share
Comments
More from Mobile dev & testing with Alex Zhukovich
Jetpack Compose: RadioButton
Jetpack Compose

Jetpack Compose: RadioButton

Many mobile apps ask a user to provide data, and RadioButton component can simplify the onboarding process and how users can fill the information. I'll show you the possibilities of "RadioButton", how to customize and test it in JetpackCompose.
Alex Zhukovich 9 min read

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Mobile dev & testing with Alex Zhukovich.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.