setting android button invisible but still having an onClick Listener attached

自古美人都是妖i 提交于 2019-12-03 03:16:00

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.

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

myButton.setText("    ");

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.

Simple answer is set alpha to 0 like this.

 <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:alpha="0"
                android:clickable="true"
                android:onClick="getAllImages"
                android:visibility="visible" />

It will be invisible and onclick will work.

Make sure that your button's width and height are not set to wrap_content because that would cause the button to be extremely small if the text is " ". If that doesn't work, you could also try replacing onClick() with onTouch():

button1.setOnTouchListener(new OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        // TODO Auto-generated method stub
        return false;
    }
});

Don't use a button and override your Activity's dispatchTouchEvent and handle it that way.

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

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     Button b = (Button) findViewById(R.id.button1);
     final CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
     b.setBackgroundColor(Color.TRANSPARENT);

     b.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            cb.setChecked(true);

to this code button is invisible but it worked ;))

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

In java code:

btn.setClickable(false);

In .xml layout:

android:clickable="false"
amit bansode

This works properly for me:

  • btn.setClickable(false) on GONE visibility.
  • btn.setClickable(true) on VISIBLE visibility.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!