ProblemOverflow97
ProblemOverflow97

Reputation: 11

Clean way to implement an onClickListener in android apps

In my games/apps I find I want button presses to make clicking or pressing sounds when the user interacts with them. My current working solution is quite ugly and I was wondering if there's a more streamlined way to implement this?

findViewById(R.id.my_button).setOnClickListener(new BigClickListener() {
            @Override
            public void onClick(View view) {
                super.onClick(view);
                //code goes in here
                }
            }
        });

BigClickListener code:

public class BigClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        SoundPool soundPool = new SoundPool.Builder().setMaxStreams(2).setAudioAttributes(audioAttrs).build();

        click1 = soundPool.load(c, R.raw.click_1, 1);
        soundPool.play(click1, someVolume, someVolume, 0, 0, 1);
    }
}

The current setup means by the time I start coding my button press I'm already 4 or 5 indents into the page and it always looks quite messy. Any tips anyone knows for overwriting button/ImageButton behaviour app-wide please let me know :)

Thanks

Upvotes: 0

Views: 16

Answers (0)

Related Questions