title: Android sign in button in a fragment
body: <p>I'm trying to have a google sign in button to my android application.</p>
  <p>I googled and found quickstart tutorials and with them I created the following code.</p>
  <p>In AndroidManifest.xml i add the following:</p>
  <pre><code>   &lt;meta-data android:name=&quot;com.google.android.gms.version&quot;
    android:value=&quot;@integer/google_play_services_version&quot; /&gt;
  </code></pre>
  <p>my fragment xml (fragment_main_menu.xml):</p>
  <pre><code>&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;match_parent&quot;
  android:paddingLeft=&quot;@dimen/activity_horizontal_margin&quot;
  android:paddingRight=&quot;@dimen/activity_horizontal_margin&quot;
  android:paddingTop=&quot;@dimen/activity_vertical_margin&quot;
  android:paddingBottom=&quot;@dimen/activity_vertical_margin&quot;
  tools:context=&quot;com.tuxin.myalcoholist.myalcoholist.myalcoholist.MainMenuActivity$PlaceholderFragment&quot;&gt;
  
  &lt;Button
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:text=&quot;@string/add_drink&quot;
      android:id=&quot;@+id/add_drink&quot;
      android:layout_alignParentTop=&quot;true&quot;
      android:layout_centerHorizontal=&quot;true&quot;
      android:layout_marginTop=&quot;36dp&quot;
      android:onClick=&quot;addStringClickHandler&quot; /&gt;
  
  &lt;com.google.android.gms.common.SignInButton
      android:id=&quot;@+id/sign_in_button&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot; /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>my main menu activity contains the following code:</p>
  <pre><code>package com.tuxin.myalcoholist.myalcoholist.myalcoholist;
  
  import android.app.PendingIntent;
  import android.content.IntentSender;
  import android.support.v7.app.ActionBarActivity;
  import android.support.v7.app.ActionBar;
  import android.support.v4.app.Fragment;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.os.Build;
  import android.widget.Toast;
  
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.SignInButton;
  import com.google.android.gms.common.api.GoogleApiClient;
  import com.google.android.gms.plus.Plus;
  import com.google.android.gms.plus.model.people.Person;
  
  
  public class MainMenuActivity extends ActionBarActivity {
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_menu);
      if (savedInstanceState == null) {
          getSupportFragmentManager().beginTransaction()
                  .add(R.id.container, new PlaceholderFragment())
                  .commit();
      }
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main_menu, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  public void addCocktailClickHandler(View v) {
  }
  
  
  
  
  /**
   * A placeholder fragment containing a simple view.
   */
  public class PlaceholderFragment extends Fragment implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  
      private SignInButton mSignInButton;
      private GoogleApiClient mGoogleApiClient;
      private int mSignInProgress;
      private PendingIntent mSignInIntent;
      private int mSignInError;
  
      private static final int STATE_DEFAULT = 0;
      private static final int STATE_SIGN_IN = 1;
      private static final int STATE_IN_PROGRESS = 2;
      private static final int RC_SIGN_IN = 0;
  
  
      private static final String SAVED_PROGRESS = &quot;sign_in_progress&quot;;
  
      public PlaceholderFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
          mSignInButton = (SignInButton) rootView.findViewById(R.id.sign_in_button);
          mSignInButton.setOnClickListener(this);
          mGoogleApiClient = buildGoogleApiClient();
  
          return rootView;
      }
  
      @Override
      public void onStart() {
          super.onStart();
          mGoogleApiClient.connect();;
      }
  
      @Override
      public void onStop() {
          super.onStop();
          if (mGoogleApiClient.isConnected()) {
              mGoogleApiClient.disconnect();;
          }
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          outState.putInt(SAVED_PROGRESS, mSignInProgress);
      }
  
      @Override
      public void onClick(View v) {
          if (!mGoogleApiClient.isConnecting()) {
              // We only process button clicks when GoogleApiClient is not transitioning
              // between connected and not connected.
              switch (v.getId()) {
                  case R.id.sign_in_button:
                      Toast.makeText(getActivity().getApplicationContext(),&quot;connecting&quot;,Toast.LENGTH_LONG).show();
                      resolveSignInError();
                      break;
              }
          }
      }
  
      private GoogleApiClient buildGoogleApiClient() {
          // When we build the GoogleApiClient we specify where connected and
          // connection failed callbacks should be returned, which Google APIs our
          // app uses and which OAuth 2.0 scopes our app requests.
          return new GoogleApiClient.Builder(getActivity().getApplicationContext())
                  .addConnectionCallbacks(this)
                  .addOnConnectionFailedListener(this)
                  .addApi(Plus.API, null)
                  .addScope(Plus.SCOPE_PLUS_LOGIN)
                  .build();
      }
  
  
      private void resolveSignInError() {
          if (mSignInIntent != null) {
              // We have an intent which will allow our user to sign in or
              // resolve an error.  For example if the user needs to
              // select an account to sign in with, or if they need to consent
              // to the permissions your app is requesting.
  
              try {
                  // Send the pending intent that we stored on the most recent
                  // OnConnectionFailed callback.  This will allow the user to
                  // resolve the error currently preventing our connection to
                  // Google Play services.
                  mSignInProgress = STATE_IN_PROGRESS;
                  startIntentSenderForResult(mSignInIntent.getIntentSender(),
                          RC_SIGN_IN, null, 0, 0, 0);
              } catch (IntentSender.SendIntentException e) {
                  Toast.makeText(getApplicationContext(),&quot;Sign in intent could not be sent: &quot;
                          + e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
                  // The intent was canceled before it was sent.  Attempt to connect to
                  // get an updated ConnectionResult.
                  mSignInProgress = STATE_SIGN_IN;
                  mGoogleApiClient.connect();
              }
          } else {
              // Google Play services wasn't able to provide an intent for some
              // error types, so we show the default Google Play services error
              // dialog which may still start an intent on our behalf if the
              // user can resolve the issue.
              Toast.makeText(getApplicationContext(),&quot;play services error&quot;,Toast.LENGTH_LONG).show();
          }
      }
  
  
      @Override
      public void onConnected(Bundle bundle) {
          // Retrieve some profile information to personalize our app for the user.
          Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
          Toast.makeText(getApplicationContext(),currentUser.getDisplayName(),Toast.LENGTH_LONG).show();
          mSignInProgress = STATE_DEFAULT;
  
      }
  
      @Override
      public void onConnectionSuspended(int i) {
  
      }
  
      @Override
      public void onConnectionFailed(ConnectionResult connectionResult) {
          Toast.makeText(getApplicationContext(),&quot;onConnectionFailed: ConnectionResult.getErrorCode() = &quot;
                  + connectionResult.getErrorCode(),Toast.LENGTH_LONG).show();
  
          if (mSignInProgress != STATE_IN_PROGRESS) {
              // We do not have an intent in progress so we should store the latest
              // error resolution intent for use when the sign in button is clicked.
              mSignInIntent = connectionResult.getResolution();
              mSignInError = connectionResult.getErrorCode();
  
              if (mSignInProgress == STATE_SIGN_IN) {
                  // STATE_SIGN_IN indicates the user already clicked the sign in button
                  // so we should continue processing errors until the user is signed in
                  // or they click cancel.
                  resolveSignInError();
              }
          }
  
          // In this sample we consider the user signed out whenever they do not have
          // a connection to Google Play services.
          onSignedOut();
      }
  
      private void onSignedOut() {
          Toast.makeText(getApplicationContext(),&quot;signed out&quot;,Toast.LENGTH_LONG).show();
      }
  
  
    }
  }
  </code></pre>
  <p>I also understood that I must have an ARM emulator of version 4.4.2 in order for good play services to work in an emulator, so I did that.. and now when I click on the google sign in button it toasts the message &quot;connecting&quot; and after that &quot;play services error&quot;</p>
  <p>so what am I doing wrong? what am I missing ?</p>
  <p>any information regarding the issue would be greatly appreciated.</p>
  <h1>update</h1>
  <p>Now i'm using the google api x86 image. it allowed me to choose a User to login with, and then failed with error code 4 and then &quot;An internal error occured&quot; toast message</p>
  <p>do I need to sign my application first?
  I still didn't quite understand how google play knows to relate the application  I registered
  at console.developers.google.com. I didn't use any certificate or anything..</p>
  <p>any ideas?</p>
  <h1>another update</h1>
  <p>I created a new client ID based on the quickstart tutorial, I typed package com.tuxin.myalcoholist
  and i copyed the SHA1 string from the keytool command (using ~/.android/debug.keystore</p>
  <pre><code>keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore -list -v
  </code></pre>
  <p>I still get the same results.</p>