I just found out View.setEnabled(false) does not work quite right in android, in the following ways:
It seems like a bug. But how do I get a real DISABLE feature?
EDIT: forget to mention the first.
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.
From memory, please excuse typos:
class ExtendedLinearLayout extends LinearLayout{
protected void setEnabled(boolean enabled, boolean setChildren){
super.setEnabled(enabled);
if(setChildren){
for ( int idx = 0 ; idx < this.getChildCount() ; idx++ ) {
(View)(this.getChildAt(idx)).setEnabled(enabled);
}
}
}
}
Just want to update this Michael SM' solution with kotlin. Override for view will looks like:
override fun setEnabled(enabled: Boolean) {
super.setEnabled(enabled)
children.forEach { it.isEnabled = enabled }
}
Call now is:
view.isEnabled = false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With