Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable all input controls in an activity in android?

Tags:

android

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.

like image 315
optimusPrime Avatar asked Feb 21 '14 12:02

optimusPrime


People also ask

What are input controls in Android?

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.


2 Answers

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.

like image 64
Juan Cortés Avatar answered Sep 23 '22 18:09

Juan Cortés


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.

like image 38
Aparupa Ghoshal Avatar answered Sep 25 '22 18:09

Aparupa Ghoshal