I have to allow user to input only time in ##:## format in edit text on the fly, is there any way to achieve it? I have used below code but it doest not working.
I a
Instead of char why dont you use string, Because char can also be used for comparsion as it can return numbers
char c ='a';
if(c>10)
//do something
//OR
int x = c;
So why dont you use String instead of char
or what you can do is, take 1st two chars using substring or something like that and use Integer.parse() method to parse it, if it successfully parsed then its a valid number else it is not so you can validate it and similarly do it for next two chars
EDIT
If you wanted to implement like this 23:59 correct 24:05 incorrect 02:56 correct 02:79 incorrect
Then here is the code that worked from my side
public class MainActivity extends Activity {
InputFilter timeFilter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timeFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
int dstart, int dend) {
if (source.length() == 0) {
return null;// deleting, keep original editing
}
String result = "";
result += dest.toString().substring(0, dstart);
result += source.toString().substring(start, end);
result += dest.toString().substring(dend, dest.length());
if (result.length() > 5) {
return "";// do not allow this edit
}
boolean allowEdit = true;
char c;
if (result.length() > 0) {
c = result.charAt(0);
allowEdit &= (c >= '0' && c <= '2');
}
if (result.length() > 1) {
c = result.charAt(1);
if(result.charAt(0) == '0' || result.charAt(0) == '1')
allowEdit &= (c >= '0' && c <= '9');
else
allowEdit &= (c >= '0' && c <= '3');
}
if (result.length() > 2) {
c = result.charAt(2);
allowEdit &= (c == ':');
}
if (result.length() > 3) {
c = result.charAt(3);
allowEdit &= (c >= '0' && c <= '5');
}
if (result.length() > 4) {
c = result.charAt(4);
allowEdit &= (c >= '0' && c <= '9');
}
return allowEdit ? null : "";
}
};
EditText txt1 = (EditText) findViewById(R.id.edTxtParcelDeliverTime);
txt1.setFilters(new InputFilter[]{timeFilter});
}
}
I have just taken your XML and placed as my mains layout AND there are no changes to XML Now try this and tell ?
EDIT 2 Now here i have added a validtion for firs char check using doneOnce boolean value This works now, tell me if you have any other problem from this code now
public class MainActivity extends Activity {
EditText edt1;
InputFilter timeFilter;
private String LOG_TAG = "MainActivity";
private boolean doneOnce = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timeFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
int dstart, int dend) {
if(source.length() > 1 && doneOnce == false){
source = source.subSequence(source.length()-1, source.length());
if(source.charAt(0) >= '0' && source.charAt(0) <= '2'){
doneOnce = true;
return source;
}else{
return "";
}
}
if (source.length() == 0) {
return null;// deleting, keep original editing
}
String result = "";
result += dest.toString().substring(0, dstart);
result += source.toString().substring(start, end);
result += dest.toString().substring(dend, dest.length());
if (result.length() > 5) {
return "";// do not allow this edit
}
boolean allowEdit = true;
char c;
if (result.length() > 0) {
c = result.charAt(0);
allowEdit &= (c >= '0' && c <= '2');
}
if (result.length() > 1) {
c = result.charAt(1);
if(result.charAt(0) == '0' || result.charAt(0) == '1')
allowEdit &= (c >= '0' && c <= '9');
else
allowEdit &= (c >= '0' && c <= '3');
}
if (result.length() > 2) {
c = result.charAt(2);
allowEdit &= (c == ':');
}
if (result.length() > 3) {
c = result.charAt(3);
allowEdit &= (c >= '0' && c <= '5');
}
if (result.length() > 4) {
c = result.charAt(4);
allowEdit &= (c >= '0' && c <= '9');
}
return allowEdit ? null : "";
}
};
edt1 = (EditText) findViewById(R.id.edTxtParcelDeliverTime);
edt1.setFilters(new InputFilter[] { timeFilter });
}
}