Skip to content

Instantly share code, notes, and snippets.

sanimap@gmail.com 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
laaptu / DpToPxAndPxToDp
Last active Sep 14, 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
laaptu / CheckingTheme.java
Last active Sep 13, 2019
How to get theme id
View CheckingTheme.java
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 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()
.open(fileName);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
@laaptu
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() {
@Override
public void onAnimationStart(Animator arg0) {
@laaptu
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 (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,
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
laaptu / ButtonProgress.java
Last active Jun 14, 2018
Using Styles values as custom attributes
View ButtonProgress.java
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 / BitmapToInputStream.java
Created Jun 12, 2014
Convert Bitmap to InputStream
View BitmapToInputStream.java
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
@laaptu
laaptu / SmoothInterpolator
Created Sep 17, 2013
Android smooth interpolator as per Cyril Mottier
View SmoothInterpolator
//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;
}
}
@laaptu
laaptu / ExpandCollapse.java
Created Jan 9, 2014
Expand Collapse Animation of a view
View ExpandCollapse.java
public void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
You can’t perform that action at this time.