3

Descrition :

I have an Fragment Activity FirstRun which consists of 5 fragments with swipe navigation, 2nd and 3rd fragments contain 2 EditTexts which are required to be filled by user, and they need not to be null, or unfilled.

Okay i can validate the fields with an OnTextChanged Listener, but there is chances that user may just swipe over to next fragment.

Question :

My question is how am I able to prevent swipe to next fragment until some value is entered in the EditText by the user ? Simply how to prevent the fragment create until the user input are validated ?

Example : prevent a swipe to 3rd fragment unless the EditText of 2nd fragment are filled.

Community
  • 1
  • 1
Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51

3 Answers3

6

Okay so after vaguely wandering for about a week, I found a workaround solution to validate the inputs, meanwhile also preventing swipes.

Step 1: First thing is to implement a Custom View Pager class as

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Toast;

public class FirstRunPager extends ViewPager {

private boolean isPagingEnabled;
public Context context;
public FirstRunPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.isPagingEnabled = true;
    this.context = context;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (isPagingEnabled) {
        return super.onTouchEvent(event);
    }
    Toast.makeText(context, "Please fill in the details, then swipe !",
            Toast.LENGTH_LONG).show();
    return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (isPagingEnabled) {
        return super.onInterceptTouchEvent(event);
    }

    return false;
}

public void setPagingEnabled(boolean b) {
    isPagingEnabled = b;
}
}

Step 2: So now i can prevent swipes just by setting is paging enabled false, meanwhile any touch events are responded by a Toast, which prompts user to fill all fields.

Step 3: After a fragment is visible

      @Override
       public void setUserVisibleHint(boolean isVisibleToUser) {
          if (isVisibleToUser) {
              // set isPagingEnabled false here
              // validate EditText values here using Text Watcher
              // if all okay 
              // set isPagingEnabled = true
              // tell the user through a Toast, that he can swipe now.
               }
          }

That's how I'm implementing validation right now. I will update it if I improve my code.

Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
0

You could not let them swipe until the TextField is filled with a boolean.

In the swipe handler just add:

if(filled1 && filled2) 

Have those by default set to false and then set true when data is added.

You could also have it so that every time data is added to the TextFields, a String is set to the data in the TextField and disallow swipes unless both strings have a length longer than 0.

tom redfern
  • 30,562
  • 14
  • 91
  • 126
user2532233
  • 831
  • 2
  • 9
  • 13
0

Can you post some code? From what I'm understanding, you should check for the existence of the edit text (whether it be it exists or has a length greater than 0) from your 2nd fragment using an IF statement within your swipe navigation class.

tsoup
  • 58
  • 7