Easily embed Twitter posts in your Android app
OS: Android
lang: Kotlin
lib: twitter-kit [link]
warning: twitter-kit is slowly maintained and has a library confict with Picasso [link] (as of Sep 2018)
Twitter provides us with an Android Twitter-Kit SDK for displaying specific tweets. Using a prebuilt library we benefit by having gallery-tweets, video-tweets and embedded replies out-of-the-box.
There are two ways in which one can embed Twitter kit in an Android app.
1. Directly embed tweetui.TweetView in you app’s view xml
2. Create tweetui.TweetView instance programatically
Since we want to use MVVM and easily set/change twitter id we will stick with the second scenario. Setting twitterId by directly embeding `TweetView` in xml is a hassle.
Steps:
1. Create a custom TwitterViewComponent
2. Set twitter id with data binding
3. Create a custom style for TweetView [Optional]
4. Prepare a proguarded version for release
5. Change date format for tweets in Twitter-Kit SDK [Optional]
1.Create TwitterViewComponent
First of all create a custom TwitterView component which we will later embed in other views. It will have a twitterId attribute. We will set this value dynamically via databinding.
TwitterViewComponent can be put anywhere in a view hierarchy e.g. inside a RecyclerView item and wrapped in a CardView
2.Set twitter id with data binding
app:twitterIdis set via tweetId databinding variable
Completed list item looks similar to the following example:

3. Create a custom style for TweetView [Optional]
We can also style how the TweetView looks. One can use tw__TweetLightStyle or tw__TweetDarkStyle .
4. Prepare a proguarded version for release
We can import twitter kit as an aar project as a whole or import only the things we need via separate maven includes.
- AAR project
dependencies {
compile('com.twitter.sdk.android:twitter:3.3.0@aar') {
transitive = true
}
}2. Separate maven includes
implementation 'com.twitter.sdk.android:twitter-core:3.1.1'
implementation 'com.twitter.sdk.android:tweet-ui:3.1.1'Proguard rules work (as of Sep 2018) only with the AAR project. Copy the rules from this link.
5. Change date format for tweets in Twitter-Kit SDK [Optional]
Theoretically in Android TwitterKit SDK we should be able to change Twitter date format by overriding two string resources (tw__relative_date_format_long and tw__relative_date_format_short) but it DID NOT work for me.
An alternative solution would be to write an Extension method which I did. Here’s a ready solution for changing date inside TweetView from Twitter SDK.
Have fun using Twitter in Android apps!
