Skip to content

Instantly share code, notes, and snippets.

@hilfritz
hilfritz / gist:2401230f5cd4435067437c2b8bb9eff2
Last active November 7, 2018 03:49
Kotlin if else equivalent
property?.let {
//if 'property' is not null
fancyPrint(it) //'it' refers to 'property'
} ?: run {
//'else' clause - property is null
showError()
}
@hilfritz
hilfritz / Example of mocking Retrofit method call
Created November 6, 2018 12:57
Android unit test: Mockito, Retrofit, Robolectric
//MODEL CLASS
interface Model{
void init(Gson gson);
public Single<List<ImgAlbum>> getPhotos();
}
/**
* @author Hilfritz Camallere on 6/11/18
*/
use rxPermissions
https://stackoverflow.com/questions/32854169/does-checking-the-never-ask-again-box-when-asking-for-a-runtime-permission-disab
final String[] objects = new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
findViewById(R.id.enableCamera).setOnClickListener(
view -> rxPermissions
@hilfritz
hilfritz / java: jodatime get all datetime in month (including timezone)
Created October 19, 2018 03:20
java: jodatime get all datetime in month (including timezone)
public static final ArrayList<DateTime> getAllDateTimeInMonth(int month, int year, String timezoneID){
Log.d(TAG, "getAllDateTimeInMonth: month:"+month);
DateTimeZone dateTimeZone = DateTimeZone.forID(timezoneID);
DateTime dateTime = new DateTime(year, month, 1,0, 0, dateTimeZone);
Log.d(TAG, "getAllDateTimeInMonth: month:"+month+" year:"+year);
ArrayList<DateTime> daysInMonthLabels = new ArrayList<DateTime>();
//from https://gist.github.com/jk2K/67502e1744e081dcfef5
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
@hilfritz
hilfritz / Android: Recyclerview inside NestedScrollview - Scroll problem
Last active September 25, 2018 09:58
Android: Recyclerview inside Scrollview - Scroll problem
//issue happens one pre lolipop devices
//https://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working
//must use NestedScrollView
fix:
mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
int action = e.getAction();
@hilfritz
hilfritz / Android + RxJava: Listen to view clicks
Created August 22, 2018 17:18
Android + RxJava: Listen to view clicks
//https://stackoverflow.com/questions/25457737/how-to-create-an-observable-from-onclick-event-android
Observable<View> clickEventObservable = Observable.create(new Observable.OnSubscribe<View>() {
@Override
public void call(final Subscriber<? super View> subscriber) {
viewIWantToMonitorForClickEvents.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (subscriber.isUnsubscribed()) return;
subscriber.onNext(v);
1. Find the longest String in an array of Strings
//ANSWER:
public String getLongestStringInArray(String[] array){
int index = 0;
int elementLength = array[0].length();
int arraySize = array.length;
for(int i=1; i< arraySize; i++) {
@hilfritz
hilfritz / Android: full screen dialogfragment (no statusbar)
Created August 15, 2018 09:25
Android: full screen dialogfragment (no statusbar)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen)
}
@hilfritz
hilfritz / Android: full screen dialog fragment
Created August 15, 2018 09:24
Android: full screen dialog fragment
//https://stackoverflow.com/questions/7189948/full-screen-dialogfragment-in-android
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
//val dialog = super.onCreateDialog(savedInstanceState)
// request a window without the title
//dialog.window!!.requestFeature(Window.FEATURE_NO_TITLE)
// the content
val root = RelativeLayout(activity)
root.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)