5

I want to Disable all input controls (eg: TextEdit, Spinners) on click of a button.

Eg: When user enters a value in text field and clicks on Submit button, I would want to disable all input controls and hide keyboard.

An overlay view on top of activity can be added to prevent user from touching screen, but this is not an option since I want to disable all input components and hide input controls.

optimusPrime
  • 249
  • 6
  • 17

3 Answers3

6

Iterate the container layout view and treat the views depending what widget they're instances of. For example if you wanted to hide all Button and disable all 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 wan to add other properties such as making it not clickable or whatever you'd just add them in the correspondent if from the previous code.

Then to hide the keyboard:

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

A cleaner way of doing this (if you know the ids of the views) is to store them in int[] and the loop over that instead of getting al children views from the layout, but as far as the result goes, they're pretty much the same.

Community
  • 1
  • 1
Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
1

Let us take the case of TextView. Then do as follows :

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

This would disable the TextView. Similarly for the others as per requirement.

Aparupa Ghoshal
  • 309
  • 2
  • 10
0

Try using editText.setEnabled(false);

Just set setEnabled(false); for all controls inside button click.

 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.
        }
    });
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35