Skip to content

Instantly share code, notes, and snippets.

@curioustechizen
curioustechizen / UseApiKey.java
Created April 6, 2015 07:37
Android: Loading API Keys and other secrets from properties file using gradle
String apiKey = BuildConfig.API_KEY
@curioustechizen
curioustechizen / LiveDataScanOperator.kt
Created January 23, 2019 15:44
Analog of RxJava's Observable.scan operator for Android Architecture Components LiveData
/**
* Analog of RxJava's Observable.scan operator.
*
* Returns a LiveData that applies the [accumulator] function to the fist item in the source LiveData,
* and an initial value, then feeds the result of that function along with the second item emitted by
* the source LiveData into the same function, and so on until all items have been emitted by the source
* LiveData, emitting the result of each of these iterations.
*/
fun <T, R> LiveData<T>.scan(initialValue: R, accumulator: (R, T) -> R): LiveData<R> {
val returnLiveData = MediatorLiveData<R>()
@curioustechizen
curioustechizen / EnhancedCheckBox.java
Last active September 14, 2020 05:50
Android programmatically checkable widgets, differentiate between user clicks & programmatic setChecked calls. Based on this answer on StackOverflow: http://stackoverflow.com/a/14307643/570930. There are times when setting a switch to ON results in some action (Save to DB/ perform an HTTP POST etc). However, you want this action to happen only o…
/**
* An enhanced {@code CheckBox} that differentiates between user clicks and
* programmatic clicks. In particular, the {@code OnCheckedChangeListener} is
* <strong>not</strong> triggered when the state of the checkbox is changed
* programmatically.
*
*/
public class EnhancedCheckBox extends CheckBox implements ProgrammaticallyCheckable{
private CompoundButton.OnCheckedChangeListener mListener = null;
public EnhancedCheckBox(Context context) {
@curioustechizen
curioustechizen / ReceiverRegistrar.java
Last active June 11, 2020 18:15
Tiny utility class that helps avoid book-keeping code when dealing with registering and unregistering BroadcastReceivers. Hopefully, this will help you get rid of "Activity has leaked IntentReceiver that was originally registered here" messages without resorting to using boolean flags to keep track of your BroadcastReceivers.
package com.github.curioustechizen.safereg;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.util.ArrayMap;
/**
* Helper class to keep track of what {@code BroadcastReceiver}s have been
@curioustechizen
curioustechizen / main.dart
Created May 22, 2020 11:56
Flutter stateless widget blog: Step 3 - Use AnimatedContainer to use a Stateless Widget
import 'package:flutter/material.dart';
const kSelectedColor = Colors.blueAccent;
const kUnselectedColor = Colors.transparent;
const kSelectedShadowOffset = Offset(0.0, 4.0);
final kSelectedBorderRadius = BorderRadius.circular(32.0);
void main() {
runApp(MyApp());
}
@curioustechizen
curioustechizen / main.dart
Created May 22, 2020 11:21
Flutter stateless widget blog: Step 2 - Toggle animation with an isSelected state
import 'package:flutter/material.dart';
const kSelectedColor = Colors.blueAccent;
const kUnselectedColor = Colors.transparent;
const kSelectedShadowOffset = Offset(0.0, 4.0);
final kSelectedBorderRadius = BorderRadius.circular(32.0);
void main() {
runApp(MyApp());
}
@curioustechizen
curioustechizen / main.dart
Created May 22, 2020 11:01
Flutter stateless widget blog: Step 1 - One-time animation
import 'package:flutter/material.dart';
const kSelectedColor = Colors.blueAccent;
const kUnselectedColor = Colors.transparent;
const kSelectedShadowOffset = Offset(0.0, 4.0);
final kSelectedBorderRadius = BorderRadius.circular(32.0);
void main() {
runApp(MyApp());
}
class EspressoTextInputLayoutUtils{
/*
* Use this method to find the EditText within the TextInputLayout. Useful for typing into the TextInputLayout
*/
public static ViewInteraction onEditTextWithinTilWithId(@IdRes int textInputLayoutId) {
//Note, if you have specified an ID for the EditText that you place inside
//the TextInputLayout, use that instead - i.e, onView(withId(R.id.my_edit_text));
return onView(allOf(isDescendantOfA(withId(textInputLayoutId)), isAssignableFrom(EditText.class)));
}
@curioustechizen
curioustechizen / StateSavingArrayAdapter.java
Last active June 17, 2018 09:56
StateSavingArrayAdapter: An ArrayAdapter that knows how to save/restore its own state.
/**
* <p>
* An {@code ArrayAdapter} that also knows how to save and restore its state.
* Basically all it does is save/restore the array of objects being managed by
* the Adapter.
* </p>
*
* <p>
* Note that this only saves the items and not things like checked item
* positions. Those belong to the {@link ListView} itself. Consider using
@curioustechizen
curioustechizen / CascadingCustomLayout.java
Created March 19, 2018 08:33
Android: Cascade the value of a custom attribute from a parent view to a child view - solution to https://stackoverflow.com/q/15112522/570930
// See https://stackoverflow.com/q/15112522/570930
class CustomLayout extends WhateverLayout {
private float percent;
private CustomView customView;
//View constructors go here
private void init(){
//Inflation goes here if needed