Skip to content

Instantly share code, notes, and snippets.

@alexshr
alexshr / DataBindingKTX
Last active Apr 18, 2020
fragment binding delegate
View DataBindingKTX
allprojects {
repositories {
.......
maven { url "https://jitpack.io" }
}
--------------------
//ext lib
implementation 'com.github.wada811:DataBinding-ktx:2.0.2'
-------------------------------------
@alexshr
alexshr / LiveEventNavigation.kt
Last active Aug 12, 2019
Navigation using LiveEvent subscription
View LiveEventNavigation.kt
sample:
https://github.com/alexshr/android-kotlin-fundamentals-apps/tree/master/MarsRealEstateFinal
---------------------------------------------
//https://github.com/hadilq/LiveEvent
implementation "com.github.hadilq.liveevent:liveevent:1.0.1"
-----------------------------
ViewModel:
/**
@alexshr
alexshr / GetFragmentViewModel.kt
Last active Aug 11, 2019
Get ViewModel scoped to this Fragment (fragment-ktx)
View GetFragmentViewModel.kt
//my sample
//https://github.com/alexshr/android-kotlin-fundamentals-apps/tree/master/MarsRealEstateFinal
//doc
//https://developer.android.com/reference/kotlin/androidx/fragment/app/package-summary#viewmodels
version_fragment = '1.2.0-alpha01'
implementation "androidx.fragment:fragment-ktx:${version_fragment}"
--------------------------------------------------------------------------------------
@alexshr
alexshr / SnackbarAnko.kt
Last active Aug 7, 2019
show snackbar (using anko view extension)
View SnackbarAnko.kt
// Observe snackbar event
sleepTrackerViewModel.showSnackBarEvent.observe(this, Observer {
binding.clearButton.snackbar(it)
})
//Anko
implementation "org.jetbrains.anko:anko-commons:$version_anko"
implementation "org.jetbrains.anko:anko-design:$version_anko" // For SnackBars
@alexshr
alexshr / SnackBarLiveEvent.kt
Last active Aug 7, 2019
LiveEvent sample (for snackbar)
View SnackBarLiveEvent.kt
/**
* Snackbar LiveEvent
* (see https://github.com/hadilq/LiveEvent)
* This is private because we don't want to expose setting this value to the Fragment.
*/
private val snackBarEvent = LiveEvent<String>()
val showSnackBarEvent: LiveData<String> = snackBarEvent
//https://github.com/hadilq/LiveEvent
@alexshr
alexshr / GetFragArgsFromNavActionKtx.kt
Last active Aug 7, 2019
Get fragment args from navigation action (lazy delegate from navigation-ktx)
View GetFragArgsFromNavActionKtx.kt
//using ktx for args https://developer.android.com/kotlin/ktx#navigation
val arguments by navArgs<SleepQualityFragmentArgs>()
@alexshr
alexshr / kotlin_supress_expiremental_api
Created Apr 30, 2019
kotlin coroutines @file:Suppress("EXPERIMENTAL_API_USAGE")
View kotlin_supress_expiremental_api
@file:Suppress("EXPERIMENTAL_API_USAGE")
View isServiceRunning.java
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
boolean isRunning = false;
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (LocationService.class.getName().equals(service.service.getClassName())) {
isRunning = true;
}
}
Timber.d("isRunning=%b", isRunning);
return isRunning;
View drawableToBitmap.java
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
@alexshr
alexshr / init_timber.java
Last active Feb 9, 2019
init Timber output format (with class, method, thread names and line number)
View init_timber.java
private void initTimber(){
if (BuildConfig.DEBUG) {
Timber.plant(new DebugTree() {
@SuppressLint("DefaultLocale")
@Override
protected String createStackElementTag(@NonNull StackTraceElement element) {
return String.format("%s: %s:%d (%s)",
super.createStackElementTag(element),
element.getMethodName(),
element.getLineNumber(),