No sound on touch button - android

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

Related

Repeatedly performing an action while button is clicked

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;
}
});

How to check If a view has been touched?

I'm using a third party video player in my app. The library provides enough functionalities like scroll video, pause, play video etc. And all those features will auto-hide after a few seconds. I need a few more controls over the player. So I put a toolbar on it ( It consists of the back, favorite, share buttons ). I need to enable this toolbar only If someone clicks on the player or even taps on it. And it must hide after a few seconds like the video player's controller. I tried using this,
youtubePlayerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
LogUtils.LOGE("youtubePlayerView.setOnClick", "called");
return true;
}
return false;
}
});
But in vain. Nothing is happening.
I want to detect If the user has clicked or tapped on the player.
Try Like this
public void onClickHandler(View v)
{
switch (v.getId()) {
case R.id.youtubePlayerView:
//Player Clicked
break;
}
}
This should work:
youtubePlayerView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// enable toolbar here!
}
});

Detect ongoing touch event

I am currently trying to detect an ongoing touch event in my Android app.
In detail I want to recognize within a fragment whether the user touches any part of the app's screen.
Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.
For example the first 1-2 seconds of touch are being detected but everything after won't.
Is there something like an "OnPressListener" or a workaround?
If it's a well defined gesture you are trying to detect, have a look at GestureDetector.
http://developer.android.com/reference/android/view/GestureDetector.html
You can use
aButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(getApplicationContext(), "Long Clicked " ,
Toast.LENGTH_SHORT).show();
return true; // <- set to true
}
});
on your aButton and if you are using API < 19 you have to add
android:longClickable="true"
Attribute to your aButton in layout xml.
I finally found it out.
The solution is to use a simple OnTouchListener:
private boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
pressed = true;
} else if ((action == MotionEvent.ACTION_UP)
|| (action == MotionEvent.ACTION_CANCEL)) {
pressed = false;
}
return true;
}

Android button onclick multitouch

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.

how do i make different touch and click behaviours for buttons?

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.

Categories

Resources