Skip to content

Instantly share code, notes, and snippets.

@hector6872
hector6872 / StringSpan.java
Created August 30, 2017 07:53
StringSpan - String.format that works with Android Spannables
// based on: https://github.com/george-steel/android-utils/blob/master/src/org/oshkimaadziig/george/androidutils/SpanFormatter.java
public class StringSpan {
private static final Pattern PATTERN = Pattern.compile("%([^a-zA-z%]*)([[a-zA-Z%]&&[^tT]]|[tT][a-zA-Z])");
private StringSpan() {
}
public static CharSequence format(@NonNull CharSequence format, Object... args) {
return format(Locale.getDefault(), format, args);
@hector6872
hector6872 / SupportVersion.java
Created September 11, 2017 09:43
SupportVersion
public class SupportVersion {
private SupportVersion() {
}
public static boolean isLollipopOrAbove() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
public static boolean isMarshmallowOrAbove() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
@hector6872
hector6872 / android-26-sources.md
Created September 12, 2017 13:28 — forked from cketti/android-26-sources.md
Build your own android-26 sources

If you are annoyed that "Sources for Android 26" are not yet available via SDK manager, this might be for you:

  1. Collect source files
mkdir android-sdk-source-build
cd android-sdk-source-build

mkdir -p frameworks/base
@hector6872
hector6872 / copyToUSB.sh
Created October 31, 2017 19:02
copyToUSB.sh
#!/bin/bash
SOURCE=$1
if [[ -z $SOURCE ]]; then
echo "Usage: $0 [source]" >&2
exit
fi
sudo mkdir -p /mnt/sd1
@hector6872
hector6872 / BaseActivity.kt
Last active November 16, 2017 09:31
MVP Kotlin
abstract class BaseActivity<MODEL : BaseModel<Bundle>, VIEW : BaseView, out PRESENTER : BasePresenter<MODEL, VIEW>> : KoinActivity() {
protected abstract val presenter: PRESENTER
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val arguments = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
val model = Class.forName(arguments.first().javaClass.toString().split(" ").last()).newInstance()
presenter.bind(model as MODEL, this as VIEW)
}
@hector6872
hector6872 / linux.sh
Created January 7, 2018 14:32
MP4 to GIF
# change resolution
ffmpeg -i video_1920.mp4 -vf scale=300:534 video_300.mp4
# convert
ffmpeg -i video_300.mp4 final.gif
@hector6872
hector6872 / ClearableAutoCompleteTextView.kt
Last active November 29, 2024 12:09
ClearableAutoCompleteTextView Kotlin
class ClearableAutoCompleteTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null,
defStyleAttr: Int = android.R.attr.editTextStyle)
: AutoCompleteTextView(context, attrs, defStyleAttr) {
private val clearDrawable: Drawable? = ContextCompat.getDrawable(context, R.drawable.ic_clear)
init {
clearDrawable?.setBounds(0, 0, clearDrawable.intrinsicWidth, clearDrawable.intrinsicHeight)
setOnTouchListener { _, event ->
if (isClearDrawableVisible() && event.action == MotionEvent.ACTION_UP) {
@hector6872
hector6872 / EditTextExtensions.kt
Created January 16, 2018 10:57
EditTextExtensions Kotlin
inline fun TextView.textWatcher(init: CustomTextWatcher.() -> Unit) = addTextChangedListener(CustomTextWatcher().apply(init))
@Suppress("unused")
class CustomTextWatcher : TextWatcher {
private var _beforeTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
private var _onTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
private var _afterTextChanged: ((Editable?) -> Unit)? = null
private var _beforeTextChangedShout: (() -> Unit)? = null
private var _onTextChangedShout: (() -> Unit)? = null
@hector6872
hector6872 / Bus.kt
Created January 18, 2018 15:13
Simple Event Bus Kotlin
object Bus {
// val event = Event<Any>()
}
class Event<TYPE> {
private val handlers = arrayListOf<((TYPE) -> Unit)>()
operator fun plusAssign(handler: (TYPE) -> Unit) {
handlers.add(handler)
}
@hector6872
hector6872 / Base64.kt
Last active May 31, 2021 12:41
Base64.kt for Kotlin
fun String.encodeBase64ToString(): String = String(this.toByteArray().encodeBase64())
fun String.encodeBase64ToByteArray(): ByteArray = this.toByteArray().encodeBase64()
fun ByteArray.encodeBase64ToString(): String = String(this.encodeBase64())
fun String.decodeBase64(): String = String(this.toByteArray().decodeBase64())
fun String.decodeBase64ToByteArray(): ByteArray = this.toByteArray().decodeBase64()
fun ByteArray.decodeBase64ToString(): String = String(this.decodeBase64())
fun ByteArray.encodeBase64(): ByteArray {
val table = (CharRange('A', 'Z') + CharRange('a', 'z') + CharRange('0', '9') + '+' + '/').toCharArray()