I have 2 buttons and I need to read onClick event from second button when first is pressed down now and v.v. Like in keyboards. How to do that?
Edit
No, no! I don't need to check was first button clicked or not. I need to listen another onClick events when first or second button is in ACTION_DOWN state couse if I press first button, I can't press second, but I have multitouch.
May be You could try the following code :
Declare a boolean variable in class.
private boolean button1IsPressed = false;
Write following code for button 1 :
button1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
button1IsPressed=true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
button1IsPressed=false;
}
}
};
For Button 2 You can do the following:
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(button1IsPressed){
//Write your to do code here
}
}
});
You could try with onTouchListeners. In the first button modify a boolean on the down and up event, in the second button, only perform an action when the boolean is true.
If the buttons are toggle buttons, what I think is the case then:
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Do something
} else {
// Disable vibrate
}
}
The main thing here is the isChecked() function, which may be used when checking which one is checked, so to execute something then. You can set in the XML of the two buttons the following:
android:onClick="onToggleClicked" then with isChecked determine which one is checked like this:
boolean on1 = ((ToggleButton) view1).isChecked();
boolean on2 = ((ToggleButton) view2).isChecked();
if (on1)
//do something with button2
if (on2)
//do something with button1
Cheers
There's a sample code in android-16/ApiDemos project called "Views -> Splitting Touches across Views" (SplitTouchView.java). In that sample enclosing LinearLayout has an attribute android:splitMotionEvents="true" which allows to scroll two list views simultaneously.
According to Android 3.0 API Overview this attribute appeared in this api version:
Previously, only a single view could accept touch events at one time. Android 3.0 adds support for splitting touch events across views and even windows, so different views can accept simultaneous touch events.
Related
I have a few imageview which have onclicklistener. If I press one (not release), I can press click others or I can click them same time. I do not want it. Everytime when I press one of them others should be disable to click.
imageview1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getMethod();
}
});
I guess, I tried setClickable(false); but it did not work properly, if I clicked one button after that it worked.
Try using onTouchListener instead of onClickListener and calling setEnabled(false); on the other views there. Here's a fairly basic example:
OnTouchListener onTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
imageView1.setEnabled(false);
imageView2.setEnabled(false);
}
return true;
}
};
And then apply it to the image views with:
imageView1.setOnTouchListener(onTouchListener);
That should work. One thing is, though, that while you'll only be able to push one button no matter what, you also won't be able to push anything after you let go - but, you can fix that by adding some logic to see if the view actually got clicked or if the user touched it, changed their mind and slid away. The (event.getAction() == MotionEvent.ACTION_DOWN) check will be true even if the user is just scrolling.
//button on which press u want to disable others
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button2.setEnabled(false); //button which u want to disable
button3.setEnabled(false); //button which u want to disable
}
});
//update fixed a spelling error
try to disable the button and
button.setEnable(false);
enable the button
button1.setEnable(true);
(Android 3.2) I have a TableLayout with 9 buttons. I want to know when any of them are un-pressed, i.e., when a press is complete, i.e., ACTION_UP. I don't care which button, I just want to know when any button which had been pressed has just been released.
I was hoping there was an Android::onTouch in the XML, like there is an Android::onClick, and I could point them all at one onTouch event handler to look for an ACTION_UP. But there isn't. I'm trying to avoid writing 9 separate OnTouchListeners.
Any suggestions?
Thanks in advance.
why don't you add onTouchListener from code and indeed, do what you want to do when you have ACTION_UP, as seen here
You can just assign each view to a single onTouch listener programmatically instead of in the XML.
Make one listener:
private View.OnTouchListener myListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Your logic
return false;
}
};
and then add that to each button.
findViewById(R.id.btn1).setOnTouchListner(myListener);
findViewById(R.id.btn2).setOnTouchListner(myListener);
Optimally you could create these buttons programmatically instead of referencing them from xml so you can do this in loops, or possibly put them in one ViewGroup and iterate though its children and add the listeners that way.
why not just create a single listener:
OnTouchListener listener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Button b = (Button) v; // the button that just gets released
if (event.getAction() == MotionEvent.ACTION_UP) {
...
}
}
});
and attach to all the buttons using a loop:
Button buttons[] = {...};
for (Button b : buttons) {
b.setOnTouchListener(listener);
}
or alternatively:
int button_ids[] = {R.id.button1, R.id.button2, R.id.button3, ...};
for (int id : button_ids) {
((Button) findViewById(id)).setOnTouchListener(listener);
}
If I use:
final Button btnNught = (Button) findViewById(R.id.night);
btnNught.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
btnNught.setPressed(true);
}
});
And if I click on button I listen click sound, but if I use:
btnNught.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (btnNught.isPressed()) {
btnNught.setPressed(false);
return true;
}
else {
btnNught.setPressed(true);
return false;
}
}
});
I do not listen any sound on touch.
Where I made mistake?
Edit:
I want to create button like togglebutton with two state: pressed and non-pressed with two different colors of course (normal-light gray non pressed and green when pressed).
In this case (above), when I touch button I do not listen any sound, but if I use onClick method I listen 'click' sound.
How can I get 'click' or 'touch' sound when I use onTouch method?
Doing this works for me:
btnNught.setSoundEffectsEnabled(true);
btnNught.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()!=MotionEvent.ACTION_UP ||
event.getAction()!=MotionEvent.ACTION_DOWN)
{
return false;
}
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
btnNught.playSoundEffect(SoundEffectConstants.CLICK);
//Set whatever color you want to set
}
else
{
}
return true;
}
});
Please, on what conditions could you get to the btnNught.setPressed(true); in the second example? As I see, you simply can't press the button. On Action Down you consume the action, so button won't be pressed.
Edit: Put a breakpoint at the start of listener and enter it in the different variants of the button use. And check the behaviour.
Try adding android:soundEffectsEnabled="true" to your <Button> tag.
Your question is not very clear over when you want to play the sound but I am answering your question assuming that you wanna play it when the Action_DOWN occurs.If you plan to create a toggle button your code should look something like this
btnNught.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()!=MotionEvent.ACTION_UP ||
event.getAction()!=MotionEvent.ACTION_DOWN)
{
return false;
}
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
btnNught.setPressed(true);
//Set whatever color you want to set
}
else
{
btnNught.setPressed(false);
}
return true;
}
});
Following line will play the click sound:
btnNught.playSoundEffect(SoundEffectConstants.CLICK);
There are two ways to listen sound
1 use inbuilt sound
Go to Settings -> Sound ->
And do the following settings
sound profile set **Normal**
Touch sound set **ON**
use custom sound
play the user defined sound
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.