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.
Try casting the chars to ints, then test if they are greater than 24 and 60.
Minus 48 because numbers 0 is 48th in the ascii table. The test has to be ascii.
Instead of char why dont you use string, Because char can also be used for comparsion as it can return numbers
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
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
//THis handles the input for the date and time at the runtime and all the //invalid atttempt are automatically consumed
//this is an alternative to regex expression that you can implement
I found this library for time
EditText
. The code is easy to use. I add some explanations from the code owner:Here is the
TimeEditText
Class:You must add this lines to your view:
Try this, I simply edited the code you provided....
This works absolutely fine for me. Accepts time in format
hh:mm
only (no other character accepted)