All Projects → curioustechizen → Android Ago

curioustechizen / Android Ago

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

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Android Ago

Blitz
Android Library: Set self-updating string with relative time in TextView (e.g. 5 minutes ago)
Stars: ✭ 217 (-67.56%)
Mutual labels:  time, textview
Wmzdialog
功能最多样式最多的弹窗,支持普通/微信底部/日期/地区/日历/选择/编辑/分享/菜单/自定义弹窗等,支持多种动画,链式编程调用(Pop-up windows with the most functions and styles, support normal/WeChat bottom/date/region/calendar/select/edit/share/menu/custom pop-up windows, etc., support multiple animations, chain programming calls)
Stars: ✭ 673 (+0.6%)
Mutual labels:  time, textview
Knptimebundle
Provides helpers for time manipulation
Stars: ✭ 459 (-31.39%)
Mutual labels:  time
Period
PHP's time range API
Stars: ✭ 616 (-7.92%)
Mutual labels:  time
Angular Moment Picker
Angular Moment Picker is an AngularJS directive for date and time picker using Moment.js.
Stars: ✭ 536 (-19.88%)
Mutual labels:  time
Stacklabel
🔥空祖家的堆叠标签(以下碎念:一开始起名字“StackLabel”没想太多结果被人吐槽Stack是整齐堆叠的意思...........好吧这是我的锅不过现在要改也来不及了,好用就行了...吧?
Stars: ✭ 471 (-29.6%)
Mutual labels:  textview
Carbon
A simple, semantic and developer-friendly golang package for datetime
Stars: ✭ 565 (-15.55%)
Mutual labels:  time
Biz
Time calculations using business hours.
Stars: ✭ 448 (-33.03%)
Mutual labels:  time
Gsyricktext
类似微博的emoji表情、@人、话题等的EdiText,优化了编辑框中的光标点击和删除处理。TextView支持emoji表情、话题、链接、电话和@某人特殊显示的文本。
Stars: ✭ 651 (-2.69%)
Mutual labels:  textview
Timewarrior
Timewarrior - Commandline Time Reporting
Stars: ✭ 528 (-21.08%)
Mutual labels:  time
Linear Time Picker
Gorgeous Android Time and Date picker library inspired by the Timely app
Stars: ✭ 613 (-8.37%)
Mutual labels:  time
Simplifyspan
A easy-to-use and powerful Spannable library
Stars: ✭ 522 (-21.97%)
Mutual labels:  textview
Flipview
Flipping views like Gmail & beyond
Stars: ✭ 477 (-28.7%)
Mutual labels:  textview
Klock
Multiplatform Date and time library for Kotlin
Stars: ✭ 569 (-14.95%)
Mutual labels:  time
Pendulum
Python datetimes made easy
Stars: ✭ 4,639 (+593.42%)
Mutual labels:  time
Selectabletextview
A text view that supports selection and expansion
Stars: ✭ 626 (-6.43%)
Mutual labels:  textview
Circlealarmtimerview
A custom reusable circular slider control for Android application 可重用的圆形双滑块,类似iOS时间设置
Stars: ✭ 455 (-31.99%)
Mutual labels:  time
Kotlinextensions.com
A handy collection of most commonly used Kotlin extensions to boost your productivity.
Stars: ✭ 522 (-21.97%)
Mutual labels:  textview
Textviewexpandableanimation
Expandable TextView With Smooth Transition Animation
Stars: ✭ 537 (-19.73%)
Mutual labels:  textview
React Timekeeper
Google Keep app inspired time picker for react 🕓
Stars: ✭ 651 (-2.69%)
Mutual labels:  time

android-ago

This library provides RelativeTimeTextView, a custom TextView that takes a reference time and always displays the relative time with respect to the reference point, automatically refreshing the display text as needed. This is a common pattern seen in several apps like chat apps, social networking, email etc.

Here is a screenshot from the sample app

This library can be seen as a wrapper on top of the excellent android.text.format.DateUtils class. Note that the library does not expose all the options provided by the DateUtils class. I have left out many features because I couldn't decide what would be the best way to achieve the flexibility - dozens of XML attributes? Contributions in this regard are welcome.

Why should I use this instead of DateUtils class?

Because this library automatically refreshes the display text as needed. It internally uses DateUtils class.

Imagine you use DateUtils directly without using this library.

  • Imagine that it is 9 am now. You set a reference time of 9:05 am. Your TextView displays in 5 mins
  • Now the time becomes 9:01 am. You still display in 5 mins even though you should be showing in 4 mins

To do this correctly, you will need to keep refreshing the text views every minute. However, even that is not necessary. If the reference time is 3 hours from now, you only need to refresh every hour - not every minute.

This library handles all of this for you. RelativeTimeTextView automatically refreshes the display text only as often as necessary.

Obtaining

Gradle

Add the following to your build.gradle

dependencies {
    compile 'com.github.curioustechizen.android-ago:library:1.4.0'
}

Important: v1.3.4 Fixed a major bug (#47). If you are using an older version, please update to at least 1.3.4 now.

Eclipse+ADT

  1. Clone the repo
  2. In Eclipse, go to File -> New -> Other. Expand Android and select Android Project from Existing Code
  3. Browse to the android-ago sub-folder of the cloned repo and hit Finish

Usage

  • Include RelativeTimeTextView in your layouts.
  • Set the reference time either using setReferenceTime method or using the XML attribute reference_time.

In your layout:

<com.github.curioustechizen.ago.RelativeTimeTextView
    android:id="@+id/timestamp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_primary" />

In your Java code:

RelativeTimeTextView v = (RelativeTimeTextView)findViewById(R.id.timestamp); //Or just use Butterknife!
v.setReferenceTime(new Date().getTime());

See the sample project for a concrete example.

Customization

By default, this library simply calls DateUtils.getRelativeTimeSpanString. This might not be sufficient for you. For example, you might need to add a prefix. RTTV provides a hook for such cases - the getRelativeTimeDisplayString method. You can override this method and add whatever prefixes or suffixes you need.

Here is a simple example:

<!-- strings.xml -->
<string name="format_relative_time_with_prefix">Updated %1$s</string>
class PrefixRttv extends RelativeTimeTextView {
    @Override
    protected CharSequence getRelativeTimeDisplayString(long referenceTime, long now) {
        final String relativeTime = super.getRelativeTimeDisplayString(referenceTime, now);
        return getResources.getString(R.string.format_relative_time_with_prefix, relativeTime);
    }    
}

More examples in the sample project

Advanced customization

What if the string returned by DateUtils.getRelativeTimeSpanString does not suit you? Well, you can still use RTTV for its auto-refresh capability and take over complete control of the display string itself. Simply override getRelativeTimeDisplayString and don't call through to the super method. Instead, perform your own logic and return whatever string you wish here.

<!-- strings.xml -->
<string name="future">Some day, in the distance future</string>
<string name="past">Once upon a time, long long ago</string>
<string name="now">Right NOW!</string>
class FullyCustomRttv extends RelativeTimeTextView {
    @Override
    protected CharSequence getRelativeTimeDisplayString(long referenceTime, long now) {
        //Notice that we don't call super here.
        int resourceId = 0;
        if(referenceTime == now) resourceId = R.id.now;
        else if(referenceTime > now) resourceId = R.id.future;
        else resourceId = past;
        
        return getResources().getString(resourceId);
    }    
}

See the examples in the sample project for more details.

Who's Using this Library?

See here. If you would like to add your app to this list, please edit the wiki.

Android version support statement

The library has been tested on API 11 and above. However, theoretically, it works on API 3 and above since all it uses is [DateUtils#getRelativeTimeSpanString](http://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString(long, long, long, int)).

The minSdkVersion has been set to 8, however do not expect support from me for API version < 11.

Usage with Data Binding

See android-ago-sample-databinding for an example of how to use this library with the Android data binding library. Thanks to @Dev-IL for providing this sample.

License

Copyright 2017 Kiran Rao

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].