SetClickable () doesn't work on button
I want to make a button unclickable using setClicable (), but it doesn't work. I use inflation because I need to. This is my code:
mContactList = (LinearLayout) findViewById(R.id.contactList);
LayoutInflater inflater = getLayoutInflater();
for (ListIterator<ContactModel> it = contactList.listIterator(); it.hasNext();){
ContactModel contact = it.next();
View view = inflater.inflate(R.layout.contact_unknown_list_row, null);
view.findViewById(R.id.inviteButton).setTag(contact.getEmail());
view.findViewById(R.id.inviteButton).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String address = (String) v.getTag();
sendInvatoin(address);
if(v.findViewById(R.id.inviteButton).isClickable())
v.findViewById(R.id.inviteButton).setClickable(false);
}
});
mContactList.addView(view);
}
source to share
Try to use.
button.setEnabled(false);
In your case, you would do something like this:
view.findViewById(R.id.inviteButton).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String address = (String) v.getTag();
sendInvitatoins(address);
Button b = (Button)v;
b.setEnabled(false);
}
});
source to share
This will work in case of Imageview as well as button.
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (imageview.isEnabled()){
//I have wrapped all code inside onClick() in this if condition
//Your onClick() code will only execute if the imageview is enabled
//Now we can use setEnabled() instead of setClickable() everywhere
}}
};
Internally, onCreate()
you can do setEnabled(false)
that is equivalent to setClickable (false).
We can use a tag setEnabled()
as a tag because its state remains unaffected when the click is invoked (as opposed to setClickable()
which state changes).
source to share
When using setOnClickListener unclickable views (= v.setClickable (false)) will become clickable as stated in the Docs.
... a callback called when this view is clicked. If this view is not clickable, it becomes interactive.
Better to use v.setEnabled (false) if you want to set OnClickListener on button or any other view ...
source to share