Skip to content

Instantly share code, notes, and snippets.

View alphamu's full-sized avatar

Ali Muzaffar alphamu

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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;