Hi, does anybody know how add click sound for button in Android? here is my code below..
i have checked some tutorials, but im not sure how to implement them on my code

package com.example.quizgame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.media.MediaPlayer;

public class MainActivity extends Activity implements OnClickListener {


    MediaPlayer clicksound;


    public void onClick(View a){
        switch (a.getId()){
        case R.id.login:
            Intent b = new Intent (this, play.class);
            startActivity(b);
            break;
        case R.id.admin:
            Intent c = new Intent (this, admin.class);
            startActivity(c);
            break;
        case R.id.exit:
            finish();
            break;

        }


    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        View playButton = findViewById(R.id.login);
        playButton.setOnClickListener(this);
        View adminButton = findViewById(R.id.admin);
        adminButton.setOnClickListener(this);
        View exitButton = findViewById(R.id.exit);
        exitButton.setOnClickListener(this);

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

Recommended Answers

All 2 Replies

This is how simple it can be done

Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);
one.setOnClickListener(new OnClickListener(){

    public void onClick(View v) {
        mp.start();
    }
});
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.