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?
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
}
}
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");
}
});
Related
hi i am currently working on QR code scanner. I want to ask if it is possible to open another activity after the QR code have been scanned rather than getting it displayed as a toast?
below is my scanner file code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtexthello=(TextView)findViewById(R.id.textview_hello);
scanbtn=(Button)findViewById(R.id.btn1) ;
final Activity activity=this;
scanbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator l=new IntentIntegrator(activity);
l.setDesiredBarcodeFormats(l.QR_CODE_TYPES);
l.setPrompt("scan");
l.setCameraId(0);
l.setBeepEnabled(false);
l.setBarcodeImageEnabled(false);
l.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult res= IntentIntegrator.parseActivityResult(requestCode, resultCode,data);
if(res!=null)
if(res.getContents()==null)
{
Toast.makeText(this,"u cancelled scanning",Toast.LENGTH_LONG).show();
}
else
{
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
// Toast.makeText(this,res.getContents(),Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
At a first reading it sounds as if the issue is how you're attempting to start the next activity, if not the activity itself.
Start a new app, and use a simple Button as the trigger for the new activity. Once you have the syntax and requirements correct there, apply what you've learned to the QR app.
If you're able to get the Toast to display from your existing code, then your difficulty has nothing to do with the QR code.
Maybe what you really need is to look at something like this (Start an Activity with a parameter) where you pass a parameter to the second activity.
I am trying to display the Drop-in UI in my app upon clicking a specific button. I have used the guide from Braintree site but for some reason nothing is happening.
Code below:
OnClick function:
public void onClick(View v){
switch (v.getId()){
case R.id.showUI_button:
onBraintreeSubmit(v);
break;
}
}
Drop-in functions:
public void onBraintreeSubmit(View v) {
PaymentRequest paymentRequest = new PaymentRequest()
.clientToken(token)
.amount("$10.00")
.primaryDescription("Awesome payment")
.secondaryDescription("Using the Client SDK")
.submitButtonText("Pay");
startActivityForResult(paymentRequest.getIntent(this), REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == BraintreePaymentActivity.RESULT_OK) {
PaymentMethodNonce paymentMethodNonce = data.getParcelableExtra(
BraintreePaymentActivity.EXTRA_PAYMENT_METHOD_NONCE
);
String nonce = paymentMethodNonce.getNonce();
// Send the nonce to your server.
}
}
}
I have checked that the token is returned from the server.
I have also tried by setting the onClick via the xml code of the button and removing the onClick from the java file but the result is the same, no UI shown.
The log has only two lines
performCreate Call Injection Manager
Timeline: Activity_idle id:android.os.BinderProxy#etc
Any ideas? If more info is needed to understand better let me know
Actually I found this there is a "BraintreeFragment" set up part. Braintree documentation needs to be more clear on this I think.
https://developers.braintreepayments.com/guides/client-sdk/setup/android/v2
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
mBraintreeFragment = BraintreeFragment.newInstance(this, mAuthorization);
// mBraintreeFragment is ready to use!
} catch (InvalidArgumentException e) {
// There was an issue with your authorization string.
}
}
The above code should work along with the previous code posted. mAuthorization is the token and needs to be valid to show the payment screen (so the variable "token" in the previous code posted which in my code I just have as private but visible from the whole activity).
Try with the test token that they have on their page and if this works then the main setup is ok.
https://developers.braintreepayments.com/start/hello-client/android/v2
For setting up tokens on your server, they have further documentation so that those test tokens work on the sandbox.
I added a +1 button in my Android app. I would like to add a callback in order to know what happened after the user clicked on the +1 button (did he validate its +1 ?, did he abort ? ...)
How can I do that ?
Thanks !
You can add a listener to check when the button is clicked and later check the result of the activity.
static final int PLUS_ONE_REQUEST = 1;
...
mPlusOneButton.setOnPlusOneClickListener(new PlusOneButton.OnPlusOneClickListener() {
#Override
public void onPlusOneClick(Intent intent) {
//here you can handle the initial click
//Start the activity to display the +1 confirmation dialog.
startActivityForResult(intent, PLUS_ONE_REQUEST);
}
});
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PLUS_ONE_REQUEST) {
switch(resultCode) {
case RESULT_OK:
//here the operation was successful
break;
case RESULT_CANCELED:
//here the user backed out or failed
break;
}
}
}
Sources:
Handling the click
Getting a result from an activity
I hope that this is what your were asking, and more importantly that this was helpful.
First post, so please go easy.
I have an app with a handful of tabs, the first is opened on running the app.
One of the tabs is 'My Account' (a ListActivity) showing account options. I switch to this and if the user is not logged in, it, in turn, runs a UserLogon activity using the following:
Intent logonActivity = new Intent(getBaseContext(), UserLogon.class);
startActivityForResult(logonActivity, 0);
To catch the result, I use the following code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
MainBlock ta = (MainBlock) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);
finish();
}
if (requestCode == 100)
{
showAccountOptions();
}
}
In the UserLogon class, I have the usual fare; TextView's and Button's. The intention is that if the user cancels the login, it will revert to the initial tab, or if login is successful, it will show the Account Options. And this is indeed what does happen.
All good so far.
The problem I'm having is that if I cancel the login and return to the first tab, when I select the Accounts tab again, I'm not presented with the UserLogon activity. I was under the impression that finish() would close the UserLogon activity, and the Accounts activity but it would appear not.
So, my question is simply, how do I, in effect, restart the Accounts activity so that the user would be presented with the option to login once more.
We're good people and all willing to help ;-) I'll give it a shot. Still, I'm not quite sure I get that all right.
Basically you have an TabActivity which you setup and where you do something like that:
myTabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if (tabId.equals("account") && !loggedIn) {
Intent logonActivity = new Intent(getBaseContext(), UserLogon.class);
startActivityForResult(logonActivity, 0);
}
}});
You're basically saying that the first Activity start of UserLogon works, but the second one doesn't, right? Did you debugged to that point to check whether you reach the code which starts the activity again?
Update based on comment
Your UserLogon should always provide a result information, here's a blueprint:
public class UserLogon extends Activity {
public void onCreate(Bundle bundle) {
// ... do something ...
// if activity is canceled this will be the "last" result
setResult(RESULT_CANCELED);
}
public void checkLoginOrSomethingLikeThat() {
Intent result = new Intent();
// put your results in this intent
setResult(RESULT_OK, intent);
// close activity since we have the information we need
finish();
}
}
The parent activity which is waiting for the result should do it like that:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// it's good practice to use a constant for 0 like LOGIN_REQUEST
// otherwise you will ask yourself later "What the hell did 0 stand for?"
if(requestCode == 0){
if (resultCode == RESULT_OK) {
// get needed data from data intent and react on that
} else {
// no success, react on that
}
}
// ... I don't know what your resultCode 100 is but handle itwith the same pattern
}
I am using following code to open Android Contacts
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnContacts = (Button) findViewById(R.id.btn_contacts);
txtContacts = (TextView) findViewById(R.id.txt_contacts);
btnContacts.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
txtContacts.setText("");
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
//display picked contact data.
}
}
}
Now I want to put Button at the top of this Contact activity when opened or add my own Menu in this Activity
Can any one guide me? Is this possible or not? If yes then please tell how to achieve this?
I don't believe that is possible as each Activity in Android is working on its own and by starting the Intent, you are basically giving the new Activity the focus (and control).
One way to do something like this would be to build a custom contact list activity that uses the public data providers to access the contacts and simply lists them then. Then you could add as many custom functions as you like or even add Intents for original actions (like viewing a contact's details).