Alex Rivera | Logout

How to restrict to input time for edittext in android

Asked 2012-10-29T11:42:32.940
9

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 able to enter number more than 24 value like 45623:5689.

edit.setInputType(InputType.TYPE_DATETIME_VARIATION_TIME)

Even android:text="time" is also not working.

how can i achieve this thing. Can anybody suggest me how can i do this thing.

I want to allow user to enter in first 2 places up to 23 value and then compulasary : and then user can allow up to 59 value.

for example

23:59 correct
24:05 incorrect
02:56 correct
02:79 incorrect

I used this customize filter also but its not working

I got this code from some where else in SO.

Code:

    InputFilter timeFilter = new InputFilter() {
        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);
                allowEdit &= (c >= '0' && c <= '9');
            }
            if (result.length() > 2) {
                c = result.charAt(2);
                allowEdit &
Edit
Report

1 Answer

1

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')
                    al
answered 2012-10-29T12:34:58.127

Your Answer