Android – setting android button invisible but still having an onClick Listener attached

androidandroid-buttoninvisible

So currently I'm putting an Easter egg inside my app and I want the Button to be invisible, but when clicked(Rick roll). So far I can make it work when I say:

Button.setVisibility(view.VISIBLE);
Button.setBackgroundColor(Color.TRANSPARENT);

and then my onClickListener. The only problem with this is that I have to have text on the Button for it to be clickable. When I take the text out and make it completely invisible then the onClickListener is never called for some reason?

Here is my OnClickListener

wonderWhatThisDoes.setOnClickListener(new Button.OnClickListener()
{
    @Override
    public void onClick(View v) {
        mMediaPlayer = MediaPlayer.create(About.this, R.raw.surprise);
        mMediaPlayer.start();
        Context context = getApplicationContext();
        CharSequence text = "Congrats on finding our easter egg! Enjoy... :]";
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
        toast.show();
    }
});

Best Answer

In your layout, make your button have a specific width, like android:layout_width="40dp".

If your width is set to wrap_content with a transparent background and no text, Android will measure that view as having a width of 0dp. You'll never be able to click on that.