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.
Input controls are the interactive components in your app's user interface. Android provides a wide variety of controls you can use in your UI, such as buttons, text fields, seek bars, check box, zoom buttons, toggle buttons, and many more.
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.
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.
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