I have an App with 10 Buttons. Everytime the User presses on one Button A TextView should change. And if the User changes the focus and ,oves its finger to right, to the next button(without taking the finger off the screen) the seond button should be focused.
I tried it with setting an OnTouchListner to all buttons, but once the finger is moved the focus still stays on the first button:
btn1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
txt1.settext("1");
return false;
}
});
I hope you understood what I mean and can help me with this.
Thanks
Edit:
I found an Application which does that, here is a Video, so you can visualise what I mean.
Notice how I move the mouse(the finger in this case) and the Boxes change its focus:
http://www.youtube.com/watch?v=MRVFpNrBmsA&feature=youtu.be
To start in your onTouch() you should handle the events...
Something like
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
txt1.settext("1");
} else if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT){
txt1.settext("0");
}
return true;
}
Not sure if those are the proper actions to check against but it is just to give the idea.
Also I believe you should return true otherwise it means that the event wasn't processed and it gets stuck.
Related
I have a button that runs a code that changes the background to a random color on each click. I would also like to give the user the ability to keep on changing the background color as long as the button is clicked. I believe that the onTouchListener will be my best bet. However, I do not now how to implement the code correctly.
I tried on the onLongClickListener but found out that onLongClickListener doesn't work that way.
Incomplete code for the onTouchListener (randomize is the name of my button):
randomize.setOnTouchListener(new Button.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// start the thread
return true;
} else if(event.getAction() == MotionEvent.ACTION_UP){
// stop the thread
return true;
}
return false;
}
});
So, what I aim to be able to do is to keep on pressing the button and having the background continuously change while still preserving the onclick method of the button. So, onclick changes the background once and continuous click changes the background continuously.
Thank you so much folks :)
Ps. I'm just a beginner to android so I'm sorry if I do not know much. :)
try this:
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
count++;
Toast.makeText(getApplicationContext(),Integer.toString(count) , Toast.LENGTH_SHORT).show();
return true;
}
});
I have an activity with an expandableListView in it !
I would like to register to an event when someone clicks on the expandableListView, but not on a group nor an item. In other words, when there is space left under all items, i want to capture a click there.
I tried a lot of things, and the only thing that works for me is
expListView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(SetsActivity.this, "" + event.getY()), Toast.LENGTH_SHORT).show();
}
return false;
}
});
Unfortunately, this captures everything. I would like to be able to know if this is the empty space or not. Is there a way ?
Problem description:
I have a TextView on a RelativeLayout and I want to color it red when the user touches it, and go on another page when he clicks on it.
So I tried to set an OnClickListener to do the click, and an OnTouchListener to implement the touch function (MotionEvent.ACTION_DOWN) but this combination doesn't work, because OnTouchListener makes OnClickListener non-functional (don't know why).
On forums people say that we can implement the OnClick by the OnTouch MotionEvent.ACTION_UP, but this one can be triggered out of my TextView layout (the TextView gonna be clicked if you press it and drag your finger out of him to release) and this is not the desired behavior because I want:
click = press + release on the TextView.
Can someone give me a solution for this please?
you may call View.performClick() when action_up. Hope it helps.
your_txtView.setOnClickListener(new TextView.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
your_txtView.setOnTouchListener(new TextView.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
} else if (MotionEvent.ACTION_UP == event.getAction()) {
v.performClick();
}
return true;
}
});
Adel, is the problem with the first click, or you don't get any click at all?
There is this issue if you have multiple clickable layout you don't get any click events for the first. That's because it makes it first selected and then you get the click event, try the below code.
private class CustomTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
TextView tv = (TextView) v.findViewById(R.id.single_line_text);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
tv.setTextColor(COLOR_WHEN_PRESSED);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
tv.setTextColor(COLOR_WHEN_RELEASED);
// Action of click goes here
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
tv.setTextColor(COLOR_WHEN_RELEASED);
// To handle release outside the layout region
}
return false;
}
}
This is working in my current implementation if you set the touch listener for your layout.
You also need to set below on your layout
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
Hope it helps!!!
EDIT: Additionally, there should be a flag in both DOWN and UP. Set it in DOWN and check if its set in UP. This will avoid a bug where user might tap anywhere in the screen and then hover on your textview and release it.
Had the same problem. Solved it by returning false from ACTION_MOVE. I've been fighting with it for few hours, trying various things, but seems like i've kept overlooking this little issue... And now it makes sense. When you return true from onTouch, futher processing is stopped, so that OnClickListener is not aware of any movements and triggers onClick even after pointer have moved outside of view.
Im working on an app with a friend and i want the button to visually press by changing the background and vibrating and then when released it does whatever that button is supposed to do.
the buttons i have right now only have an onclick method but i want it vibrate when touched and execute their function when clicked
i know of the ontouch and onclick methods but i cant seem to use them togetherand have already implemented both onclicklistener and ontouchlistener
how might i manage this.
You could do this by only using the OnTouchListener:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
/* Change your button background and vibrate */
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
/* Button's functionality */
}
return true;
}
Hope it helps.
I'm working on a soundboard, however I've got a problem when it come to drag the finger over the screen to play the sounds for the buttons I drag the finger over.
Button Button3 = (Button)findViewById(R.id.button03);
Button3.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
mp.playSound(3);
}
return false;
}
});
Do anyone know how I can detect when a finger enter a button and not click the button?
Thanks :)
Look into onTouchEvent
public boolean onTouchEvent (MotionEvent event)
Since: API Level 1
Implement this method to handle touch screen motion events.
Parameters
event - The motion event.
Returns
True - if the event was handled, false otherwise.