So I'm new to Android and have this wee app that has a variety of Buttons. The buttons do a variety of things, but of particular interest are the buttons that intent to another activity.
Because as I'm happily programming and testing along, I discover that I can double- and sometimes triple-tap these buttons.
I look for methods on the Button object that will allow me to specify the number of clicks that the button is allowed or whether the button should be (even briefly) disabled after a click. I find nothing of the sort.
Incredulous, I begin googling for a high-level discussion of this strange behavior. I find no interesting discussions, just suggestions about how to handle the issue on every single button in my app.
With a heavy sigh, I surrender to the time demands of my project, and add private variables to my activities (no static locals in Java. crap.), which the click-handling method uses to tell whether it's already busy handling a button click.
But still I wonder. Do Button events in Android really have to be explicitly disabled?
Edit: I'm looking for an answer of the form: "Yes (or no), and I know they have to be explicitly disabled because X".
The platform can't assume you only want to allow the button to be clicked once, or how frequently you should be able to click it. Just add logic to disable the button once you've clicked it, e.g.:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
v.setEnabled(false);
//Do other stuf
}
});
You can use droidQuery to set a button or a set of buttons to handle the first click event, then not any future click events. For example:
$.with(button).one("click", new Function() {
#Override
public void invoke($ d, Object... params) {
//Do stuff - this will only happen on the first click event
}
});
Related
I have an Android app, when a user taps a button multiple times quickly, same activity is initialized multiple times.
To prevent this, I added android:launchMode="singleInstance" in Manifest file. But now, when an activity calls itself, it doesn't work.
I also tried
Intent myintent = getIntent();
myintent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
But this didn't work either.
How can I prevent having multiple activities when user clicks a button multiple times quickly, and how can I have same activity call itself correctly. Thanks.
Yeah, this happens if you are "trigger happy". You can also in many situations use multi-touch to activate a bunch of options at the same time. If you really need to solve this, you can look at disabling elements like J Whitfield suggested (element.setEnabled(false) or element.setClickable(false)) or intercepting onTouch.
You could try disabling the button after the first click has been detected.
Button button = theView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.isEnabled()){
v.setEnabled(false);
}
//Call your new activity here
...activity stuff...
}
});
First of all i am learning android, I am testing my Andorid app in my Samsung Galaxy S1.
My app Function is: while i am pressing RandomNumber button, it will generate Random numbers and displaying in the screen in TextArea.
But i am facing the below issues.
The Device back button is allow user to go back. How i can avoid that? ( I have buttons defined in the program dynamically, only that Back button should work )
While shaking the phone or change the position of the phone, then the Random numbers are automatically generating. How to avoid that?. Please advise.
Button Creation Dynamic
final Button buttonToAdd = new Button(this);
buttonToAdd.setText("RandomNumber");
Listener:
buttonToAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Strvalue = (String) buttonToAdd.getText();
if (Strvalue.equals("RandomNumber"))
{
Randomnumbergeneration();
}
}
});
You can overwrite the Activities onBackPressed() method to handle the back-button click event.
#Override
public void onBackPressed() {
// put some code here or just do nothing
// don't call super.onBackPressed() if you want to disable the back function
}
But if you want to publish your application you should follow the official design guidelines and do not disable this behaviour because every android user is used to it and will find it unlikely if the back button does not work anymore.
I have a button that when pressed, will call the company. Now, I was doing some research and found that there is a way to include a context menu. I really like the context menu because it gives you so many options.
Do you think it would be a waste of code to set a context menu for a click of the button that when pressed will open up the options to add contact, call contact, sms contact, etc.? Is it necessary?
I did come across these:
Android opening context menu after button click
http://developer.android.com/guide/practices/ui_guidelines/menu_design.html#tour_of_the_menus
I think it would be a good feature to include. Thats what context menu is there for, to give more options. I think it would be good to give the user more options when the button is clicked. Well it makes more since anyway.
Heres how you would get the long click
Button downSelected = (Button) findViewById(R.id.downSelected);
downSelected.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
EDIT:
If you just want one click on the button just register its click listener like this..
downSelected.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
It would be nice to provide a big main button to call the number, and some additional mechanism, let's say a smaller + button to do more stuff with as you sugggested. Also a long click could be considered a right user interaction to provide with more features.
Just a user feeling...
I'm clear on how the id of my xml button gets cast as a Button and ultimately to the sayIt field, however...
Button sayIt = (Button) findViewById(R.id.sayit);
...is it setOnClickListener that "registers" with the Controller to be notified when the button is clicked? If so, then is View.OnClickListener() and its onClick(View v) method where Controller first tells my code, hey I've been clicked and this is what gets kicked up the food chain?
sayIt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Does something cool
}
});
For most use cases, yes, View.OnClickListener#onClick() is the standard "do something when this thing is clicked" idiom. I say most, because the actual underlying implementation is a bit more involved than that, and there are different ways in which the touch event travels up the view hierarchy, before being detected as a "click" event, and propagating back down the hierarchy as a click event -- but unless you're implementing custom views, and need to do custom touch-based tracking, you generally don't need to worry about those events.
For example, if you set a View.OnTouchListener on the view, you get every touch event (down, motion, up, and in supported devices, even multiple pointers). In the onTouchEvent() handler, if you return true, it tells the view "I was interested in this motion event, and I have consumed the event; therefore pretend that the event never happened and stop propagating/processing it" -- by doing that, you would actually interfere with the standard OnClickListener click event detection.
But in most cases, if you want something to happen because you clicked on a Button (surprise, surprise :), View.OnClickListener and View#setOnClickListener() are what you want.
I have an activity which contains QuickContactBadges. I'm looking for a way to either chain event listeners on the QuickContactBadge, or to call the default listener from within an override.
Specifically, what I am looking to do is have the QuickContactBadge, when clicked to show the QuickContact card, and then to setResult and finish, to close my activity.
So either I want to add a second listener to the badge in addition to the default one, or implement something like the following:
bdg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
QuickContactBadge bdg = (QuickContactBadge) view;
bdg.base.onClick(); // PSEUDO-CODE LINE
setResult(RESULT_CANCELED, null);
finish();
}
});
Are either of these methods possible, or is there some other way I should be doing this?
Well, the answer to what I was trying to do was not actually in an event listener at all.
The key to getting my activity to close when the QuickBadge is clicked was to add android:noHistory="true" to the activity definition in the application manifest file.
Though, it would still be interesting to know yes/no if there is a way to chain event listeners.