How to Add validation on dynamically added fragment editttext
Closed this issue · 8 comments
I am working on an android app In which similar Fragments(each fragment containing some edit-texts) are added to activity pro-grammatically. I want to validate all fragment edit text data on activity button click event so that all data can be validated.How this can achieved?
I think you should add the validation (e.g. mAwesomeValidation.addValidation(...)
) right after you add the fragment programmatically.
@rohitbhoite if there is no further problem, I'll close this ticket in 24 hours. Thanks.
@thyrlian Please excuse me for late reply.
FragmentTemplate frag = new FragmentTemplate();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.homework_template, frag, "fragment_one");
fragmentTransaction.commit();
This is how I am adding fragment from activity.
Multiple similar fragments can be added by user.
I want to validate all fragment data on button present on activity.Please help me regarding this
@myeduapp no problem. I don't think there is any problem. You just need to addValidation
for all the input fields one by one after fragmentTransaction.commit();
. That's it.
mAwesomeValidation.addValidation((TextInputLayout) frag.getView().findViewById(R.id.til_homework_description),".+","Error");
I have above code after fragmentTransaction.commit();
I am getting attempt to invoke virtual method findviewbyid on a null object reference error.
So this is nothing to do with the library. It's because your frag.getView()
is just null
.
I suggest you to check your fragment's lifecycle calls, move the code to a place where the view is valid. Also as a reference, to use this library properly with fragment, you need to do as mentioned in README:
It works perfectly with Fragment, but please pay attention to Fragment's lifecycle. You should set the
validate()
inside Fragment'sonActivityCreated
instead ofonCreateView
or any other early stage.
Hope it helps and good luck.
Right now my validation code is inside activity if I move it to the fragment level How can I validate all fragment data on single button click (Button is inside activity) and multiple similar fragments can be added?Am I missing something?
Actually you don't have to differentiate between activity and fragment. This library doesn't care. As long as you pass the correct view (not null object), it will do its job. The only thing you need to pay attention is as I said, move the code to the correct lifecycle call place, so that the view object is not null.