Skip to content

Instantly share code, notes, and snippets.

@AnderWeb
AnderWeb / Simple setup for item backgrounds pre - post lollipop
Last active January 12, 2024 18:04
Simple setup for item backgrounds pre/post lollipop
AppCompat-v7:21 provides a very useful way of dealing with pressed/focused/activated states maintaining backwards compatibility downto API-7, but there's a small issue (big for some) with the default selectableItemBackground: It uses some PNGs and/or default values for API<21.
The main reason is that android drawable resource definitions (prior API 21) CANNOT use theme attributes at all, so there's no way of making something like:
<shape android:shape="rectangle">
<solid android:color="?attr/colorControlHighlight" />
</shape>
For this, I've put this simple mockup on how to give your app better drawables that the appcompat defaults.
@alex-shpak
alex-shpak / Interceptor.java
Last active June 24, 2025 03:32
Refreshing OAuth token with okhttp interceptors. All requests will wait until token refresh finished, and then will continue with the new token.
private class HttpInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
//Build new request
Request.Builder builder = request.newBuilder();
builder.header("Accept", "application/json"); //if necessary, say to consume JSON
@odedhb
odedhb / PermissionsHelper.java
Last active April 4, 2019 18:02
A simple class for Android Marshmallow. Showing a list of permissions, and allowing the user to change them. This was created to be implemented in http://wheredatapp.com, android's greatest search engine.
package com.nextstagesearch;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
@paraya3636
paraya3636 / KotlinAndroidMainApplication
Last active July 20, 2024 03:28
Kotlin Android MainApplication class for global applicationContext.
// Not object class. AndroidManifest.xml error happen.
class MainApplication : Application() {
init {
instance = this
}
companion object {
private var instance: MainApplication? = null
@heiswayi
heiswayi / SimpleLogger.cs
Last active March 14, 2024 00:19
Simple C# logger utility class
/*
MIT License
Copyright (c) 2016 Heiswayi Nrird
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@elizarov
elizarov / ContextCounter.kt
Last active April 28, 2021 12:42
Using Kotlin Coroutine context to keep a mutable counter
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.runBlocking
import java.util.concurrent.atomic.AtomicInteger
import kotlin.coroutines.experimental.AbstractCoroutineContextElement
import kotlin.coroutines.experimental.CoroutineContext
import kotlin.coroutines.experimental.intrinsics.*
// coroutine context element that keeps a (mutable) integer counter
class Counter : AbstractCoroutineContextElement(Key) {
@GBouerat
GBouerat / CursorLiveData.java
Last active March 7, 2020 09:00
First try at LiveData with Cursor and ContentProvider to keep using CursorAdapter
import android.app.Application;
import android.arch.lifecycle.LiveData;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@NickHolcombe
NickHolcombe / ListAdapterWithHeader.kt
Last active October 18, 2023 00:16
Example of a RecyclerView Adapter based on ListAdapter with a header
package example
import android.support.v7.recyclerview.extensions.AsyncDifferConfig
import android.support.v7.recyclerview.extensions.AsyncListDiffer
import android.support.v7.util.DiffUtil
import android.support.v7.util.ListUpdateCallback
import android.support.v7.widget.RecyclerView
/**
* {@link RecyclerView.Adapter RecyclerView.Adapter} base class based on
@Gnzlt
Gnzlt / LocationLiveData.kt
Last active March 11, 2020 15:33
Android Location LiveData in Kotlin
package com.example
import android.arch.lifecycle.LiveData
import android.content.Context
import android.location.Location
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import com.google.android.gms.tasks.OnSuccessListener
@trevorwhitney
trevorwhitney / SimpleHttpServer.kt
Created October 24, 2018 22:50
Simplest Java HTTP Server in Kotlin
package io.pivotal
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
import java.net.InetSocketAddress
fun main(args: Array<String>) {
val server = HttpServer.create(InetSocketAddress(8000), 0)
server.createContext("/test", MyHandler())