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
android android-viewpager pagerslidingtabstrip


source to share


No one has answered this question yet

Check out similar questions:

481
How do I disable swapping by swiping in the ViewPager but still be able to scroll programmatically?
129
How do I change the ViewPager page?
127
Determine when the ViewPager is changing pages
6
How to attach a PDF of assets via email?
2
setText on a button from another kind of android activity
2
Can anyone show me a simple working implementation of PagerSlidingTabStrip?
1
Animation issues in ViewPager
1
Weird OutOfMemoryError while loading view pager
0
Saving the state of a ToggleButton to a ListView using SharedPreferences
0
How does the viewPager preserve the states of the fragments when the orientation changes?



All Articles
Loading...
X
Show
Funny
Dev
Pics