Skip to content

Instantly share code, notes, and snippets.

View NielsMasdorp's full-sized avatar
🏠
Working from home

Niels Masdorp NielsMasdorp

🏠
Working from home
View GitHub Profile
@NielsMasdorp
NielsMasdorp / DragLinearLayout.java
Created July 14, 2017 15:18
Linear layout with vertical orientation that adds draggable reordering to its children
package com.themobilecompany.Onderweg.generic.view.draggable;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
@NielsMasdorp
NielsMasdorp / sqlite-android-emulator.txt
Last active April 13, 2018 07:43
How to connect to SQlite database on Android emulator
adb devices
sudo adb -s emulator-xxx shell
run-as {package-name}
cd databases/
ls
sqlite3 {database name}
.databses -> view databases
.tables -> view tables
.schema -> view schema of table

Use FragmentTransaction#setPrimaryNavigationFragment(fragment) to set a ChildFragmentManager as deleage for the Activitys FragmentManager. This makes sure the back-button gets delegated to the ChildFragmentManager and allows you to keep a back-stack per tab without having to override OnBackPressed() everywhere.

@NielsMasdorp
NielsMasdorp / rxjavawrap.md
Last active June 1, 2018 07:47
How to wrap an existing async API in a reactive stream
return Observable.create { emitter ->
      foo.fetchBar(object : FooCallback<Bar> {
          override fun onSuccess(bar: Bar) {
               emitter.onNext(bar)
          }

          override fun onError(error: Throwable) {
               emitter.onError(error)
 }
@NielsMasdorp
NielsMasdorp / focusedittext.md
Last active June 8, 2018 14:25
How to request focus on EditText and show keyboard and also hide
fun EditText.showSoftKeyboard() {
    requestFocus()
    (context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager)?.showSoftInput(this, 0)
}

fun EditText.hideSoftKeyboard() {
    (context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager)?.hideSoftInputFromWindow(windowToken, 0)
}
@NielsMasdorp
NielsMasdorp / edittextalertdialog.md
Created June 26, 2018 05:53
Show an AlertDialog with EditText that is properly tinted
private fun show() {
    val input = AppCompatEditText(context).apply {
        setHint(R.string.parking_product_add_promo_code_hint)
        ViewCompat.setBackgroundTintList(this, ColorStateList.valueOf(ContextCompat.getColor(context, R.color.orange)))
    }
    AlertDialog.Builder(ContextThemeWrapper(context, R.style.DFWTheme_Parking))
        .setTitle("Title")
        .setEditText(input)
        .setPositiveButton("Button", null)
@NielsMasdorp
NielsMasdorp / highorderviews.md
Created June 27, 2018 09:47
Example of using Kotlin higher order functions to create views in a ViewPager
class FooFragment : DialogFragment() {

    private val viewPagerAdapter by lazy { BarViewPager() }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_foo, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@NielsMasdorp
NielsMasdorp / touchdelegate.md
Created September 11, 2018 15:40
Increase touchable area of view without increasing its size
private fun changeTouchableAreaOfView(view: View, extraSpace: Int) {
    val parent = view.parent as View
    parent.post {
        val touchableArea = Rect()
        button.getHitRect(touchableArea)
        touchableArea.top -= extraSpace
        touchableArea.bottom += extraSpace
        touchableArea.left -= extraSpace
 touchableArea.right += extraSpace
@NielsMasdorp
NielsMasdorp / delayedaction.md
Created September 26, 2018 13:36
RxJava2: Have an action (like a loading spinner) be invoked when an observable takes a long time and not instantaneously
/**
 * Invoke some action when the observable does not emit an item for longer than a given time out
 * @param timeout time out
 * @param timeUnit the unit for the time out
 * @param scheduler the scheduler on which the action must be ran
 * @param action the action to invoke
 */
fun <T> Observable<T>.startAfterDelay(timeout: Long, timeUnit: TimeUnit, scheduler: Scheduler, action: () -> Unit): Observable<T> {
 return this.publish { o -&gt;
@NielsMasdorp
NielsMasdorp / layoutcontainer.md
Created October 15, 2018 13:32
Use Kotlin Android extensions to access views inside `RecyclerView` `ViewHolder`s
class SomeViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer {

    fun bind(data: Data) {
        myFirstTextView.text = data.foo
    }
}