@@ -49,26 +49,26 @@ public ComprehensionTemplate() {
4949 metaData = new ArrayList <>();
5050 }
5151
52- private static boolean validated (Context context , EditText title , EditText passage , EditText timer ) {
52+ private static boolean validated ( Context context , EditText title , EditText passage , EditText timer ) {
5353 if (title == null || passage == null || timer == null ) {
5454 return false ;
5555 }
5656
57- String titleText = title .getText ().toString ();
58- String passageText = passage .getText ().toString ();
59- String timerText = timer .getText ().toString ();
57+ String titleText = title .getText ().toString (). trim () ;
58+ String passageText = passage .getText ().toString (). trim () ;
59+ String timerText = timer .getText ().toString (). trim () ;
6060
61- if (timerText . length () > 9 ) {
62- Toast . makeText (context , R .string .comprehension_template_timer_correct_hint , Toast . LENGTH_SHORT ). show ( );
61+ if ("" . equals ( titleText ) ) {
62+ title . setError (context . getString ( R .string .comprehension_template_title_hint ) );
6363 return false ;
64- } else if ("" .equals (titleText )) {
65- Toast . makeText (context , R .string .comprehension_template_title_hint , Toast . LENGTH_SHORT ). show ( );
64+ } else if ("" .equals (passageText )) {
65+ passage . setError (context . getString ( R .string .comprehension_template_passage_hint ) );
6666 return false ;
67- } else if (passageText . equals ( "" ) ) {
68- Toast . makeText (context , R .string .comprehension_template_passage_hint , Toast . LENGTH_SHORT ). show ( );
67+ }else if (timerText . length () > 9 ) {
68+ timer . setError (context . getString ( R .string .comprehension_template_timer_correct_hint ) );
6969 return false ;
70- } else if (timerText .equals ("" )) {
71- Toast . makeText (context , R .string .comprehension_template_timer_hint , Toast . LENGTH_SHORT ). show ( );
70+ } else if ("" .equals (timerText )) {
71+ timer . setError (context . getString ( R .string .comprehension_template_timer_hint ) );
7272 return false ;
7373 }
7474
@@ -137,8 +137,8 @@ private void checkButton(ArrayList<RadioButton> buttons, ArrayList<EditText> opt
137137 for (RadioButton button : buttons ) {
138138 if (button .getId () == id ) {
139139 int index = buttons .indexOf (button );
140- if (options .get (index ).getText ().toString ().equals ( "" )) {
141- Toast . makeText ( context , "Enter a valid option before marking it as answer" , Toast . LENGTH_LONG ). show ( );
140+ if ("" . equals ( options .get (index ).getText ().toString ().trim () )) {
141+ options . get ( index ). setError ( context . getString ( R . string . valid_before_setting_answer ) );
142142 button .setChecked (false );
143143 return ;
144144 } else {
@@ -200,42 +200,66 @@ public void onClick(View v) {
200200 dialog .getButton (DialogInterface .BUTTON_POSITIVE ).setOnClickListener (new View .OnClickListener () {
201201 @ Override
202202 public void onClick (View v ) {
203+
203204 boolean isValidated = true ;
204- int checkedAns = getCheckedAnswer ( buttons );
205- if (checkedAns < 0 ) {
206- Toast . makeText (activity , "Choose a correct option" , Toast . LENGTH_SHORT ). show ( );
205+
206+ if ("" . equals ( question . getText (). toString (). trim ()) ) {
207+ question . setError (activity . getString ( R . string . enter_question ) );
207208 isValidated = false ;
209+ return ;
208210 }
209- if (question .getText ().toString ().equals ("" )) {
210211
211- question .setError ("Question is required" );
212+ if (options .get (0 ).getText ().toString ().trim ().equals ("" )){
213+ options .get (0 ).setError (activity .getString (R .string .cannot_be_empty ));
212214 isValidated = false ;
215+ return ;
216+ }
217+ if (options .get (1 ).getText ().toString ().trim ().equals ("" )){
218+ options .get (1 ).setError (activity .getString (R .string .cannot_be_empty ));
219+ isValidated = false ;
220+ return ;
221+ }
222+ if (options .get (2 ).getText ().toString ().trim ().equals ("" ) && !options .get (3 ).getText ().toString ().trim ().equals ("" )){
223+ options .get (2 ).hasFocus ();
224+ options .get (2 ).setError (activity .getString (R .string .comprehension_select_option_3_first ));
225+ isValidated = false ;
226+ return ;
227+ }
228+
229+ int correctAnswer = 0 ;
230+ int checkedAns = getCheckedAnswer (buttons );
231+
232+ if (checkedAns < 0 ) {
233+ Toast .makeText (activity , R .string .comprehension_template_choose_correct_option , Toast .LENGTH_SHORT ).show ();
234+ isValidated = false ;
235+ return ;
213236 }
214237
215- int optionCount = 0 ;
216238 for (EditText option : options ) {
217- if (!option .getText ().toString ().equals ("" )) {
218- optionCount ++;
239+ if ("" .equals (option .getText ().toString ().trim ())){
240+ continue ;
241+ }
242+ if (option .getText ().toString ()!= null && "" .equals (option .getText ().toString ().trim ())) {
243+ option .getText ().clear ();
244+ option .setError (activity .getString (R .string .comprehension_template_valid_option ));
245+ isValidated = false ;
246+ return ;
219247 }
220- }
221- if (optionCount < 2 ) {
222- Toast .makeText (activity , "Minimum two multiple answers are required." , Toast .LENGTH_SHORT ).show ();
223- isValidated = false ;
224248 }
225249
226250 if (isValidated ) {
227251 dialog .dismiss ();
228252 ArrayList <String > answerOptions = new ArrayList <>();
229- int correctAnswer = 0 ;
253+ correctAnswer = 0 ;
230254 for (int i = 0 ; i < buttons .size (); i ++) {
231- if (buttons .get (i ).isChecked () && !options .get (i ).getText ().toString ().equals ( "" )) {
255+ if (buttons .get (i ).isChecked () && !"" . equals ( options .get (i ).getText ().toString ().trim () )) {
232256 correctAnswer = answerOptions .size ();
233- answerOptions .add (options .get (i ).getText ().toString ());
234- } else if (!options .get (i ).getText ().toString ().equals ( "" )) {
235- answerOptions .add (options .get (i ).getText ().toString ());
257+ answerOptions .add (options .get (i ).getText ().toString (). trim () );
258+ } else if (!"" . equals ( options .get (i ).getText ().toString ().trim () )) {
259+ answerOptions .add (options .get (i ).getText ().toString (). trim () );
236260 }
237261 }
238- String questionText = question .getText ().toString ();
262+ String questionText = question .getText ().toString (). trim () ;
239263 comprehensionData .add (new ComprehensionModel (questionText , answerOptions , correctAnswer ));
240264 setEmptyView (activity );
241265 adapter .notifyDataSetChanged ();
@@ -283,11 +307,11 @@ public void fileSelected(File file) {
283307 @ Override
284308 public void onClick (View v ) {
285309
286- if (validated (activity , title , passage , timer )) {
310+ if (validated (activity ,title , passage , timer )) {
287311
288- String titleText = title .getText ().toString ();
289- String passageText = passage .getText ().toString ();
290- long timerLong = Long .parseLong (timer .getText ().toString ());
312+ String titleText = title .getText ().toString (). trim () ;
313+ String passageText = passage .getText ().toString (). trim () ;
314+ long timerLong = Long .parseLong (timer .getText ().toString (). trim () );
291315 ComprehensionMetaModel temp = new ComprehensionMetaModel (titleText , passageText , timerLong );
292316 metaData .add (temp );
293317 setEmptyView (activity );
@@ -321,8 +345,8 @@ public void editItem(final Activity activity, final int position) {
321345 final EditText passage = (EditText ) dialogView .findViewById (R .id .meta_passage );
322346 final EditText timer = (EditText ) dialogView .findViewById (R .id .meta_timer );
323347 title .setText (data .getTitle ());
324- passage .setText (data .getPassage ());
325- timer .setText (String .valueOf (data .getTime ()));
348+ passage .setText (data .getPassage (). trim () );
349+ timer .setText (String .valueOf (data .getTime ()). trim () );
326350
327351 dialogView .findViewById (R .id .upload ).setOnClickListener (new View .OnClickListener () {
328352 @ Override
@@ -343,11 +367,11 @@ public void fileSelected(File file) {
343367 @ Override
344368 public void onClick (View v ) {
345369
346- if (validated (activity , title , passage , timer )) {
370+ if (validated (activity ,title , passage , timer )) {
347371
348- String titleText = title .getText ().toString ();
349- String passageText = passage .getText ().toString ();
350- long timerLong = Long .parseLong (timer .getText ().toString ());
372+ String titleText = title .getText ().toString (). trim () ;
373+ String passageText = passage .getText ().toString (). trim () ;
374+ long timerLong = Long .parseLong (timer .getText ().toString (). trim () );
351375
352376 data .setTitle (titleText );
353377 data .setPassage (passageText );
@@ -392,7 +416,7 @@ public void onClick(View v) {
392416 options .get (i ).setText (data .getOptions ().get (i ));
393417 }
394418
395- question .setText (data .getQuestion ());
419+ question .setText (data .getQuestion (). trim () );
396420 buttons .get (data .getCorrectAnswer ()).setChecked (true );
397421
398422 for (final RadioButton button : buttons ) {
@@ -409,45 +433,66 @@ public void onClick(View v) {
409433 public void onClick (View v ) {
410434
411435 boolean isValidated = true ;
412- int checkedAns = getCheckedAnswer ( buttons );
413- if (checkedAns < 0 ) {
414- Toast . makeText (activity , "Choose a correct option" , Toast . LENGTH_SHORT ). show ( );
436+
437+ if ("" . equals ( question . getText (). toString (). trim ()) ) {
438+ question . setError (activity . getString ( R . string . enter_question ) );
415439 isValidated = false ;
416440 }
417- if (question .getText ().toString ().equals ("" )) {
418441
419- question .setError ("Question is required" );
442+ if (options .get (0 ).getText ().toString ().trim ().equals ("" )){
443+ options .get (0 ).setError (activity .getString (R .string .cannot_be_empty ));
420444 isValidated = false ;
445+ return ;
446+ }
447+ if (options .get (1 ).getText ().toString ().trim ().equals ("" )){
448+ options .get (1 ).setError (activity .getString (R .string .cannot_be_empty ));
449+ isValidated = false ;
450+ return ;
451+ }
452+ if (options .get (2 ).getText ().toString ().trim ().equals ("" ) && !options .get (3 ).getText ().toString ().trim ().equals ("" )){
453+ options .get (2 ).hasFocus ();
454+ options .get (2 ).setError (activity .getString (R .string .comprehension_select_option_3_first ));
455+ isValidated = false ;
456+ return ;
457+ }
458+
459+ int correctAnswer = 0 ;
460+ int checkedAns = getCheckedAnswer (buttons );
461+
462+ if (checkedAns < 0 ) {
463+ Toast .makeText (activity , R .string .comprehension_template_choose_correct_option , Toast .LENGTH_SHORT ).show ();
464+ isValidated = false ;
465+ return ;
421466 }
422467
423- int optionCount = 0 ;
424468 for (EditText option : options ) {
425- if (!option .getText ().toString ().equals ("" )) {
426- optionCount ++;
469+ if ("" .equals (option .getText ().toString ().trim ())){
470+ continue ;
471+ }
472+ if (option .getText ().toString ()!= null && "" .equals (option .getText ().toString ().trim ())) {
473+ option .getText ().clear ();
474+ option .setError (activity .getString (R .string .comprehension_template_valid_option ));
475+ isValidated = false ;
476+ return ;
427477 }
428- }
429- if (optionCount < 2 ) {
430- Toast .makeText (activity , "Minimum two multiple answers are required." , Toast .LENGTH_SHORT ).show ();
431- isValidated = false ;
432478 }
433479
434480 if (isValidated ) {
435481 dialog .dismiss ();
436482 ArrayList <String > answerOptions = new ArrayList <>();
437- int correctAnswer = 0 ;
483+ correctAnswer = 0 ;
438484 for (int i = 0 ; i < buttons .size (); i ++) {
439- if (buttons .get (i ).isChecked () && !options .get (i ).getText ().toString ().equals ( "" )) {
485+ if (buttons .get (i ).isChecked () && !"" . equals ( options .get (i ).getText ().toString ().trim () )) {
440486 correctAnswer = answerOptions .size ();
441- answerOptions .add (options .get (i ).getText ().toString ());
442- } else if (!options .get (i ).getText ().toString ().equals ( "" )) {
443- answerOptions .add (options .get (i ).getText ().toString ());
487+ answerOptions .add (options .get (i ).getText ().toString (). trim () );
488+ } else if (!"" . equals ( options .get (i ).getText ().toString ().trim () )) {
489+ answerOptions .add (options .get (i ).getText ().toString (). trim () );
444490 }
445491 }
446- String questionText = question .getText ().toString ();
492+ String questionText = question .getText ().toString (). trim () ;
447493 comprehensionData .set (position , new ComprehensionModel (questionText , answerOptions , correctAnswer ));
448494 adapter .notifyDataSetChanged ();
449495 }
450-
451496 }
452497 });
453498 }
@@ -591,4 +636,4 @@ private String readFile(File file) {
591636 }
592637 return "" ;
593638 }
594- }
639+ }
0 commit comments