Skip to content

Instantly share code, notes, and snippets.

Avatar

Piyush Malaviya piyush-malaviya

  • Ahmedabad, Gujarat, India
View GitHub Profile
View Text Center
int xPos = (canvas.getWidth() / 2);
int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
//((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center.
canvas.drawText("Hello", xPos, yPos, textPaint);
https://stackoverflow.com/a/38415380
View AppUtils.java
import android.app.Activity;
import android.app.ActivityManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.support.v4.app.Fragment;
View StringUtils.java
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;
public class StringUtils {
private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
View EncryptDecryptConverterFactory.Java
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import android.util.Log;
import java.nio.charset.Charset;
View Global Exception Handling on android
Stackoverflow Link: https://stackoverflow.com/a/19968400
Handle uncaughtException, start activity:
public class MyApplication extends Application
{
public void onCreate ()
{
// Setup handler for uncaught exceptions.
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
View AccessibilityUtils.java
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.content.Context;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
@piyush-malaviya
piyush-malaviya / MultiPartUtility.java
Created Nov 27, 2019 — forked from Antarix/MultiPartUtility.java
Uploading Multiple files at once with Post and Header parameter in Android
View MultiPartUtility.java
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
View DisplayUtils.java
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
public class DisplayUtils {
private static DisplayUtils instance;
public static DisplayUtils getInstance() {
View RegexUtils.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexUtils {
//private static final String DISTANCE_PATTERN = "(?i)[0-9]{1,2}\\s??(?:mi|mile|miles|km)\\b";
private static final String DISTANCE_PATTERN = "(?i)((?:\\d+)?\\.?\\d+)\\s??(?:mi|mile|miles|km)\\b";
private static final String DISTANCE_UNIT_PATTERN = "(?i)(?:mi|mile|miles|km)\\b";
private static final String TIME_PATTERN = "(?i)[0-9]{1,2}\\s??(?:min|hr)";
private static final String TIME_UNIT_PATTERN = "(?i)(?:min|hr)";
@piyush-malaviya
piyush-malaviya / DeCryptor.java
Created Jun 21, 2019 — forked from JosiasSena/DeCryptor.java
Encryptor and Decryptor for data encryption.decryption using the Android KeyStore.
View DeCryptor.java
/**
_____ _____ _
| __ \ / ____| | |
| | | | ___| | _ __ _ _ _ __ | |_ ___ _ __
| | | |/ _ \ | | '__| | | | '_ \| __/ _ \| '__|
| |__| | __/ |____| | | |_| | |_) | || (_) | |
|_____/ \___|\_____|_| \__, | .__/ \__\___/|_|
__/ | |
|___/|_|
*/