Skip to content

Instantly share code, notes, and snippets.

@ayushhgoyal
ayushhgoyal / DatabaseHandler.java
Last active February 1, 2018 03:49
This can be used to handle external sqlite database in android. Just edit the name of database, and create your own methods with openDB() and closeDB()
/**
* This file is taken from
* https://github.com/rbochet/External-SQlite-database/blob
* /master/src/fr/stackr/android/externaldb/DataBaseHelper.java
*
* @author ayush@click4tab.com
*
*/
public class DataBaseHelper extends SQLiteOpenHelper {
@ayushhgoyal
ayushhgoyal / HorizontalSwipe.java
Last active December 12, 2015 09:38
Android Horizontal swipe gesture detector.
package com.example.horizontalswipe;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ImageView;
@ayushhgoyal
ayushhgoyal / LoginAsyncTask.java
Last active December 13, 2015 18:58
Sample async task class..sends params and gets data in json format.
class LoginUser extends AsyncTask<String, Integer, String> {
HttpClient client = new DefaultHttpClient();
JSONObject json;
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
final static String URL = "http://www.your-url-here.com";
@Override
protected String doInBackground(String... params) {
@ayushhgoyal
ayushhgoyal / custom_toast_example.xml
Created February 16, 2013 07:07
This code snippet can be used to create a custom toast.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="horizontal"
android:padding="8dp" >
<TextView
@ayushhgoyal
ayushhgoyal / CustomLog.java
Created March 19, 2013 05:42
THis gist can be used to create custom logs, just add this class to your package and use its methods to generate a log with class name, method name and line of code. Use any method like this: CustomLog.logBlue( Thread.currentThread().getStackTrace(), string);
package com.ayushhgoyal.custom;
import android.util.Log;
public class CustomLog {
public static void logRed(StackTraceElement[] stackTraceElements, String msg) {
Log.e(stackTraceElements[2].getClassName().substring(
stackTraceElements[2].getClassName().lastIndexOf(".") + 1)
+ "."
+ stackTraceElements[2].getMethodName()
SimpleDateFormat fromUser = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat myFormat = new SimpleDateFormat("MMM d, ''yy");
try {
String reformattedStr = myFormat.format(fromUser
.parse("2013-09-12 09:09:09"));
CustomLog.logBlack(Thread.currentThread().getStackTrace(),
reformattedStr);
} catch (Exception e) {
@ayushhgoyal
ayushhgoyal / GetAddress
Created May 6, 2013 11:48
This snippet can be used to get address(approx) from a location's latitude and longitudes.
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
String address = "";
try {
addresses = geocoder.getFromLocation(latitude,
longitude, 1);
address = addresses.get(0).getAddressLine(0);
Log.e("address", address);
@ayushhgoyal
ayushhgoyal / DelayExecution
Created May 10, 2013 06:22
This snippet can be used to delay the execution of code by certain time.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//your code here- to be executed after delay
}
}, 2000); //2000 is 2 seconds here
@ayushhgoyal
ayushhgoyal / ChatHeadService.java
Last active December 18, 2015 11:59
Facebook chat heads in android application.
// I forked this code from- http://www.piwai.info/chatheads-basics/
// Chat head is displayed from a service.
// uses a permission: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
// This is a service so it needs to be declared in manifest: <service android:name=".ChatHeadService" />
// Whenever you need to display the chat head, just start the service
// example: startService(new Intent(context, ChatHeadService.class));
public class ChatHeadService extends Service {
private WindowManager windowManager;
@ayushhgoyal
ayushhgoyal / SimpleCustomAdapter.java
Created July 2, 2013 07:43
This is custom adapter sample file. It can be used to create different list views according to needs.
public class SimpleCustomAdapter extends SimpleAdapter {
private int[] colors = new int[] { 0x30FFFFFF, 0x30ccffcc };
Context context;
DatabaseHelper myDatabase;
public SimpleCustomAdapter(Context context,
List<HashMap<String, String>> items, int resource, String[] from,
int[] to) {
super(context, items, resource, from, to);
this.context = context;