Skip to content

Instantly share code, notes, and snippets.

View alphamu's full-sized avatar

Ali Muzaffar alphamu

View GitHub Profile
@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 / ExpandableTextView.java
Created April 24, 2015 01:17
A TextView and that expand and contract to show more or less content. Using ellipses with the TextView or the TextUtils to calculate where to ellipsize text can return the wrong result as it doesn't cater for new line characters. This code will consistently returns the correct number of lines.
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.FrameLayout;
import android.widget.TextView;
@alphamu
alphamu / PinchGestureActivity.java
Last active July 27, 2021 14:38
How to detect a pinch gesture in Android using an OnTouchListener to figure it our yourself (gives you more control) or by using the ScaleGestureDetector provided by Android.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewConfiguration;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
@alphamu
alphamu / SharedPreferencesSingletonEnumKeyTemplate.java
Last active March 9, 2023 11:59
A SharedPreferences singleton that can be used to centralise and simplify reading and writing of SharedPerferences in your Android app. There are 2 versions, one that uses static String for Keys and an other that uses enums. Which one you use comes down to your preference, enums I feel provides better control when you have multiple programmers w…
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import android.content.Context;
import android.content.SharedPreferences;
/*
* A Singleton for managing your SharedPreferences.
*
* You should make sure to change the SETTINGS_NAME to what you want
* and choose the operating made that suits your needs, the default is
@alphamu
alphamu / AA Transition App theme between light and dark themes
Last active September 9, 2022 21:59
Example of how to change themes at runtime with a smooth animation. In this example, we just switch between a light and a dark theme. The activity animation is defined in the theme and as such will be default on all activities with the set theme. [Demo Video here](http://youtu.be/Ps0phswbHls).
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class TransitionThemeActivity extends AppCompatActivity implements View.OnClickListener {
@alphamu
alphamu / SeparatorTextView.java
Last active February 3, 2023 18:54
Demonstration of how to make a custom TextView which has a separator running across it. Screenshot of TextView https://raw.githubusercontent.com/alphamu/RxAndroidDemo/master/app/SeparatorTextViewSample.png
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;
/**
* Simple version used in the article:
@alphamu
alphamu / CreditCardEditText.java
Last active October 13, 2023 09:53
A simple EditText view for use with Credit Cards. It shows an image of the credit card on the right hand side.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
import android.util.SparseArray;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@alphamu
alphamu / CameraMicPermissionHelper.java
Last active May 17, 2019 09:03
Gist showing use cases of headless Fragments to request Android-M runtime permissions
public class CameraMicPermissionHelper extends Fragment {
private static final int REQUEST_CAMERA_MIC_PERMISSIONS = 10;
public static final String TAG = "CamMicPerm";
private CameraMicPermissionCallback mCallback;
private static boolean sCameraMicPermissionDenied;
public static CameraMicPermissionHelper newInstance() {
return new CameraMicPermissionHelper();
}
@alphamu
alphamu / NetworkHelper.java
Last active October 15, 2021 06:42
Gist showing the use case of a headless Fragment to check if internet is available
public class NetworkHelper extends Fragment {
public static final String TAG = "NetworkHelper";
public static final String CHECK_INTERNET = "network_connection";
private Activity mActivity;
AlertDialog mAlertDialog = null;
private BroadcastReceiver onNotice = new BroadcastReceiver() {
@Override
@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;