android – How to change default ClickSound in whole application from one place-ThrowExceptions

Exception or error:

Is it possible to change default ClickSound in whole android application without needing to implement MediaPlayer in onButtonClickListener/onButtonTouchListener for each button in the app ?

As the application has nearly 20 buttons, it seems to be irrational to implement onButtonClickListenr and play sound in every activity or fragment. It could be great if we can change the ClickSound in one place for the app.

Be glad to hear your opinions …

How to solve:

You can create an extension function to face this problem


fun View.setOnClickListenerWithSound(action: () -> Unit) {
    val actionWithSound = ActionWithSound(context, action)

    setOnClickListener {

private class ActionWithSound(context: Context,private val action: () -> Unit) {

    private var mediaPlayer: MediaPlayer = MediaPlayer.create(context, )

    fun notifyAction() {

and you can use it as bellow:

someBtn.setOnClickListenerWithSound {
        // do what you want

Leave a Reply

Your email address will not be published. Required fields are marked *