Search code examples
android-studiotimeandroid-edittextlong-integer

How do i get an input of time in milliseconds?


private long START_TIME_IN_MILLIS;
private long TimeLeftInMillis = START_TIME_IN_MILLIS;

i have declared these variables, which is meant to store an input from an EditText, but in my OnCreateView i have this line

START_TIME_IN_MILLIS = edtInsertTime.getText();

but it gives me an error, how do I get the input of time in milliseconds and store it in START_TIME_IN_MILLIS?


Solution

  • You didn't post the error you received, and that would be helpful since it can be of hundred reasons..

    Just looking at this code, you have variable of type long, ( which is a numeric type ), and what you get from edtInsetTime.getText()? , a type "Editable" according to the documentation, and you can't store Editable object in a long variable.

    https://developer.android.com/reference/android/widget/EditText

    public Editable getText ()
    Returns Editable - The text displayed by the text view.
    

    To get a long value from it, you first need to get a String from this EditText, then convert it to a long format, and then assign it to the variable.

    Examplary code would be.

    START_TIME_IN_MILLIS = Long.valueOf(edtInsertTime.getText().toString());
    

    Breaking it down.

    String text = edtInsertTime.getText().toString() returns the String value of an edittext

    That value then can be converted to a long value by parsing the String to long, with methods like

    Long.parseLong(text)
    

    or

    Long.valueOf(text)
    

    Asking this question means you don't have basic understanding of how types works in java, so feel free to read about it https://www.baeldung.com/java-primitives