kotlin

Android Q Kotlin - API 29: check if image exists

让人想犯罪 __ 提交于 2021-01-28 01:53:14
问题 This is the code I'm using to save image in Android Q: private var fileName = "" private fun downloadImage(){ val folderName = "Funny" fileName = "bla_" + dlImageURL.split("/").toTypedArray().last() // find out here if this image already exists val requestOptions = RequestOptions() .skipMemoryCache(true) .diskCacheStrategy(DiskCacheStrategy.NONE) val bitmap = Glide.with(this@DownloadImage) .asBitmap() .load(dlImageURL) .apply(requestOptions) .submit() .get() try { val values = ContentValues()

Android Kotlin - How to set Spinner selected item

a 夏天 提交于 2021-01-27 22:15:42
问题 I have a spinner populated with array of strings from XML: <Spinner android:id="@+id/spinnerUnits" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:entries="@array/units" app:layout_constraintStart_toEndOf="@+id/barrier3" app:layout_constraintTop_toBottomOf="@+id/labelGeneric" /> When user selects something, I save it to SharedPreferences. How do I set the value back when the app is opened next time? I have only the value saved,

Write permissions not working - scoped storage Android SDK 30 (aka Android 11)

北战南征 提交于 2021-01-27 20:32:12
问题 Anyone else finding scoped-storage near-impossible to get working? lol. I've been trying to understand how to allow the user to give my app write permissions to a text file outside of the app's folder. (Let's say allow a user to edit the text of a file in their Documents folder). I have the MANAGE_EXTERNAL_STORAGE permission all set up and can confirm that the app has the permission. But still every time I try val fileDescriptor = context.contentResolver.openFileDescriptor(uri, "rwt")?

Having coroutine wait for previous calls

帅比萌擦擦* 提交于 2021-01-27 20:16:03
问题 I still haven't fully grasped Kotlin coroutines yet. Basically I want a coroutine to wait for any previous calls to finish before executing. The following code seems to work. But is it doing what I think it's doing? private var saveJob: Job? = null fun save() { saveJob = someScope.launch { saveJob?.join() withContext(Dispatchers.IO) { // suspending database operation } } } As far as I can tell, the code is doing what I want. But is it? 回答1: Keep in mind that the launch ed code is concurrent

When do we use launch(SupervisorJob())?

荒凉一梦 提交于 2021-01-27 20:10:20
问题 I have seen tutorials passing SupervisorJob to CoroutineScope to avoid all coroutine jobs being cancelled if one of its child fails. In run3 , I thought passing SupervisorJob to launch can get the same result, but apparently it does not. It seems to allow the Coroutine to be reused if there is an exception (if you remove the SupervisorJob from launch , second run2 call won't run the coroutine job), but it dose not behave like supervisorScope , whose other children job can continue (in the

What is the reason behind “unresolved reference” when using kotlin for FacebookLogin?

只愿长相守 提交于 2021-01-27 20:00:27
问题 I keep getting a "unresolved reference: FacebookCallback" error when I am trying to implement the code attached in the picture. I am trying to set up facebook login as instructed in this link: https://developers.facebook.com/docs/facebook-login/android#addbutton I am new to Kotlin but I can't see what I'm doing wrong here. EDIT: Here are my gradle files: apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'com.neenbedankt.android-apt' android {

Firebase - GetTokenResult, getExpirationTimestamp() not found

梦想与她 提交于 2021-01-27 19:48:12
问题 I am trying below code to obtain token and its expiration time for a Firebase user: fun refreshToken(apiBlock: (() -> Unit)? = null) { val firebaseUser = FirebaseAuth.getInstance().currentUser if (firebaseUser != null) { firebaseUser.getIdToken(false) .addOnCompleteListener { task -> if (task.isSuccessful) { generatedToken = task.result.token val tokenExpirationTime = task.result.expirationTimestamp apiBlock?.invoke() } else { // Handle error -> task.getException(); } } } } However I am

Access Binding Adapters in multi module project

北战南征 提交于 2021-01-27 19:41:56
问题 I've got a multi module project where the app module contains my binding adapters and my feature module depends on my app module since it is a dynamic feature module. App (contains binding adapters) ----> Dynamic feature module (where the layout is present) I have databinding and kapt enabled in all the modules. I'm unable to build the app successfully. Android Studio gives me the following error, error: cannot find symbol import com.app.module1.databinding.FeatureItemBindingImpl; Layout file

Different background for a button's states in Kotlin

蓝咒 提交于 2021-01-27 19:40:54
问题 I have a button in my project that works like this: The button has 3 different designs for each state - disabled ( state_enabled="false" ), enabled, pressed. This button remains disabled if no file is selected, and has a particular design for it. Although, when file is selected, this button becomes enabled and switches to a different design. And the button's highlight color is possible to see every time when the button is enabled and pressed. What I have tried so far: <?xml version="1.0"

RecyclerView with various elements in the same line

时光怂恿深爱的人放手 提交于 2021-01-27 19:33:36
问题 I would like to make a RecyclerView having differents elements per line depending on a boolean in the adapter. In my app, you have some banknote and coins. I have to put all in a RecyclerView . First of all I want to show the banknote (2 per 2) and after the coins (4 per 4) like this : This is how I want the result look like : This is my Adapter class MoneyAdapter(private val money_list: ArrayList<Money>) : RecyclerView.Adapter<MoneyAdapter.ViewHolder>() { override fun onCreateViewHolder