Skip to content

Instantly share code, notes, and snippets.

View alphamu's full-sized avatar

Ali Muzaffar alphamu

View GitHub Profile
@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();
}
<FrameLayout ...>
<ImageView
android:id="@+id/imgBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/baking"/>
<ScrollView
@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 / 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 / HorizontalBarView.java
Last active April 29, 2019 21:28
A View that displayers a horizontal bar like the kind that is used in a bar chart.
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
@alphamu
alphamu / AbstractParcelDiskCache.java
Last active May 2, 2019 18:04 — forked from VladSumtsov/DiskCache
Encrypted persisted and cached Parcelable data disklrucache and facebook conceal on Android.
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain;
import com.facebook.crypto.Crypto;
@alphamu
alphamu / activity_frosted_toolbar.xml
Created October 13, 2016 11:53
Sample layout for frosted toolbar talk
<android.support.design.widget.CoordinatorLayout>
<ImageView
android:id="@+id/imgBgBlur"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/app_bar_height"
android:scaleType="matrix"
android:visibility="visible" />
@alphamu
alphamu / CalendarDayDifference.java
Last active May 4, 2019 12:50
Returns the difference between the provided date and now. The difference is in actual calendar days rather than time difference based. This means 01/Jan/2015 23:50 and 02/Jan/2015 00:01AM has a difference of 1 day.
/**
* Returns the difference between the provided date and now.
* The difference is in actual calendar days rather than time difference based.
* This means 01/Jan/2015 23:50 and 02/Jan/2015 00:01AM has a difference of 1 day.
*
* @param date The date from which the difference to today has to be calculated.
* @return difference in calendar days.
*/
public static int getRealDayDifference(Date date) {
String timeString = null;
@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;
@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