curioustechizen / android-ago

An Android TextView that always displays an auto refreshing relative time span with respect to a reference time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to setText manually on RelativeTimeTextView

NayanaRBhoj opened this issue · comments

How to setText manually on RelativeTimeTextView

@nayana123 I'm not sure what you mean. Could you provide an example? RelativeTimeTextView is after all a TextView so you could just call setText() on it but that defeats the purpose.

Yaah sure, in my case i want to display time as "Today", "Yesterday","Days ago".
so when setReferenceTime(long) is setTexted to RelativeTimeTextView in case of "2mins ago" or "1 hour ago" i want manually setText as "Today".

i tried by calling setText("Today") but it does't sets the text

@nayana123 Ah I see the problem now.

Unfortunately, your use case is not supported by the library currently. Once you call setReferenceTime(), the library automatically calls setText() internally at some intervals.

I will consider providing a way to override the reference time and force callers to set the text instead, however don't expect this to happen anytime soon.

In the meantime, I suggest you look into the source code of RelativeTimeTextView and customize it for your needs. It is only one file and there's not much logic in there any way.

In particular, look at these lines. I haven't actually tried this, but you should be able to do something like

return (difference >= 0 &&  difference<=DateUtils.DAY_IN_MILLIS) ? 
                getResources().getString(R.string.today): //Define "today" in your strings.xml
                DateUtils.getRelativeTimeSpanString(
                    mReferenceTime,
                    now,
                    DateUtils.DAY_IN_MILLIS,
                    DateUtils.FORMAT_ABBREV_RELATIVE);

@nayana123 Having said that, if you do not need the display string to be updated in the resolution of minutes, i.e., if you are only going to show differences in days, I strongly suggest you not to use this library at all. Just use a regular TextView along with the DateUtils class as follows:

myTextView.setText(
    DateUtils.getRelativeTimeSpanString(
            mReferenceTime,
            System.currentTimeInMillis(),
            DateUtils.DAY_IN_MILLIS,
            DateUtils.FORMAT_ABBREV_RELATIVE));

The android-ago library is useful if you have to keep refreshing the time even while the user is viewing your app. Once you set "Today" as the time, it won't change for a long time and the user will almost definitely NOT be looking at your time till it changes 😄

OK - I have a very general use case that I need to setText for the RelativeTimeTextView for. For example, when I refresh the view, I want the RelativeTimeTextView to say "Updating" but since I cannot setText on the RelativeTimeTextView, I could not change the TextView to reflect this. I had to create another textView on top of the RelativeTimeTextView and toggle the visibility of that textview and the RelativeTimeTextView when refreshing.
I think it would be a good enhance to make to your library if one can setText on the RelativeTimeTextView.

@Winghin2517 That is a valid use case. Thanks for pointing it out.

Do note that you can simply use setText on an RTTV. If you use both setReferenceTime and setText, the last one wins. So, if you want to set the text to "Updating", simply using rttv.setText("Updating"); should be enough.

If you have a different use case that prevents you from using this, please let me know so I can investigate how we can accommodate it.