Skip to content

Instantly share code, notes, and snippets. laaptu

Block or report user

Report or block laaptu

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
laaptu / DpToPxAndPxToDp
Last active Nov 28, 2019
Android convert dp to px and vice versa
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);
laaptu /
Last active Sep 13, 2019
How to get theme id
TypedValue outValue = new TypedValue();
getTheme().resolveAttribute(R.attr.themeName, outValue, true);
//you have applied this theme else not
laaptu / AssetFileStringReader
Last active Mar 17, 2019
Android read file as string from asset
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);
laaptu / CollapseValueAnimator
Last active Mar 14, 2019
Android collapse animation by ValueAnimator
View CollapseValueAnimator
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() {
public void onAnimationStart(Animator arg0) {
laaptu / GetActionBarHeight
Created Dec 9, 2013
Android getting ActionBar Height in dip
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(,
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);
laaptu /
Last active Jun 14, 2018
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 /
Created Jun 12, 2014
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 / SmoothInterpolator
Created Sep 17, 2013
Android smooth interpolator as per Cyril Mottier
View SmoothInterpolator
//as per
public class SmoothInterpolator extends LinearInterpolator {
public float getInterpolation(float input) {
return (float) Math.pow(input - 1, 5) + 1;
laaptu /
Created Jan 9, 2014
Expand Collapse Animation of a view
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 {
You can’t perform that action at this time.