i have an application with an imagebutton that has both an onclick and an onlongclick listener. However, when the button is long pressed, both of these listeners are executing. Any suggestions?
d1.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
selectMode = true;
dockNum = 1;
sd1.open();
d1.cancelLongPress();
return false;
}
});
...d1.setOnClickListener(this);
...case R.id.d1:
if(d1s.equals("empty")) {
selectMode = true;
dockNum = 1;
sd1.open();
} else {
Intent d1i = pm.getLaunchIntentForPackage(d1s);
startActivity(d1i);
}
break;
I think your problem has to do with the fact that you're returning false in your onLongClick method. Try returning true instead (despite the fact that you're canceling the long click, returning true is just saying "I've handled this, no further action is required.").
Related
I'm building a simple counter, it has buttons to add or subtract from a total value, I want it to keep adding if I hold the button, incrementing +5 every second, but i'm having problems to make it work. I'm using onClickListener, but i can't find a way to make it work "together" with on touch listener.
pl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
lf++;
lt.setText(Integer.toString(lf));
}
}
You will have to use handler to schedule a runnable that increments +5 every 1 second (+1 per 200 milliseconds to keep it smooth)
Handler handler = new Handler();
Runnable incrementTask = new Runnable(){
#Override
public void run(){
lf++;
handler.postDelayed(incrementTask, 200); //Execute after 200 milliseconds
}
};
Then you implement onTouchListener and post this runnable when ACTION_DOWN is dispatched and cancel it when ACTION_UP is dispatched.
boolean buttonPressed = false;
pl.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//This check is imporant since ACTION_DOWN might be called
// multiple times when finger is moving
if(!buttonPressed){
buttonPressed = true;
handler.post(incrementTask);
}
} else if(event.getAction() == MotionEvent.ACTION_UP){
if(buttonPressed)
{
buttonPressed = false;
handler.cancel(incrementTask);
}
}
return false;
}
});
Please note that this logic won't work well with your current click listener. For better user experience, I would recommend that you start this timer/runnable only when your button is long pressed. I had a similar situation in a project so I wrote a utility class to help me detect when a button is being held and released after a long click. Normal clicks work fine as well. You can find my ClickAndHoldManager class on Github.
To use it, you simply pass your view in the constructor and set a listener:
ClickAndHoldManager manager = new ClickAndHoldManager(myButton);
manager.setClickCallback(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Regular Click
}
});
manager.setHoldCallback(new ClickAndHoldManager.HoldListener() {
#Override
public void holdStarted() {
// do handler.post() here
}
#Override
public void holdEnded() {
//do handler.cancel() here
}
});
I have a customView , i want to set onClick which will only be called on the very first click. In which i want to start a thread which will start a counter on other TextView , with simple onClickListener with each click a new threads starts which is a problem . How can i achieve such task ?
Another option is in your onClick() method do set a null listener, i.e.
#Override
public void onClick(View view) {
// disable any other clicks from now on
customView.setOnClickListener(null);
...
}
I think this is only logic problem, So I solve this problem by using a boolean variable for the first click:
boolean isFristClick = true;
#Override
public void onClick(View v) {
if (isFristClick) {
// Start your counter Thread here
isFristClick = false;
} else {
// Do nothing
}
}
What about making a workaround for that?!! like assigning a boolean value to tell if it's the first click:
private boolean first_click = true;
your_view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(first_click){
first_click = false;
// Do something on first click
}else{
// Do another thing on later clicks
}
}
});
I'm looking around internet to have the possibility to press a button just two times. I'm implementing the code for a poker game, then the player should press the button one time to show the card and the second time to change it. How can I do it. The only thing I found was to click it one time (that I don't need because it has also to change a card, but just one time). That's what I have for the first button:
backgroundA.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
int randomInt1 = random1.nextInt(Deck.length());
int drawableIDA = Deck.getResourceId(randomInt1, -1);
backgroundA.setBackgroundResource(drawableIDA);
}
});
You need to have a flag that will check for click, if you click it once the flag will be true and if you click it again your statement inside your OnClickListener will get executed.
sample:
boolean flag = false;
boolean flag2 = false;
backgroundA.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if(!flag){ flag = true }
else
{
if(flag2) { flag = false; }
else
{
int randomInt1 = random1.nextInt(Deck.length());
int drawableIDA = Deck.getResourceId(randomInt1, -1);
backgroundA.setBackgroundResource(drawableIDA);
flag2 = true;
}
}
}
});
I have a listview with item. Click on an item it goes to another activity. My problem is on clicking on multiple items (say 3 0r4 ) all clicked are loaded. But i dont need it. Only want to load 1 item at a time. Tested on HTC one,samsung galaxy s plus. Please help.
You can detect multitouch and neglect it.
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getPointerCount() > 1) {
System.out.println("Multitouch detected!");
return true;
}
else
return super.onTouchEvent(event);
}
use the follow method can solve that:
public class FastClickUtil {
private static long lastClickTime;
public synchronized static boolean isFastClick() {
long time = System.currentTimeMillis();
if ( time - lastClickTime < 500) {
return true;
}
lastClickTime = time;
return false;
}
}
put that method in your onItemCLickListner or in your adapter‘s getview like me
holder.title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// 解决短时间内同时点击多个item
if (FastClickUtil.isFastClick()) {
return;
} else {
Message msg = Message.obtain();
msg.what = MSG_WHAT_ONITEM_CLICK;
// Bundle data = new Bundle() ;
// msg.setData(data) ;
msg.obj = menuItem.getTitleResId();
getHandler().sendMessage(msg);
}
}
});
To stop this, Use this in your list view.
android:splitMotionEvents="false"
In my android app I have the following code in the OnCreate function:
txtUsername.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
String Username = txtUsername.getText().toString();
if (arg1 == KeyEvent.KEYCODE_ENTER) {
DontShowDialog = false;
if ((Username.toLowerCase().endsWith("blabla.com") == false && Username.toLowerCase().endsWith("blabla-bla.nl") == false) || validateEmail(Username) == false) {
final Dialog dialog = new Dialog(arg0.getContext());
dialog.setContentView(R.layout.startdialog);
dialog.setTitle("Warning 1");
Button btOk = (Button) dialog.findViewById(R.id.btOk);
btOk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DontShowDialog = true;
dialog.dismiss();
return;
}
});
if (DontShowDialog == false) {
dialog.show();
}
return false;
}
}
txtUsername.clearFocus();
txtPassword.setNextFocusDownId(txtPassword.getId());
return false;
}
});
When the user hits the NEXT button and the username is not right, a dialog is shown which can be canceled by the OK button.
But.... after hitting the OK button, the dialog is shown again... I don't want that.
Why is that happening?
rg,
Eric
By returning false in your onKey() event, you're telling Android that the KeyEvent was not consumed - so my guess is that changing the return statement to true might fix the problem, because Android would know the event was consumed, and it would not re-enter the onKey() method. If you could try that out and share the results that would be great!
Solved it.
moved the line
DontShowDialog = true;
from the OnClick part
Changed the if-then below the onClick to:
if (DontShowDialog == false) {
dialog.show();
DontShowDialog = true;
return false;
}