1

I have a layout in which there are spinners, editText and checkboxes. There two modes:

1- edit all views (edit mode)

2- view (non edit mode)

But I don't want to do it for each view . Is there any way to set editable true or false?

simonalexander2005
  • 4,338
  • 4
  • 48
  • 92
javad ff
  • 75
  • 5
  • 2
    Enabled (what you're calling editable) isn't an inherited feature down the hierarchy chain. Visibility is (you could hide them all by hiding the root), but not enabled. – Gabe Sechan Jul 04 '19 at 10:01
  • You can get all the child views of your parent and set enable/disable them acc. to your need. – Yyy Jul 04 '19 at 11:50

3 Answers3

0

in edit mode use this in every view

yourView.setEnabled(true);

in read mode use this

yourView.setEnabled(false);
jins joseph
  • 271
  • 1
  • 11
0

The simple way is to create a set of such views manually:

val editableViews: Set<View> = setOf(v1, v2, v3)

and use it:

editableViews.forEach { it.enabled = isEditMode }

If you have a complex layout you may add dynamic initialization:

private fun getAllViews(
    view: View,
    set: MutableSet<View>, 
    filter: (view: View) -> Boolean = {true}
){
    val viewGroup = view as? ViewGroup
    if (viewGroup != null) {
        for (i: Int in 0 until viewGroup.childCount) {
            val child = viewGroup.getChildAt(i)
            getAllViews(child, set)
        }
    } else {
        if (filter()) {
            set.add(view)
        }
    }
}

initialize it in onViewCreated or in onCreate

val views = mutableSetOf<View>()
getAllViews(root, views) {
    it is Spinner || it is EditText || it is Checkbox
}
editableViews = views

It collects all required views, so you may make them enabled or disabled. But you should note, that this variant is not so flexible and you should prefer just the first one. (In case any exception you have to exclude some)

Beloo
  • 9,723
  • 7
  • 40
  • 71
0

This method will enable/disable all widgets of your parent layout.

public void enableAllView(ViewGroup rootView, boolean state) {
    for (int i = 0; i < rootView.getChildCount(); i++) {
        View childAt = rootView.getChildAt(i);
        if (childAt instanceof ViewGroup ) {
            enableAllView((ViewGroup) childAt, state);
        } else {
            if (childAt instanceof EditText) {
                EditText child = (EditText) childAt;
                child.setEnabled(state);
                child.setFocusable(state);
            } else if (childAt instanceof Spinner) {
                Spinner child = (Spinner) childAt;
                child.setEnabled(state);
                child.setFocusable(state);
            } else if (childAt instanceof CheckBox) {
                CheckBox child = (CheckBox) childAt;
                child.setEnabled(state);
                child.setFocusable(state);
            }
        }
    }
}

call this method like this--

enableAllView(rootView, true); // in case of edit(enable)
enableAllView(rootView, false); // in case of view(disable)
//rootView is a view in which your spinners/editText/checkbox are availabe.
Prachi Singh
  • 564
  • 3
  • 8
  • what if my checkbox or spinner are not direct child of root View and are inside relative layout in my root View – javad ff Jul 04 '19 at 18:35
  • it will work fine in that case also..just pass your very first view in which all other child are available – Prachi Singh Jul 05 '19 at 05:39
  • so you suggest to walk through view tree each time if change of enable state is needed, what a performance leak – Beloo Jul 05 '19 at 09:48