Sebastian Mocanu
Sebastian Mocanu

Reputation: 45

ImageButton with clickable = "false" is still clickable

I use an ImageButton to make a phonecall, but it should not be clickable until I tap the showPhoneNumberBtn ImageButton.

ImageButton showPhoneNumberBtn = root.findViewById(R.id.showPhoneNumberBtn);
showPhoneNumberBtn.setOnClickListener(new View.OnClickListener() {
    @SuppressLint("ResourceAsColor")
    @Override
    public void onClick(View v) {
        TextView phoneNumber = root.findViewById(R.id.phoneNumberText);
        phoneNumber.setText(currentUser.getPhoneNumber());
        ImageButton makeCallBtn = root.findViewById(R.id.makeCallBtn);
        makeCallBtn.setClickable(true);
        Drawable img = getContext().getDrawable(R.drawable.ic_call_black_24dp);
        img.setTint(Color.parseColor("#3CB371"));
        makeCallBtn.setBackground(img);
    }
});

ImageButton makeCallBtn = root.findViewById(R.id.makeCallBtn);
makeCallBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE}, 1);
        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + phoneNumber));
            startActivity(callIntent);
        } else {
            Toast.makeText(getContext(), "You don't assign permission.", Toast.LENGTH_SHORT).show();
        }
    }
});

Upvotes: 0

Views: 470

Answers (1)

devgianlu
devgianlu

Reputation: 1580

Using setOnClickListener will reset the clickable state to true. You need to set it to false right after.

From the source code:

public void setOnClickListener(OnClickListener l) {
    if (!isClickable()) {
        setClickable(true);
    }
    getListenerInfo().mOnClickListener = l;
}

A detailed explanation can be found here.

Upvotes: 2

Related Questions