Skip to content

Instantly share code, notes, and snippets.

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri;
import android.util.Log;
import com.squareup.picasso.Picasso;
@keyboardr
keyboardr / gist:572a5eae9f57be286c24
Created July 7, 2015 21:59
Centered circular reveal animator
@TargetApi(VERSION_CODES.LOLLIPOP)
private Animator createCenteredReveal(View view) {
// Could optimize by reusing a temporary Rect instead of allocating a new one
Rect bounds = new Rect();
view.getDrawingRect(bounds);
int centerX = bounds.centerX();
int centerY = bounds.centerY();
int finalRadius = Math.max(bounds.width(), bounds.height());
return ViewAnimationUtils.createCircularReveal(view, centerX, centerY, 0f, finalRadius);
}
@keyboardr
keyboardr / gist:3977796
Created October 30, 2012 01:32
Updating a view within a ViewPager
//This is in the inistantiateItem() method of my PagerAdapter.
//View view is my inflated layout, and SharedPreferences prefs contains my backing data.
final SeekBar seekBar = (SeekBar) view.findViewById(R.id.transparency_seekbar);
seekBar.setMax(80);
seekBar.setProgress(prefs.getInt(seekBar.getContext().getString(R.string.pref_transparency),
0));
seekBar.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
private IntentFilter mTransparencyFilter = new IntentFilter(
@keyboardr
keyboardr / NavActivity.java
Created May 12, 2013 09:44
Automatically handle HomeAsUp functionality
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.app.TaskStackBuilder;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
public abstract class NavActivity extends SherlockFragmentActivity {
@keyboardr
keyboardr / CachedLoader.java
Created May 12, 2013 10:01
A handy AsyncTaskLoader that contains common caching functionality.
import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
public abstract class CachedLoader<D> extends AsyncTaskLoader<D> {
private D mResult;
public CachedLoader(Context context) {
super(context);
}
@keyboardr
keyboardr / gist:5615431
Created May 20, 2013 20:49
IntentFilter for backdoor. Accessed by entering *#*#code#*#* into the dialer
<!-- Replace 111222 with your secret code -->
<receiver android:name=".receiver.DiagnoserReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SECRET_CODE"/>
        <data android:scheme="android_secret_code" android:host="111222"/>
    </intent-filter>
</receiver>
@keyboardr
keyboardr / StylableString.java
Last active December 18, 2015 16:19
Quick utils for simple Spannable styling
public class StylableString extends SpannableString {
public StylableString(CharSequence source) {
super(source);
}
public StylableString setForegroundColor(int color) {
ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
setSpan(colorSpan, 0, length(), 0);
return this;
@keyboardr
keyboardr / gist:6033986
Last active December 19, 2015 23:19
Concat strings with a delimeter
public class Util {
public static String buildConcatString(String delimeter, String... strings) {
StringBuilder builder = new StringBuilder();
boolean isFirst = true;
for (String string : strings) {
if (TextUtils.isEmpty(string)) {
continue;
}
if (!isFirst) {
@keyboardr
keyboardr / CursorReaderExample.java
Created November 15, 2016 23:46
Cursor example
import android.support.annotation.WorkerThread;
import android.support.annotation.Nullable;
import android.database.Cursor;
public class CursorReaderExample {
// Example columns, would likely be in a different file (e.g. a contract class).
private static String COLUMN_FOO = "foo";
private static String COLUMN_BAR = "bar";
@keyboardr
keyboardr / HeadlessFragmentFactory.java
Created December 8, 2016 01:54
Demonstration of why the HeadlessFragment gist can't be done as an abstract Factory class
/**
* What you'd think an abstract factory would look like. Unfortunately, this class won't compile.
*/
public abstract class HeadlessFragmentFactory<F extends Fragment & ParentedFragment<F, ? extends P>, P> {
protected abstract F newInstance();
/* There will be a compiler warning on P. When joining types, only the first type is allowed to
be a class. All others MUST be interfaces. In this situation, there is no way for the compiler
to know that P will be an interface. If P is removed, there is no longer a compile-time check