srbyk1990
srbyk1990

Reputation: 411

Disable all edittext inside linear layout

I am trying to disable all field inside linear layout. There are many edittext and textview inside that linear layout. However, i am only trying to disable edittext. I was able to disable all children but i want able to disable on edit text. Is there any way to do that?

Upvotes: 3

Views: 2126

Answers (4)

Sethuraman Srinivasan
Sethuraman Srinivasan

Reputation: 1606

The accepted answer allows only to disable the EditText. The same solution cannot be used to enable it because all EditTexts are not touchables anymore.

The below solution is just an improvement on previously produced solutions.

       fun setAllEditTextEnabled(view: View, enable: Boolean){
            if (view is EditText) {
                view.setEnabled(enable)
            }
            else if (view is ViewGroup) {
                for (i in 0 until view.childCount) {
                    val innerView = view.getChildAt(i)
                    setAllEditTextEnabled(innerView, enable)
                }
            }
        }

Upvotes: 0

sokarcreative
sokarcreative

Reputation: 526

The recursivity is the easiest way to do that. More secure to catch them all.

private void setAllEditTextEnabled(@NonNull View view, boolean enable){
    if (view instanceof EditText) {
        view.setEnabled(enable);
    }
    else if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setAllEditTextEnabled(innerView, enable);
        }
    }
}

Upvotes: 1

petey
petey

Reputation: 17140

Where ll is your linearlayout:

    LinearLayout ll = (LinearLayout) findViewById(R.id.myLLid);
    for (View view : ll.getTouchables()){
        if (view instanceof EditText){
            EditText editText = (EditText) view;
            editText.setEnabled(false);
            editText.setFocusable(false);
            editText.setFocusableInTouchMode(false);
        }
    }

Upvotes: 9

Alex
Alex

Reputation: 9720

LinearLayout linearLayout = 
                    (LinearLayout) getView().findViewById(R.id.contentPanel);

        for (int i=0;i<linearLayout.getChildCount();i++){
            View view = ll.getChildAt(i);
            if (view instanceof EditText){
                EditText editText = (EditText) view;
                editText.setEnabled(false);
            }
        }

Upvotes: 0

Related Questions