setting android button invisible but still having an onClick Listener attached

后端 未结 10 1580
情深已故
情深已故 2021-02-06 06:39

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:

相关标签:
10条回答
  • 2021-02-06 06:55

    try making the text in the button " "...

    myButton.setText("    ");
    
    0 讨论(0)
  • 2021-02-06 06:55

    You can also disable the button (It will not be clickable).

    In java code:

    btn.setClickable(false);
    

    In .xml layout:

    android:clickable="false"
    
    0 讨论(0)
  • 2021-02-06 06:57

    You can add an OnClickListener to any View, so try creating an ImageView with a transparent image and attach your listener to that.

    0 讨论(0)
  • 2021-02-06 07:04

    You can create any view, such as LinearLayout, as clickable. Make a LinearLayout with the same dimensions as the button and set it's onClick listener to whatever handles the event. Since it inherently isn't visible, it should hold the same effect.

    0 讨论(0)
  • 2021-02-06 07:06

    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.

    0 讨论(0)
  • 2021-02-06 07:10

    This works properly for me:

    • btn.setClickable(false) on GONE visibility.
    • btn.setClickable(true) on VISIBLE visibility.
    0 讨论(0)
提交回复
热议问题