Example usage for android.widget TextView setText

List of usage examples for android.widget TextView setText

Introduction

In this page you can find the example usage for android.widget TextView setText.

Prototype

@android.view.RemotableViewMethod
public final void setText(@StringRes int resid) 

Source Link

Document

Sets the text to be displayed using a string resource identifier.

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 {//from   ww  w  .  j a  va  2  s.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);

}