Reputation: 2326
I'm making an app with using Xamarin.forms. You might know forms' button is not enough to use as image button if you tried one.
So I use Image as a button and add gesturerecogniger. It's working fine. Good thing is that I can use all Image's bindable property same like using Image. (like 'Aspect property' and else)
Only problem is that Android button has sound effect when it's pressed. Mine doesn't have.
How to play default button sound on Android?
[another try]
I tried to make layout and put Image and empty dummy button on it. But If I do this, I can't use any property of Image or Button unless I manually link it.
So I think it's not the right way.
Thanks.
Upvotes: 9
Views: 5154
Reputation: 2378
Xamarin Forms:
PCL interface:
interface ISoundService { void Click(); }
Click handler:
void Handle_OnClick(object sender, EventArgs e) {
DependencyService.Get<ISoundService>().Click();
}
Android:
public class MainActivity {
static MainActivity Instance { get; private set; }
OnCreate() {
Instance = this;
}
}
class SoundService : ISoundService {
public void Click() {
var activity = MainActivity.Instance;
var view = activity.FindViewById<View>(
Android.Resource.Id.Content);
view.PlaySoundEffect(SoundEffects.Click);
}
}
Upvotes: 1
Reputation: 74174
Xamarin.Android
:var root = FindViewById<View>(Android.Resource.Id.Content);
root.PlaySoundEffect(SoundEffects.Click);
Android playSoundEffect(int soundConstant)
Xamarin.iOS
UIDevice.CurrentDevice.PlayInputClick();
Xamarin.Forms
via Dependency Service:public interface ISound
{
void KeyboardClick () ;
}
And then implement the platform specific function.
iOS:
public void KeyboardClick()
{
UIDevice.CurrentDevice.PlayInputClick();
}
Android:
public View root;
public void KeyboardClick()
{
if (root == null)
{
root = FindViewById<View>(Android.Resource.Id.Content);
}
root.PlaySoundEffect(SoundEffects.Click);
}
Upvotes: 14
Reputation: 27224
Take a look at the following:
MonoTouch.UIKit.IUIInputViewAudioFeedback
Interface that, together with the UIInputViewAudioFeedback_Extensions class, comprise the UIInputViewAudioFeedback protocol. See Also: IUIInputViewAudioFeedback
https://developer.xamarin.com/api/type/MonoTouch.UIKit.IUIInputViewAudioFeedback/
You'll want something like this (untested):
public void SomeButtonFunction()
{
SomeBtn.TouchUpInside += (s, e) => {
UIDevice.CurrentDevice.PlayInputClick();
};
}
Upvotes: 0