Learn to display current time in English Words on Android
In that tutorial, you are going to discover how to display the current time in English words on Android.
As a developer, you know probably how to get the current time via the standard SDK but do you know how to display this time in English words to let the users to read directly the time on the screen ?
Note that you can discover this tutorial directly in video on YouTube :
To display the current time in English Words, we will need to make our own algorithm. But first, we are going to create the user interface (UI) of our Android application.
The UI will be quite simple with a simple TextView centered on the screen :
Algorithm to convert time to English Words
Now, we are going to create an algorithm to convert the current time in English Words. In our algorithm, we will need an array to store words used to write the time in English.
Then, we must get the current time. From this time, we extract the hour and the minutes values. In the algorithm, we start by checking if we need to print the word minute or minutes. We must print minute if the value of minutes is one or fifty nine.
When we have minutes from thirty one to fifty nine, we print the hour ahead of the given hour like six fifty five will be five minutes to seven and not five minutes to six when we print the hour ahead of the give hour, we face a problem at hour twelve because if we print an hour ahead of twelve, it will be thirteen.
But, we want one so the code will check this and decides what hour to print in words when minutes are from thirty one to fifty nine. If hour is twelve, we store one. Otherwise, we store words an ahead of given hour.
For the rest of the algorithm, we process like that :
- For minutes, we check if minutes are equal to zero then we add the O’Clock value.
- Then, if minutes are equal to fifteen, we add Quarter Past before the words.
- If minutes are equal to thirty , we add Half past before the words.
- If minutes are equal to forty five, we add Quarter to just before.
- If minutes are lower than thirty, so for minutes between one and twenty nine, we create a specific display.
- And finally in the else part, we create the String to return for the minutes between thirty one and fifty nine.
That gives us the following code for our algorithm letting you to get current time in English Words :
Using the Algorithm in the Application
Now, we can use this algorithm in our Android Application. To refresh the current time continually, we will use a Timer object and we will schedule a TimerTask at fixed rate.
In this TimerTask object, we will get the current time in English Words and we will just have to update the text in the TextView. This gives us the following code for the Main Activity :
The execution of the Android Applications gives us the expected result :
That’s all for that tutorial. To discover more tutorials on Android development, don’t hesitate to subscribe to the SSaurel’s Channel on YouTube :
Kujifunza jinsi ya kuonyesha wakati wa sasa katika maneno ya Kiingereza kwenye Android Kama developer, unajua pengine jinsi ya kupata wakati wa sasa kupitia SDK ya kawaida, lakini unajua jinsi ya kuonyesha wakati huu katika maneno ya Kiingereza ili kuruhusu watumiaji kusoma moja kwa moja wakati kwenye skrini? Kwa mfano, unaweza kupata mafunzo haya kwenye YouTube, lakini kwa kutumia algorithm ya Kiingereza, unaweza kuamua wakati wa sasa. Lakini kwanza, sisi ni kwenda kujenga user interface (UI) ya maombi yetu Android. Algorithm ya kubadilisha wakati kwa maneno ya Kiingereza Sasa, tutaunda algorithm ya kubadilisha wakati wa sasa katika maneno ya Kiingereza. Katika algorithm yetu, tutahitaji safu kuhifadhi maneno kutumika kuandika muda katika Kiingereza. Kisha, ni lazima tupate wakati wa sasa. Kutoka wakati huu, sisi kutoa saa na dakika maadili. Katika algorithm, sisi kuanza kwa kuangalia kama tunahitaji kuchapisha neno dakika au dakika. Lazima kuchapisha dakika kama thamani ya dakika ni moja au hamsini na tisa. "Kama tuna dakika kutoka saa thelathini na moja hadi hamsini na tisa, tunachapisha saa mbele ya saa iliyopewa kama sita hamsini na tano itakuwa dakika tano hadi saba na si dakika tano hadi sita wakati tunachapisha saa mbele ya saa ya kutoa, tunakabiliwa na tatizo saa kumi na mbili kwa sababu ikiwa tunachapisha saa moja mbele ya kumi na mbili, itakuwa kumi na tatu. """ Lakini, tunataka moja hivyo kanuni itakuwa kuangalia hii na kuamua nini saa ya kuchapisha katika maneno wakati dakika ni kutoka thelathini na moja hadi hamsini na tisa. Kama saa ni kumi na mbili, sisi kuhifadhi moja. Vinginevyo, sisi kuhifadhi maneno mbele ya saa fulani. Kwa ajili ya mapumziko ya algorithm, sisi mchakato kama kwamba: - Kwa dakika, sisi kuangalia kama dakika ni sawa na sifuri kisha sisi kuongeza O<unk>Clock thamani. - Kisha, ikiwa dakika ni sawa na kumi na tano, tunaongeza Quarter Past kabla ya maneno. - Kama dakika ni sawa na thelathini, sisi kuongeza Nusu iliyopita kabla ya maneno. - Kama dakika ni sawa na arobaini na tano, sisi kuongeza Quarter kwa tu kabla. - Kama dakika ni chini ya thelathini, hivyo kwa dakika kati ya moja na ishirini na tisa, sisi kujenga kuonyesha maalum. - Na hatimaye katika sehemu nyingine, sisi kujenga String kurudi kwa dakika kati ya thelathini na moja na hamsini na tisa. "Kama unavyoona, algorithm hii ina ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano"" wa ""mfano""" Ili kusasisha wakati wa sasa daima, tutatumia kitu cha Timer na tutapanga TimerTask kwa kiwango cha kudumu. Katika kipengele hiki cha TimerTask, tutapata wakati wa sasa katika Maneno ya Kiingereza na tutakuwa na kusasisha maandishi katika TextView. Hii inatupa kanuni ifuatayo kwa ajili ya shughuli kuu: Utekelezaji wa Programu ya Android inatupa matokeo inatarajiwa: Hiyo ni yote kwa ajili ya mafunzo hayo. Kugundua mafunzo zaidi juu ya maendeleo ya Android, usisite kujiandikisha kwa SSaurel's Channel kwenye YouTube: