Skip to content

Instantly share code, notes, and snippets.

@worker8
Last active December 4, 2015 05:57
Show Gist options
  • Select an option

  • Save worker8/9e354b705f6a89d02a92 to your computer and use it in GitHub Desktop.

Select an option

Save worker8/9e354b705f6a89d02a92 to your computer and use it in GitHub Desktop.
if(viewToMeasure.getViewTreeObserver().isAlive()) {
        viewToMeasure.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if(viewToMeasure.getViewTreeObserver().isAlive()) {
                    // only need to calculate once, so remove listener
                    viewToMeasure.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                // you can get the view's height and width here

                // the listener might not have been 'alive', and so might not have been removed....so you are only
                // 99% sure the code you put here will only run once.  So, you might also want to put some kind
                // of "only run the first time called" guard in here too                                        

            }
        });            
    }
  • ripple effect, res/drawable/ripple_gray.xml:
<?xml version="1.0" encoding="utf-8"?>

<!--<ripple xmlns:android="http://schemas.android.com/apk/res/android"--><!--android:color="@color/colorSecondaryGray5">--><!--<item android:drawable="@android:color/transparent" />--><!--</ripple>-->

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorSecondaryGray3">
    <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white" />
</ripple>

ripple with background

<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorSecondaryGray3"> <!-- The ripple will be red -->

    <!-- the normal bg color will be light grey -->
    <item>
        <color android:color="@color/colorSecondaryGray1" />
    </item>

    <!-- make sure the ripple doesn't exceed the bounds -->
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="@color/black" />
        </shape>
    </item>
</ripple>

use: android:background="@drawable/ripple_gray"

  • Java regex http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

  • How to create Junit

    • create a file under src/test/java//Filename.java
    • change Build Variant to unit test. Annotate with @Test, then tight click on the test method name and press run, and use Ctrl-R for subsequent run
  • RxJava simple

         Observable.just(1)
                  .observeOn(Schedulers.io())
                  .map(dummy_i -> {
                      
                  return 1;
                  }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Integer>() {
                      @Override
                      public void onCompleted() {
    
                      }
    
                      @Override
                      public void onError(Throwable e) {
    
                      }
    
                      @Override
                      public void onNext(Integer r) {
    
                      }
                  });
    
  • Spinner solution: http://stackoverflow.com/a/26475701/75579

package custom_view;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Taken from: http://stackoverflow.com/a/26475701/75579
 */
public class HintSpinnerAdapter<T> extends ArrayAdapter {
    public HintSpinnerAdapter(Context context, int resource) {
        super(context, resource);
    }

    public HintSpinnerAdapter(Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public HintSpinnerAdapter(Context context, int resource, Object[] objects) {
        super(context, resource, objects);
    }

    public HintSpinnerAdapter(Context context, int resource, int textViewResourceId, Object[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public HintSpinnerAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
    }

    public HintSpinnerAdapter(Context context, int resource, int textViewResourceId, List objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = super.getView(position, convertView, parent);
        if (position == getCount()) {
            ((TextView) v.findViewById(android.R.id.text1)).setText("");
            Object object = getItem(getCount());
            if (object instanceof String) {
                ((TextView)v.findViewById(android.R.id.text1)).setHint((String)object); //"Hint to be displayed"
            }

        }

        return v;
    }

    @Override
    public int getCount() {
        return super.getCount()-1; // you dont display last item. It is used as hint.
    }
}
         HintSpinnerAdapter<String> adapter = new HintSpinnerAdapter(mActivity, android.R.layout.simple_spinner_dropdown_item);

                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    String [] foodSpinnerArray = getResources().getStringArray(R.array.add_note_row_food_dropdown_array);
                    for (String string : foodSpinnerArray) {
                        adapter.add(string);
                    }

                    adapter.add(getString(R.string.add_note_row_meal_type)); //This is the text that will be displayed as hint.


                    spinner.setAdapter(adapter);
                    spinner.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
  • color animation example:
//        Integer colorFrom = from;
//        Integer colorTo = to;
//        ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
//        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animator) {
//                int color = (Integer) animator.getAnimatedValue();
//                mToolbarTitle.setTextColor(color);
//                mValueTV.setTextColor(color);
//                mUnitTV.setTextColor(color);
//            }
//        });
//        colorAnimation.setDuration(700);
//        colorAnimation.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment