Skip to content

Instantly share code, notes, and snippets.

@nichtemna
nichtemna / DateTimeUtils.java
Created April 28, 2016 10:11
Utilities class for converting date/time related strings
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTimeUtils {
public static String getCurrentDateToFormat(SimpleDateFormat dateFormat) {
return dateToFormat(new Date(), dateFormat);
}
@nichtemna
nichtemna / App.java
Created April 28, 2016 10:13
Android app foreground tracker
public class App extends Application {
private static LifecycleManager manager = new LifecycleManager();
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(manager);
}
public static boolean isAppForeground() {
@nichtemna
nichtemna / .gitignore
Created April 28, 2016 10:15
Android gitignore
# built application files
*.apk
*.ap_
# files for the dex VM
*.dex
# Java class files
*.class
@nichtemna
nichtemna / HintSpinnerAdapter.java
Created April 28, 2016 11:26
Adapter for spinner to show hint item when nothing chosen. Position of selected items is shifted for 1.
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
private boolean isEmailValid(String email){
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
@nichtemna
nichtemna / Utils
Created July 22, 2016 11:49
Android toggle keyboard programmaticaly
public static void showKeyboard(Context context, View view) {
InputMethodManager inputMethodManager =(InputMethodManager)context.getSystemService(Activity
.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInputFromInputMethod(view.getWindowToken(), 0);
}
public static void hideKeyboard(Context context, View view) {
InputMethodManager inputMethodManager =(InputMethodManager)context.getSystemService(Activity
.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
public class DayView extends LinearLayout implements View.OnClickListener {
private boolean isChecked = false;
private OnCheckedChangeListener changeListener;
private TextView textView;
private Enums.WeekDays day;
public DayView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
initAttrs(attrs);
@nichtemna
nichtemna / Singleton.java
Created August 4, 2016 08:42
Synchronized Singleton
public class Singleton{
private static Singleton instance;
private Singleton(){}
public static synchronized Singleton getInstance(){
if (instance == null){
instance = new Singleton();
}
return instance;
@nichtemna
nichtemna / MaxPairwiseProduct.java
Created August 10, 2016 08:15
Get sum of two biggest integers in array
static long getMaxPairwiseProduct(int[] numbers) {
long result = 0;
int n = numbers.length;
int max_position1 = -1;
for (int i = 0; i < numbers.length; i++) {
if ((max_position1 == -1) || (numbers[i] > numbers[max_position1])) {
max_position1 = i;
}
}
@nichtemna
nichtemna / Number.java
Created August 10, 2016 16:00
Get last digit from a number
int number = 101;
int lastDigit = number % 10;