View click not recognized?


My button click does not get recognized. Actually, no clicks get recognized at all.

Here is my activity:

public class ShowRecieveActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.copy_button)
    Button button;

    //oncreate and all other methods....

    @Override
    public void onClick(View v) {

        Timber.d("Click detected");
        if(v.getId() == R.id.copy_button){
            Timber.d("Click detected as copy button");
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("Wallet address", wallet.getAddress());
            clipboard.setPrimaryClip(clip);
            Timber.d("Clip set as: "+clipboard.getPrimaryClip().toString());

            showToast("Address copied to clipboard");
        }
    }
}

Somehow clicks in this activity do not get recognized at all. BaseActivity only implements BottomNavigationView.OnNavigationItemSelectedListener and that's it.

Does this interfere somehow with this view.onclicklistener? It shouldn't, right? What could the problem be?

If you need more from my code, let me know please.

- - Source

Answers

answered 5 day ago SinaMN75 #1

I not sure about this but I think you're not using butterknife correctly.

if you just binding button for clicking, you don't need to use @BindView, use @Onclick(R.id.copy_button)

@OnClick(R.id.copy_button)
public void submit(View view) {
  // TODO submit data to server...
}

based on butterknife official website

answered 5 day ago B.M #2

You need to attach an ClickListener to you button, add this code after Butter.bind(this) inside onCreate method :

button.setOnClickListener(this);

Hope this helps

answered 5 day ago Nero #3

There are two ways you can do this -
The way which you've applied so far:

@BindView(R.id.copy_button)
Button button;

//onCreate method
button.setOnClickListener()... and so on...

However, this method doesn't justify the use of ButterKnife as you are using the default method of doing this but replacing the wiring process in the onCreate method.
It's only useful when you want to access elements easily i.e. textview, edittext and so on..

But to answer the question, you should be using the following method:

@OnClick(R.id.click_button) 
public void buttonClick(View view) { 
    Timber.d("Click detected");
    if(v.getId() == R.id.copy_button){
        Timber.d("Click detected as copy button");
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("Wallet address", wallet.getAddress());
        clipboard.setPrimaryClip(clip);
        Timber.d("Clip set as: "+clipboard.getPrimaryClip().toString());

        showToast("Address copied to clipboard");
    }
}

Via this method, you are creating a link button the button which should listen to the event when triggered.

answered 5 day ago Igor Maia Romcy #4

Using butter knife looks like this line

ButterKnife.bind(this);

that makes the real bind of all your views (make sure to put it on onCreate method before using views).

comments powered by Disqus