In Viewpager, how to validate the form on the page and if any blank field stays on the same page?

I used ViewPager using PagerSlidingTabStrip Library and Forms on every page. I am trying to validate forms on a method SimpleOnPageChangeListener.onPageSelected

and if any field is empty go back to the same page again. But my following code works correctly when scrolling through the page, but if I click on the tabs, it doesn't go back to the previous page. In Viewpager, how to validate the form on the page and if any blank field stays on the same page?

private int mLastPage;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLastPage = 0;

        newInspectionPagerTabStrip = (PagerSlidingTabStrip) findViewById(R.id.new_inspection_pager_tab_strip);
        newInspectionPagerTabStrip.setViewPager(mViewpager);

        newInspectionPagerTabStrip.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                NewInspectionPagerAdapter adapter = (NewInspectionPagerAdapter)mViewpager.getAdapter();

                // If reverse page navigation(page 1 to page 0) dont validate forms and return   
                if(mLastPage > position){
                    return;
                }

                // If move from Page-0 to Page-1
                if(mLastPage==0){
                    NewInspectionFragment1 newInspectionFragment1 = (NewInspectionFragment1) adapter.getRegisteredFragment(0);
                    View newInspectionForm1GridLayout = newInspectionFragment1.getView();

                    EditText timeInEditText = (EditText) newInspectionForm1GridLayout.findViewById(R.id.new_inspection_form1_et_value_timein);
                    String timeIn = timeInEditText.getText().toString();

                    FORM_1_TIMEIN = timeIn;

                    EditText ageEditText = (EditText) newInspectionForm1GridLayout.findViewById(R.id.new_inspection_form1_et_value_age);
                    int age = ageEditText.getText().toString();

                    FORM_1_AGE = age;

                    // If any field is empty, again return to Page-0 and set mLastPage as 0
                    if(FORM_1_TIMEIN.isEmpty() || FORM_1_AGE==0)
                    {
                        mViewpager.setCurrentItem(0);

                        if(FORM_1_AGE==0)
                        {
                            ageEditText.setError(getString(R.string.error_incorrect_age));
                            ageEditText.requestFocus();
                        }

                        if(FORM_1_TIMEIN.isEmpty())
                        {
                            timeInEditText.setError(getString(R.string.error_incorrect_timein));
                            timeInEditText.requestFocus();
                        }

                        mLastPage = 0;
                        return;
                    }
                }
                else if(mLastPage==1){
                    NewInspectionFragment2 newInspectionFragment2 = (NewInspectionFragment2) adapter.getRegisteredFragment(1);
                    View newInspectionForm2GridLayout = newInspectionFragment2.getView();

                    EditText totalEditText = (EditText) newInspectionForm2GridLayout.findViewById(R.id.new_inspection_form2_et_value_total);
                    int total=totalMortalityEditText.getText().toString();

                    FORM_2_TOTAL = total;

                    EditText percentageEditText = (EditText) newInspectionForm2GridLayout.findViewById(R.id.new_inspection_form2_et_value_percentage);
                    int percentage=percentageEditText.getText().toString();

                    FORM_2_PERCENTAGE = percentage;

                    if(FORM_2_TOTAL==0 || FORM_2_PERCENTAGE==0)
                    {
                        mViewpager.setCurrentItem(1);

                        if(FORM_2_PERCENTAGE==0)
                        {
                            percentageEditText.setError(getString(R.string.error_incorrect_percentage));
                            percentageEditText.requestFocus();
                        }

                        if(FORM_2_TOTAL_MORTALITY==0)
                        {
                            totalEditText.setError(getString(R.string.error_incorrect_total));
                            totalEditText.requestFocus();
                        }

                        mLastPage = 1;
                        return;
                    }
                }
                else if(mLastPage==2){
                   // Do Same as above // Removed for code clarity
                }
                mLastPage = position;
            }
        });
}

      

+3


source to share





All Articles