Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict to input time for edittext in android

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 &= (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 : "";
        }
    };

Edited Question : main.xml file code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txtRecipientName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="20dp"
            android:text="@string/recipient_name" />

        <EditText
            android:id="@+id/edTxtRecipient"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:paddingLeft="20dp" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txtParcelDeliverTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="20dp"
            android:text="@string/delivered_time" />

        <EditText
            android:id="@+id/edTxtParcelDeliverTime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:paddingLeft="20dp" >
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnRecipient_OK"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@android:string/ok" />
    </LinearLayout>

</LinearLayout>

This code is working but if i insert first alphabet and insert proper value then its not working because source contains its previous character value.

like image 472
sam_k Avatar asked Oct 29 '12 11:10

sam_k


2 Answers

Try casting the chars to ints, then test if they are greater than 24 and 60.

int a = ((int) result.charAt(0)) - 48;
int b = ((int) result.charAt(1)) - 48;
int c = ((int) result.charAt(3)) - 48;
if(a < 0 || b < 0 || c < 0) {
    Not right.
}

if((a > 2 || (a == 2 && b > 3)) || c > 59) {
    Neither is this.
}

Minus 48 because numbers 0 is 48th in the ascii table. The test has to be ascii.

like image 171
Adude11 Avatar answered Oct 14 '22 08:10

Adude11


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 });

}
}
like image 23
Girish Nair Avatar answered Oct 14 '22 09:10

Girish Nair