codec123
codec123

Reputation: 19

How to take input for timer with 11 buttons

I am new to android development . I am trying to make a timer app . My UI has 11 buttons from 0-9 , a 00 button and a backspace button . The initial format of my textview will be like "00h 00m 00s" . When a user type input like "123" the textview should change from right to left like "00h 01m 23s" . I cant figure out how to do this. It should take input like google clock app GOOGLE CLOCK APP

GOOGLE CLOCK APP

PLS WRITE CLEAR CODE IN JAVA .

I tried using three textview for h , m and s . I used SpannableStringBuilder to reduce the size of h , m and s . i was expecting it to replace zeros in the text view but it is removing it and writing new number . in short i am messed up.

Upvotes: 0

Views: 95

Answers (2)

Reilas
Reilas

Reputation: 6266

'... When a user type input like "123" the textview should change from right to left like "00h 01m 23s" . I cant figure out how to do this. It should take input like google clock app GOOGLE CLOCK APP ...'

Utilize a class structure to parse the value into hours, minutes, and seconds.

import static java.lang.Integer.parseInt;
class Time {
    String string = "";
    int h, m, s;

    Time() { }
    Time(String s) { append(s); }

    void append(String s) {
        string += s;
        parse();
    }

    private void parse() {
        int n = string.length();
        if (n < 3) s = parseInt(string);
        else {
            if (n <= 4) m = parseInt(string.substring(0, n - 2));
            else {
                h = parseInt(string.substring(0, n - 4));
                m = parseInt(string.substring(n - 4, n - 2));
            }
            s = parseInt(string.substring(n - 2));
        }
    }

    @Override
    public String toString() {
        return "%02dh %02dm %02ds".formatted(h, m, s);
    }
}
String s = "12345";
System.out.println(new Time(s));

Output

01h 23m 45s

Here is an example of an appending value.

Time t = new Time();
for (int i = 1; i < 10; i++) {
    t.append(String.valueOf(i));
    System.out.println(t);
}

Output

00h 00m 01s
00h 00m 12s
00h 01m 23s
00h 12m 34s
01h 23m 45s
12h 34m 56s
123h 45m 67s
1234h 56m 78s
12345h 67m 89s

Upvotes: 0

C.F.G
C.F.G

Reputation: 1463

First Method:

Try using Strings features (I haven't tested):

String formattedSecond = second + Html.fromHtml("<sub><small>" + s + "</small></sub>")

secondTextView.setText(formattedSecond);

Second Method:

You can add an extra TextView aligned in Right (End) Bottom of each TextView of smaller size both inside a RelativeLayout.

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/aa"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="12"
            android:gravity="center"
            android:textSize="50sp"/>

        <TextView
            android:id="@+id/bb"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:text="S"
            android:gravity="center"
            android:textSize="20sp"
            android:layout_alignBottom="@id/aa"
            android:layout_alignEnd="@id/aa" />

</RelativeLayout>

Upvotes: 0

Related Questions