Example usage for android.text InputType TYPE_DATETIME_VARIATION_TIME

List of usage examples for android.text InputType TYPE_DATETIME_VARIATION_TIME

Introduction

In this page you can find the example usage for android.text InputType TYPE_DATETIME_VARIATION_TIME.

Prototype

int TYPE_DATETIME_VARIATION_TIME

To view the source code for android.text InputType TYPE_DATETIME_VARIATION_TIME.

Click Source Link

Document

Default variation of #TYPE_CLASS_DATETIME : allows entering only a time.

Usage

From source file:Main.java

public static void autoCompleteTime(CharSequence text, EditText time, TextView timeHint) {
    String stringText = text.toString();

    String textToBeSet = "";
    int inputType = InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_TIME;
    time.setError(null); //remove any error set from previously
    boolean error = false;

    if (stringText.matches("^\\d$")) {
        if (stringText.equals("1") || stringText.equals("0"))
            textToBeSet = "0:00AM";
        else {//ww w .  j a  va 2s.c o  m
            textToBeSet = ":00AM";
        }
    } else if (stringText.matches("^\\d\\d$")) {
        int intText = Integer.parseInt(stringText);
        if (intText >= 0 && intText <= 12)
            textToBeSet = ":00AM";
        else
            error = true;
    } else if (stringText.matches("^\\d+:$"))
        textToBeSet = "00AM";
    else if (stringText.matches("^\\d+:\\d$"))
        textToBeSet = "0AM";
    else if (stringText.matches("^\\d+:\\d\\d$")) {
        int intText = Integer.parseInt(stringText.replaceAll("^\\d+:", "")); //get minutes
        if (intText > 0 && intText < 60) {
            inputType = InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
            textToBeSet = "AM";
        } else
            error = true;
    } else if (stringText.matches("^\\d+:\\d\\d(A|P)$")) {
        inputType = InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
        textToBeSet = "M";
    } else if (stringText.equals(""))
        textToBeSet = "";
    else if (stringText.matches("^\\d+:\\d+(A|P)M")) {
        //To-do - take control to next input field
    } else {//error condition
        error = true;
    }

    if (error) {
        textToBeSet = "";
        time.setError("Incorrect time format");
    }

    time.setInputType(inputType);
    timeHint.setText(textToBeSet);

}