Reputation: 2146
I have a form in an Android application used for setting up matches for a particular sport. I am able to successfully validate the inputs to exclude stuff (using TextWatcher), but not to enforce minimum input.
Although there is a means within XML to have minimum input in a certain field, I would prefer to do this programatically. Moreover, someone could still just ignore the fields altogether and just press the button to start the match (will result in application crashing).
If anyone has any suggestions I would really appreciate it - there doesn't seem to be a great deal of information about this available online (or I've been looking the wrong way).
I've excluded all but two fields in the form (teamA name and matchlength) for the sake of simplicity (Strings and ints are the only input "types").
public class MatchConfig extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_config);
// Show the Up button in the action bar.
setupActionBar();
final Context context = getApplicationContext();
final EditText teamA = (EditText) findViewById(R.id.teamA_editText); //Team A input
teamA.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{}
@Override
public void beforeTextChanged(CharSequence s,int start,int count,int after)
{}
@Override
public void afterTextChanged(Editable s) //Team A validation
{
String filtered_str = s.toString();
if (filtered_str.matches(".*[^A-Za-z^0-9].*")) { //if not alphanumeric
filtered_str = filtered_str.replaceAll("[^A-Za-z^0-9]", "");
s.clear();
s.append(filtered_str);
// s.insert(0, filtered_str);
Toast.makeText(context, //warning Toast
"Only letters and numbers are allowed!",
Toast.LENGTH_SHORT).show();
}
}
});
final EditText matchlength = (EditText) findViewById(R.id.stones_editText); //matchlength input
matchlength.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{}
@Override
public void beforeTextChanged(CharSequence s,int start,int count,int after)
{}
@Override
public void afterTextChanged(Editable s) //matchlength validation
{
int no=Integer.parseInt(s.toString());
if(no>999)
{
s.replace(0,s.length(), "999");
Toast.makeText(context,
"999 minutes is max length!",
Toast.LENGTH_SHORT).show();
}
}
});
final EditText location = (EditText) findViewById(R.id.location_editText);
Button start = (Button) findViewById(R.id.start_button);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent theIntent = new Intent(MatchConfig.this, MatchTimer.class);
theIntent.putExtra("teamAvar", teamA.getText().toString());
theIntent.putExtra("matchlengthVar", Integer.parseInt(matchlength.getText().toString()));
startActivity(theIntent);
//this finish() will close the MatchConfig Activity when start button will be pressed
finish();
}
});
Upvotes: 1
Views: 204
Reputation: 1139
Check the lengths when you click the start button:
if(teamA.getText().length() < 5){ //5 char min
//Show error
}
else{
//Do match
}
Upvotes: 2