How to disable all controls in action in Android?

I want to disable all input controls (e.g. TextEdit, Spinners) with the click of a button.

For example: when a user enters a value in a text box and clicks the Submit button, I want to disable all input controls and hide the keyboard.

You can add an overlay view over the activity so that the user does not touch the screen, but this is not an option, since I want to disable all input components and hide input controls.

+3
source share
3 answers

Iterate the container layout view and process the views depending on which widget they are instances of. For example, if you want to hide everything Buttonand disable everything EditText:

for(int i=0; i < layout.getChildCount(); i++) {
    View v = layout.childAt(i);
    if (v instanceof Button) {
        v.setVisibility(View.GONE); //Or View.INVISIBLE to keep its bounds
    }else
    if (v instanceof EditText) {
        ((EditText)v).setEnabled(false);
    }
}

Of course, if you want to add other properties, for example, to make it non-clickable or whatever, you would just add them to the correspondent, if from the previous code.

Then hide the keyboard :

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

A cleaner way to do this (if you know the identifiers of the views) is to store them in int[]and in a loop, instead of getting the view from the children from the layout, but as far as the result is, they are almost the same.

+5
source

Try using editText.setEnabled(false);

setEnabled(false); .

 buttonOK.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
             editText.setEnabled(false);
             spinner.setEnabled(false);

                           .......

                       // here you can disable all InputControls.
        }
    });
0

Take the TextView case. Then do the following:

textView.setClickable(false);
textView.setFocusable(false);
textView.setFocusableInTouchMode(false);

This will disable TextView. Similarly for the rest on demand.

0
source

All Articles