Skip to content

Instantly share code, notes, and snippets.

View alphamu's full-sized avatar

Ali Muzaffar alphamu

View GitHub Profile
@alphamu
alphamu / UndoBarController.java
Last active May 4, 2019 12:54
This Gist demonstrates how to modify the UndoBarController created by Roman Nurik so implement functionality which will hide the UndoBar once it loses focus. Original code: https://code.google.com/p/romannurik-code/source/browse/misc/undobar/
/*
* Copyright 2012 Roman Nurik
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@alphamu
alphamu / Android Using Handler With Orientation Change
Created August 15, 2013 06:54
Example on how to create a static handler in an Android Activity so that there are no memory leaks and we can support orientation change and still have messages with a long delay delivered to the activity in the foreground.
package com.alimuzaffar.android.handlertest;
import java.lang.ref.WeakReference;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
@alphamu
alphamu / Android - Resize Drawable
Last active January 13, 2020 15:31
Code on how to get a bitmap from a Drawable and how to resize that bitmap and return a Drawable. This could prove to be useful if you want to manually resize a drawable or if you want to simply get a bitmap of a drawable. Note: If using Compound Drawables you can simply call `setBounds` on the drawable and set it using the `setCompoundDrawable` …
private Drawable resize(Resources r, Drawable image, int newSize) {
Bitmap b = ((BitmapDrawable)image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(b, newSize, newSize, false);
BitmapDrawable drawableBmp = new BitmapDrawable(r, bitmapResized);
return drawableBmp;
}
@alphamu
alphamu / android - SquareLinearLayout
Created September 16, 2013 05:46
Android LinearLayout with the same height as width.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class SquareLinearLayout extends LinearLayout {
public SquareLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@alphamu
alphamu / Android - get views with tag
Created November 4, 2013 12:32
Android get all views with a tag
public static ArrayList<View> getViewsByTag(ViewGroup root, String tag) {
ArrayList<View> views = new ArrayList<View>();
final int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = root.getChildAt(i);
if (child instanceof ViewGroup) {
views.addAll(getViewsByTag((ViewGroup) child, tag));
}
final Object tagObj = child.getTag();
@alphamu
alphamu / Android - DIP to PX or PX to DIP
Last active February 24, 2020 16:32
Android - DIP to PX or PX to DIP
public static int dpToPx(Context context, int dp) {
Resources r = context.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
return (int) px;
}
public static int pxToDp(Context context, int px) {
final float scale = context.getResources().getDisplayMetrics().density;
int dp = (int) (px * scale + 0.5f);
return dp;
@alphamu
alphamu / Android-TextViewWithFont.java
Last active December 29, 2015 12:09
A textview that can use a custom font without having to set it in code all the time
public class TextViewWithFont extends TextView {
private Context mContext;
private String mFont;
public TextViewWithFont(Context context) {
super(context, null);
mContext = context;
init();
}
@alphamu
alphamu / Percentages expressed as HEX values
Last active May 4, 2019 12:52
Hexadecimal percentages from from 0 - 100 in increments of 5. These are particularly useful for alpha values in ARGB strings.
100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
@alphamu
alphamu / CallMultipartRequest.java
Last active October 26, 2017 06:06
An implementation of Request<String> for Google Volley which posts data using `multipart/form-data`. Volley by default uses `application/x-www-form-urlencoded` which may not be supported by all services.
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.HttpHeaderParser;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@alphamu
alphamu / BitmapUploadUsingSslHttpStack.java
Last active May 4, 2019 12:51
Simple implementation of SslHttpStack using Apache 4.2 api (compiled under a different package name so it doesn't conflict with Android). Using the SslHttpStack to upload bitmaps.
HttpClient client = sslHttpStack.getmHttpClient();
HttpEntityEnclosingRequestBase entityRequest;
entityRequest = new HttpPost("http://my-server.com.au/my-api/customer/data");
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.addTextBody("someparameter", params.toString()); //params in this case is a JSONObject
String uniqueListingId = "images_to_upload"; //used to name any images being uploaded
int count = 0;