Skip to content

Instantly share code, notes, and snippets.

public abstract class AbstractFragment extends Fragment {
protected <T> T getListener(Class<T> type){
for(Object o : new Object[]{getParentFragment(), getActivity()}){
if(type.instanceOf(o)){
return type.cast(o);
}
}
throw new RuntimeException("Missing listener " + type.getName());
}
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.peekvision.screening.R;
@alex-richards
alex-richards / Bind.java
Created December 1, 2015 09:55
What if Butterknife worked for Resources???
package com.github.alexrichards.pocketknife;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Bind {
public class ExampleActivity extends AppCompatActivity implements ExampleActivity.FooAdapter.OnFooSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(); //TODO
final ListView foos = findViewById(R.id.view_foos);
foos.setAdapter(new FooAdapter(this, getFoos()));
private int smoothingHead = 0;
private final float[] smoothing = new float[12];
/**
* @param in New value for smoothing.
* @return Average of the last 12 values.
*/
private float smoothRadians(final float in) {
smoothing[smoothingHead++ % smoothing.length] = in;
public class WhateverActivity extends Activity {
public void onCreate(final Bundle savedInstanceState){
super.onCreate();
final WhateverIntent intent = new WhateverIntent(getIntent());
final String whateverExtra = intent.getWhatever();
}
@alex-richards
alex-richards / CurrencyTest.java
Last active December 28, 2015 09:19
Currency Codes & Locales
import java.util.*;
import java.text.*;
public class CurrencyTest {
private static final Locale[] LOCALES = {
Locale.UK,
Locale.FRANCE,
Locale.US,
new Locale("en", "AU"),
@alex-richards
alex-richards / gist:6900515
Created October 9, 2013 12:31
Shutter count from 20D (supposedly).
for F in `ls *.CR2`; do echo "ibase=16;" `xxd -s 0x95D -l 2 -u -p $F` | bc; done