The Google +1 button can be a powerful way to increase the reach and engagement of your content on the web. Here's a quick and easy way to add a +1 button to your Android app:
- First, create a +1 button widget in your app's layout:
<!-- Place this where you want the +1 button to go -->
<com.google.android.gms.plus.PlusOneButton
android:id="@+id/plus_one_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp" />
- Then, find the view in your activity's code and get a reference to the +1 button:
// In your onCreate() method:
PlusOneButton mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
- Set up an OnPlusOneClickListener for the button:
mPlusOneButton.setOnPlusOneClickListener(new OnPlusOneClickListener() {
@Override
public void onPlusOneClick(int status) {
// status can be either PlusOneButton.STATUS_SUCCEEDED or PlusOneButton.STATUS_ERRORED
Log.d(" PlusOneButtonSample ", " Clicked! Status: " + status);
}
});
- That's it! When the button is clicked, your callback will be invoked with the current PlusOneButton status.