Skip to content

Instantly share code, notes, and snippets.

View kwong93's full-sized avatar

Kevin Wong kwong93

View GitHub Profile
@kwong93
kwong93 / example.xml
Created February 7, 2018 23:10
Text with link string resource Android
<string name="text_link_resource"><span>Some example text <a href="https://gist.github.com/">link text</a></span></string>
@kwong93
kwong93 / viewmodel.java
Last active September 15, 2017 16:10
How to use new ViewModel library
/**
* The view model class
*/
public class TestViewModel extends AndroidViewModel {
private final StocksLiveData stocksLiveData;
public TestViewModel(Application application) {
super(application);
stocksLiveData = new StocksLiveData(application);
@kwong93
kwong93 / file.java
Created September 7, 2017 21:06
How to listen for sd card mount and unmount
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", intent.getAction());
}
};
@Override
protected void onResume() {
super.onResume();
@kwong93
kwong93 / file.java
Created September 7, 2017 20:35
Android how to get all external directory files including sd card
public List<File> getExternalDirectoryRoots(Context context) {
File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(context, null);
List<File> externalDirectories = new ArrayList<>();
Set<String> stringSet = new HashSet<>();
for (File file : externalFilesDirs) {
String[] split = file.getAbsolutePath().split("/");
if (split.length > 1) {
@kwong93
kwong93 / gist:642bb05ead3f33725f1fb57ef54d98b0
Last active June 24, 2020 10:48
How to handle button clicks on notification for playing media in service using media session
// Took forever because of crappy tutorials, for example https://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice.html
// Key information was in COMMENTS in MediaButtonReceiver.java
// The key is to have a receiver with filter android.intent.action.MEDIA_BUTTON AND in the service
// register pending intent to the button in notification
builder.addAction(new NotificationCompat.Action(
R.drawable.ic_skip_next_black_24dp, getString(R.string.skip_next),
MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)));
// manifest changes
@kwong93
kwong93 / exoPlayerCacheMp3.java
Created August 6, 2017 16:23
How to cache mp3 files using ExoPlayer
private final ExoPlayer exoPlayer;
public PlayerExoPlayer(ExoPlayer exoPlayer) {
this.exoPlayer = exoPlayer;
}
public void prepareMp3(Context context, Uri uri) {
DefaultBandwidthMeter defaultBandwidthMeter = new DefaultBandwidthMeter();
String userAgent = System.getProperty("http.agent");
@kwong93
kwong93 / html.java
Created March 8, 2017 03:17
fromHtml and toHtml deprecation utility methods
@SuppressWarnings("deprecation")
public static Spanned fromHtml(String source) {
if (source == null) {
return null;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY);
}
@kwong93
kwong93 / 1.java
Created February 28, 2017 03:41
endless recyclerview bottom scroll check
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
if (!recyclerView.canScrollVertically(1)) {
}
}
@kwong93
kwong93 / 1.js
Created February 28, 2017 00:17
nativescript propertyCommitted ui pro dataform
exports.onPropertyCommitted = function (args) {
if (args.propertyName == "height") {
var ageEntityProperty = dataForm.getPropertyByName("age");
if (ageEntityProperty) {
if (args.entityProperty.originalValue == 2) {
ageEntityProperty.hidden = true;
} else {
ageEntityProperty.hidden = false;
}
@kwong93
kwong93 / gist:b970f5950df7a0828a020af9b90e556c
Last active February 22, 2017 23:25
glide-4.0.0-20170214.032747-210 Glide v4 4.0.0 snapshot test gif play on click without white flicker
final String gifUrl = "http://i.imgur.com/LU208u2.gif";
final RequestOptions requestOptions = new RequestOptions()
.fitCenter(this);
Glide.with(LoginActivity.this).asBitmap().load(gifUrl).apply(requestOptions).into(imageViewPreview);
final View.OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {