Git Product home page Git Product logo

timesincetextview's Introduction

TimeSinceTextView

This is a subclass of android.widget.TextView that exposes a method setDate() which accepts a long Unix timestamp or java.util.Date. The view converts the date into a String which describes the date in terms of time since that timestamp. For example, if the current timestamp is Unix 1453503166 and we call timeSinceTextView.setDate(1453503116), "50 seconds ago" is displayed.

Changelog

Javadoc

Comparison to DateUtils.getRelativeTimeSpanString

I actually wrote this library before I knew about DateUtils.getRelativeTimeSpanString, but the output is actually quite a bit different. The DateUtils implementation should return localized text and allows for customizable flags. See here for a comparison of the output of different time stamps.

Usage

Simply declare a TimeSinceTextView in XML or create one in code.

<com.ddiehl.timesincetextview.TimeSinceTextView
  android:id="@+id/timestamp"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

Then call setDate(Date) or setDate(long) with a Unix timestamp, and the text will be automatically generated and set to the view.

((TimeSinceTextView) findViewById(R.id.timestamp)).setDate(1452827942);

To get an abbreviated form of the converted text, add app:abbreviated="true" to your XML layout.

<com.ddiehl.timesincetextview.TimeSinceTextView
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:tstv_abbreviated="true" />

The class TimeSince also contains static methods which can be used to retrieve a relative timestamp string without an instance of TimeSinceTextView.

Add to your project

Release

repositories {
    maven { url "https://jitpack.io" }
}
dependencies {
  compile 'com.github.damien5314:TimeSinceTextView:1.+'
}

Screenshot

Contributions

Pull requests are welcome, in particular in would be nice to have strings.xml translated into as many languages as possible.

timesincetextview's People

Contributors

damien5314 avatar vkotovv avatar jorgedls avatar jewelzqiu avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.