How to make all EditText uneditable (and vicer-versa)?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to make all EditText uneditable (and vicer-versa)?



I have an app where there are several text fields. I want to be able to toggle between editing them and not editing them. How can this be done without hard-coding the id for each and everyone one of the EditText?


EditText



For example, I'd like an alternative to


EditText Et = findViewById((R.id.one));
Et.setFocusable(false);
Et.setClickable(false);
Et = findViewById((R.id.two));
Et.setFocusable(false);
Et.setClickable(false);
//...continue the pattern
Et = findViewById((R.id.ten));





any feedback after the answers below?
– Lino
Aug 13 at 11:07





@Lino I haven't had the chance to implement the suggestions yet but if I haven't gotten back in a few days feel free to remind me :)
– northerner
Aug 14 at 10:26




2 Answers
2



Put them in an array:


EditText etArray = findViewById(R.id.one), findViewById(R.id.two), findViewById(R.id.three) ;

for (EditText et : etArray)
et.setFocusable(false);
et.setClickable(false);



or if the names are like EditText1, EditText2,.. for 10 views:


for (int i = 1; i <= 10; i++)
int id = getResources().getIdentifier("EditText" + i, "id", getPackageName());
EditText et = findViewById(id);
et.setFocusable(false);
et.setClickable(false);



or even better, create an ArrayList in onCreate() and use the list:

this declaration goes global in your activity


onCreate()


ArrayList<EditText> etArrayList;



this is for onCreate():


etArrayList = new ArrayList<EditText>();
for (int i = 1; i <= 10; i++)
int id = getResources().getIdentifier("EditText" + i, "id", getPackageName());
EditText et = findViewById(id);
etArrayList.add(et);



and use this anywhere in your activity class:


for (EditText et : etArrayList)
et.setFocusable(false);
et.setClickable(false);





Is this the best? No way to iterate over them without knowing the name?
– northerner
Aug 12 at 10:05





Yes there is but you will have to change their names to something like EditText1, EditText2,... I will edit my answer
– user8959091
Aug 12 at 10:09





see my edited answer, I hope I have no typos
– user8959091
Aug 12 at 10:16



Yes, it is possible to modify your EditTexts without hardcoding stuff. Suppose they're included in a LinearLayout named main_content, then you can loop through each of them in the following way:


LinearLayout


main_content


LinearLayout layout = findViewById(R.id.main_content);
for (int i = 0; i < layout.getChildCount(); i++)
View widget = layout.getChildAt(i);
if (widget instanceof EditText)
widget.setFocusable(false);
widget.setClickable(false);






Sorry for the long delay. The if statment is never true and I think it's because the EditText is inside a TableRow
– northerner
2 days ago



if


EditText


TableRow





so use TableRow layout = findViewById(R.id.tableRowID); TableRow is a LinearLayout therefore you can loop through child views.
– Lino
2 days ago



TableRow layout = findViewById(R.id.tableRowID);





unfortunately the EditText are in multiple TableRows
– northerner
2 days ago


EditText


TableRow





I still believe that the loop can be made considering the parent view that contains all the TableRow
– Lino
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered