Pregunta

In my Android app I am giving the possibility to tap on a Google +1 button.

This works fine with the following code from the developpers source material (https://developers.google.com/+/mobile/android/recommend):

mPlusClient = new PlusClient.Builder(this, this, this)
    .clearScopes()
    .build();  

mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);

Now I would like to detect when the user taps on this button, so I tried this:

mPlusOneButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {                   
        Toast.makeText(this, "Thanks for your +1", Toast.LENGTH_LONG).show();
    }
});

But it doesn't do anything, it doesn't display the toast message.

As an alternative, I was thinking of having the player tap twice on the button : first being a regular Button object that would make the plus one button visible, second being the plusone button. but it is an awckward user experience

How can I detect user's tap on this button?

¿Fue útil?

Solución 3

Plus one button provides a listener function to detect button click. Here is the code.

    mPlusOneButton.setOnPlusOneClickListener(new PlusOneButton.OnPlusOneClickListener() {
        @Override
        public void onPlusOneClick(Intent intent) {
            Log.w(tag,"plus one click");

        }
    });

Otros consejos

Don't use setOnPlusOneClickListener (or any listener) with SDK 13 (and perhaps future versions), it induces a bug (infinite rotation of the progress indicator in the +1 button when you click on it).

Use ActivityForResult to trigger the user choice.

mPlusOneButton.setOnPlusOneClickListener(new PlusOneButton.OnPlusOneClickListener() {
                @Override
                public void onPlusOneClick(Intent intent) {
                    startActivityForResult(intent, 0);
                }
            });

Check the result code

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == -1) {
      // OK or SHARE

    } else {
      //UNDO

    }

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top