Skip to content

Instantly share code, notes, and snippets.

1. Testing the most recent or latest emitted item :

  • In our tests, in most of the cases, we are only interested in the last emitted value.For instance, for any UI state we are mostly interested on what will be the final UI state. And to test the last emitted value, we can do the following
            @Test
            fun verifyErrorOnUpcomingShiftsFetchUIState() = runDeputyTest {
                // given fetched result is failure
                coEvery { getGroupedUpcomingShifts(request) } returns Failure()
 // when
@laaptu
laaptu / DpToPxAndPxToDp
Last active February 14, 2022 21:06
Android convert dp to px and vice versa
public static float convertPixelsToDp(float px){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return Math.round(dp);
}
public static float convertDpToPixel(float dp){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);
@laaptu
laaptu / BitmapToInputStream.java
Created June 12, 2014 09:26
Convert Bitmap to InputStream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
@laaptu
laaptu / CheckingTheme.java
Last active September 13, 2019 15:56
How to get theme id
TypedValue outValue = new TypedValue();
getTheme().resolveAttribute(R.attr.themeName, outValue, true);
if(outValue.equals(getString(R.string.custom_title_theme))){
//you have applied this theme else not
}
//http://stackoverflow.com/questions/7267852/android-how-to-obtain-the-resource-id-of-the-current-theme
@laaptu
laaptu / AssetFileStringReader
Last active March 17, 2019 15:10
Android read file as string from asset
public String ReadFromfile(String fileName, Context context) {
StringBuilder ReturnString = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = context.getResources().getAssets()
.open(fileName);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
@laaptu
laaptu / CollapseValueAnimator
Last active March 14, 2019 07:31
Android collapse animation by ValueAnimator
private void valueAnimateAndDelete(final View view,
final int deletePosition) {
final ViewGroup.LayoutParams lp = view.getLayoutParams();
final int originalHeight = view.getHeight();
ValueAnimator animator = ValueAnimator.ofInt(originalHeight, 0);
animator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator arg0) {
@laaptu
laaptu / GetActionBarHeight
Created December 9, 2013 05:51
Android getting ActionBar Height in dip
public int getActionBarHeight() {
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv,
true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(
tv.data, getResources().getDisplayMetrics());
} else {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
@laaptu
laaptu / CustomScrollView
Created September 5, 2013 08:03
Android View Pager or Horizontal Scroll View/List inside a vertical ScrollView http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class VerticalScrollView extends ScrollView {
public VerticalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@laaptu
laaptu / ButtonProgress.java
Last active June 14, 2018 07:56
Using Styles values as custom attributes
package com.zala.widgets;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.zala.R;
@laaptu
laaptu / SmoothInterpolator
Created September 17, 2013 07:52
Android smooth interpolator as per Cyril Mottier
//as per cyrilmottier.com/2012/05/22/the-making-of-prixing-fly-in-app-menu-part-1/
public class SmoothInterpolator extends LinearInterpolator {
@Override
public float getInterpolation(float input) {
return (float) Math.pow(input - 1, 5) + 1;
}
}