Instantly share code, notes, and snippets.

View DpToPxAndPxToDp
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);
View GetActionBarHeight
public int getActionBarHeight() {
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv,
actionBarHeight = TypedValue.complexToDimensionPixelSize(, getResources().getDisplayMetrics());
} else {
actionBarHeight = TypedValue.complexToDimensionPixelSize(,
TypedValue outValue = new TypedValue();
getTheme().resolveAttribute(R.attr.themeName, outValue, true);
//you have applied this theme else not
View CustomScrollView
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);
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;
View AssetFileStringReader
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()
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
View SmoothInterpolator
//as per
public class SmoothInterpolator extends LinearInterpolator {
public float getInterpolation(float input) {
return (float) Math.pow(input - 1, 5) + 1;
public void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
protected void applyTransformation(float interpolatedTime,
Transformation t) {
if (interpolatedTime == 1) {
} else {
View WebViewNonLeakMemory
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.webkit.WebViewClient;