Skip to content

Instantly share code, notes, and snippets.

@laaptu
laaptu / CheckConnection.java
Created January 22, 2014 06:13
Checking for internet connection
public static boolean isConnectedToInternet(Context context) {
if (isConnectingToInternet(context)) {
return isNetworkAvailable();
}
return false;
}
public static boolean isConnectingToInternet(Context context) {
@laaptu
laaptu / ExpandCollapse.java
Created January 9, 2014 10:10
Expand Collapse Animation of a view
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 {
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.text_menu, menu);
getMenuInflater().inflate(R.menu.actionview_menu, menu);
// menu.getItem(0).setVisible(false);
this.menu = menu;
MenuItem shareItem = menu.findItem(R.id.action_share);
ShareActionProvider actionProvider = (ShareActionProvider) MenuItemCompat
@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 / 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 / gist:7822254
Created December 6, 2013 11:26
Android getting status bar height
public int getStatusBarHeight(){
int result=0;\
int resourceId= getResources().getIdentifier("status_bar_height","dimen","android");
if(resourceId >0)
result = getResources().getDimensionPixelSize(resourceId);
return result;
}
@laaptu
laaptu / gist:7783545
Created December 4, 2013 07:12
Device Screen Width Height
WindowManager windowManager =(WindowManager)getSystemService(Context.WindowService);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
@laaptu
laaptu / AndroidEmailIntent
Created November 7, 2013 04:40
Android email intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto:"));
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
intent.putExtra(Intent.EXTRA_SUBJECT, Constants.SHARE_MSG);
intent.putExtra(Intent.EXTRA_TEXT, Constants.INVITE_MSG);
intent.putExtra(Constants.EXIT_ON_SENT, true);
startActivity(Intent.createChooser(intent,
getString(R.string.invite_via)));
@laaptu
laaptu / sumpercentage
Created October 22, 2013 05:19
Properly calculating sum to percentage with rounding error to +-1%
float a = 221, b = 77, c = 17, d = 88, e = 22, f = 44;
float total = a + b + c + d + e + f;
int sum = (int) Math.round(100.0 / total * a)
+ (int) Math.round(100.0 / total * b)
+ (int) Math.round(100.0 / total * c)
+ (int) Math.round(100.0 / total * d)
+ (int) Math.round(100.0 / total * e)
+ (int) Math.round(100.0 / total * f);
System.out.println(Math.round(100.0 / total * a));
System.out.println(Math.round(100.0 / total * b));
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.webkit.WebViewClient;