Skip to content

Instantly share code, notes, and snippets.

@shkcodes
shkcodes / LazyColumn.kt
Created July 25, 2021 15:46
ExoPlayer in LazyColumn
@Composable
fun TweetList(state: TweetListState) {
val context = LocalContext.current
val listState = rememberLazyListState()
val exoPlayer = remember {
SimpleExoPlayer.Builder(context).build().apply {
repeatMode = Player.REPEAT_MODE_ALL
}
}
@nightwolf738
nightwolf738 / focusAndShowKeyboardBySquareEditedByEgemenHamutcu.kt
Created June 3, 2021 23:38
My final solution to show Android keyboard thanks to Helios Alonso and Square team
fun View.focusAndShowKeyboard(tryAgain: Boolean = true) {
/**
* This is to be called when the window already has focus.
*/
fun View.showTheKeyboardNow() {
if (isFocused) {
post {
// We still post the call, just in case we are being notified of the windows focus
// but InputMethodManager didn't get properly setup yet.
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
@hansenji
hansenji / FlowLifecycleExt.kt
Created December 31, 2020 19:18
Collect flows tied to the Lifecycle
class LifecycleGroup(val lifecycleOwner: LifecycleOwner) {
/**
* Recommended for the majority of use cases
* The crucial difference from [collect] is that when the original flow emits a new value, [block] for previous
* value is cancelled.
**/
inline fun <T> Flow<T>.collectLatestWhenStarted(crossinline block: suspend (T) -> Unit): Job {
return lifecycleOwner.lifecycleScope.launchWhenStarted {
this@collectLatestWhenStarted.collectLatest {
block(it)
package com.agrawalsuneet.gist
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
object ShareTextToWhatsappContact {
@timtsj
timtsj / RoundBarChartRender.kt
Created October 5, 2020 04:19
mpandroidchart stacked bar chart radius
package com.exmaple
import android.graphics.*
import com.github.mikephil.charting.animation.ChartAnimator
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.github.mikephil.charting.renderer.BarChartRenderer
import com.github.mikephil.charting.utils.Utils
import com.github.mikephil.charting.utils.ViewPortHandler
@yusufceylan
yusufceylan / OpenCameraAndSavePhoto.kt
Last active February 6, 2023 03:18
Take photo from camera and save it to gallery
// Take photo from camera and save it to public gallery
// Before Q and After Q implementations
// Answer riginally taken from this SO answer
// https://stackoverflow.com/a/59482148/5695091
private val REQUEST_TAKE_PHOTO = 101
private val REQUEST_PICK_PHOTO = 102
private var photoURI : Uri? = null
@manuelvicnt
manuelvicnt / AnAndroidApp.kt
Last active January 1, 2023 17:05
Hilt and AssistedInject working together in Hilt v2.28-alpha times - ViewModel version
// IMPORTANT! READ THIS FIRST
// Assisted Injection doesn't work with @HiltViewModel or @ViewModelInject
// Read more about the issue here: https://github.com/google/dagger/issues/2287
//
//
// AssistedInject and Hilt working together in v2.28-alpha times
// Example of a ViewModel using AssistedInject injected in a Fragment by Hilt
// As AssistedInject isn't part of Dagger yet, we cannot use in
// conjuction with @ViewModelInject
@jhonsore
jhonsore / README.md
Last active November 9, 2022 03:07
[DEPRECATED] Upload an Image from camera or gallery in WebView.

This is an old way to Upload an Image from camera or gallery in WebView. It was made a long time ago and is not maintened anymore. Use it at your own risk.

@arcadefire
arcadefire / RecyclerViewExtension.kt
Last active April 20, 2023 10:14
Add addOnItemClickListener easily to a RecyclerView using Kotlin
import android.support.v7.widget.RecyclerView
import android.view.View
interface OnItemClickListener {
fun onItemClicked(position: Int, view: View)
}
fun RecyclerView.addOnItemClickListener(onClickListener: OnItemClickListener) {
this.addOnChildAttachStateChangeListener(object: RecyclerView.OnChildAttachStateChangeListener {
override fun onChildViewDetachedFromWindow(view: View?) {
@cesarferreira
cesarferreira / RxJava.md
Last active February 2, 2022 07:28
Party tricks with RxJava, RxAndroid & Retrolambda

View Click

Instead of the verbose setOnClickListener:

RxView.clicks(submitButton).subscribe(o -> log("submit button clicked!"));

Filter even numbers

Observable
    .just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)