Skip to content

Instantly share code, notes, and snippets.

View kwong93's full-sized avatar

Kevin Wong kwong93

View GitHub Profile
@kwong93
kwong93 / gist:3dcea9f4207a85842ada1bb1b680f798
Last active October 7, 2017 00:49
Show or hide Android soft keyboard
public static void hideKeyboard(Context context, View view) {
getInputMethodManager(context).hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public static void showKeyboard(Context context, View view) {
getInputMethodManager(context).showSoftInput(view, 0);
}
public static InputMethodManager getInputMethodManager(Context context) {
return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
@kwong93
kwong93 / gist:66dfcad029578bfc4780548f540a0533
Last active February 15, 2017 21:46
rxjava:2.0.6, rxandroid:2.0.1
private List<String> getHeavyList() {
List<String> list = new ArrayList<>();
list.add("First string");
list.add("Second string");
list.add("Third string");
return list;
}
@kwong93
kwong93 / gist:b11a80ab982c8fd2992dc46061007357
Created February 16, 2017 19:38
RxJava flatMap single
/**
* Do 1 heavy operation, when successful, fire 2 more heavy operations sequentially that use the first operation's data
*/
private List<String> getHeavyList() {
List<String> list = new ArrayList<>();
list.add("First string");
list.add("Second string");
list.add("Third string");
@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) {
@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 / 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 / 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 / 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 / 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 / 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) {