Android ListView child View setEnabled() and setClickable() do nothing
So, you may be using a custom adapter too. If you do, override these methods:
public boolean areAllItemsEnabled() {
return false;
}
public boolean isEnabled(int position) {
// return false if position == position you want to disable
}
Then, when you receive a click tell the adapter what was the last item clicked and return false on isEnabled
for that position. For instance, you can have a method like this in your adapter:
private int mLastClicked;
public void setLastClicked(int lastClicked){
mLastClicked = lastClicked;
}
setEnabled(),setClickable() not working
Take front layout's size as background layout's size and set front
layout's background color transparent so background layout is partially
visible.
And set onClickListener to front layout and in onClick method do nothing.
This answer is not exactly as you want but it is a good alternative.
Hope it helps
View.setFocusable() and View.setClickable() have contrary effect in custom ArrayAdapter
Actually here I found the explanation of it all. The thing is that if you set the view to be clickable, then it will consume the click and not propagate it to the container.
Thus convertView.setClickable(true)
actually disables the clicks.
Disabled item in spinner list
You can get the item from the array in your ListAdapter based on its position and call setEnabled(false)
in the public getView()
method.
Like this:
if (position==10) {
convertView.setEnabled(false);
}
else{
convertView.setEnabled(true);
}
You will probably need to override some other methods. Check those posts:
Android ListView child View setEnabled() and setClickable() do nothing
Android: How to disable list items on list creation
Disappearing divider in ListView when ArrayAdapter.isEnabled returns false
Return true in areAllItemsEnabled() and false in isEnabled for specific item. The disabled item wont be clickable but you will still be able to view the divider lines
Note: This doesn't work for Android 5
View.setEnabled(false) does not work quite right in android
Recursively setting all descendants is needed, otherwise RadioButtons in the layout will not get setEnabled because RadioButtons are grand-children not direct children, there is RadioGroup in between. I record my best solution so far here based on others' answer.
public static void setEnabledAll(View v, boolean enabled) {
v.setEnabled(enabled);
v.setFocusable(enabled);
if(v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++)
setEnabledAll(vg.getChildAt(i), enabled);
}
}
Subclass is not working at this time. If I have:
public MyView class View {
protected void setEnabled(boolean enabled, boolean setChildren){
setEnabled(enabled);
setFocusable(enabled);
if(setChildren && this isinstanceof ViewGroup){
for ( int i = 0 ; i < this.getChildCount() ; i++ )
//this line will have issue if it is not MyView
this.getChildAt(i).setEnabled(enabled, true);
}
}
}
unless View itself has this overloaded setEnabled like this:
public class View {
protected void setEnabled(boolean enabled, boolean setChildren){
setEnabled(enabled);
setFocusable(enabled);
if(setChildren && this isinstanceof ViewGroup){
for ( int i = 0 ; i < this.getChildCount() ; i++ )
this.getChildAt(i).setEnabled(enabled, true);
}
}
}
This solution will eliminate problems 1, 2, 3. Problem 4 can be solved by setting disabled style selector for RadioGroup - I am not sure why android has no default disabled style selector for RadioGroup. Maybe another point for Android enhancement. Android is very flexible in SDK design also means sometimes extra work needed.
How do I prevent clicking elements in a ListView row when in MutliChoiceMode in Android
Add the property android:descendantFocusability="blocksDescendants"
to the layout root of your list row. This will prevent the children from gaining focus, and will enable the user to click on the list row or any of its child views independently. Now you can disable (gray out) and re-enable any of the child views by using setEnabled()
like you do in your code. Or if you would just like to prevent the user from being able to interact with a view without graying it out, you can use setClickable()
instead.
Related Topics
How to Solve Render Problem Path.Op() Not Supported
Objectanimator Animate Linearlayout Width
Android Ndk Std::To_String Support
Running Google Map Application on Android Emulator
Android Studio - Processdebugresources Failed
A Correct Way to Convert Byte[] in Java to Unsigned Char* in C++, and Vice Versa
Expandablelistview with Viewpager Combination as Its Child
Android: How to Implement First Time Tutorial Like Go Launcher in My App
Which HTML Tags Are Supported by Android Textview
Eclipse Indexer Errors When Using Stl with Android Ndk
Couldn't Get Connection Factory Client - Fighting with Google Maps
How to Avoid Scientific Notation in Double
Display the App Icon If the Contact Is Associated with the Application in Phone Address Book
How to Integrate Opencv 4.0 into a Pure C++ Android Ndk Project
How to Reduce Latency in Mediacodec Video/Avc Decoding