Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting android button invisible but still having an onClick Listener attached

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();
    }
});
like image 715
cj1098 Avatar asked Jul 27 '11 21:07

cj1098


1 Answers

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.

like image 106
Steve Prentice Avatar answered Oct 23 '22 21:10

Steve Prentice