Posts

Showing posts with the label kotlin

Android WebView shouldInterceptRequest?

Android WebView shouldInterceptRequest? I need to intercept android requests. I passed an HTTPClient and implemented shouldInterceptRequest. The doc says If the return value is null, the WebView will continue to load the resource as usud But when I return the value of super or return null it breaks and won't load the webview? Any thoughts? class WebViewClientImpl(val webView: WebView): WebViewClient() { @RequiresApi(Build.VERSION_CODES.LOLLIPOP) override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? { retuen null } } By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Convert Array to List in Kotlin

Convert Array to List in Kotlin I try to do this with (same as java) val disabledNos = intArrayOf(1, 2, 3, 4) var integers = Arrays.asList(disabledNos) but this doesn't give me a list. Any ideas? 2 Answers 2 Kotlin support in the standard library this conversion. You can use directly disableNos.toList() or if you want to make it mutable: disableNos.toMutableList() yep got that... – Audi Oct 10 '17 at 9:14 Just because you used Arrays.asList(disabledNos) .toList() – crgarridos Oct 10 '17 at 9:15 Oops that was very simple: var integers = disabledNos.toList() This ...

Reading JSON or text file in resource folder

Reading JSON or text file in resource folder I am developing Kotlin app. I am trying to read text and JSON files which I stored in resource folder(res/raw/file.txt). Looking at the hierachy of app structure, I put absolute path(../../res/raw/file.txt) for file reader. Unfortunately, my app cannot find the file. Can you tell me how I can read files stored in res/raw? If I store file in wrong directory, can you tell me where to store and how to access them? Thanks 1 Answer 1 If you want to read a file from res/raw folder you can obtain InputStream by R.raw.yourfile id like this: res/raw InputStream R.raw.yourfile resources.openRawResource(R.raw.yourfile) Or you can open file by Uri: val uri = Uri.parse("android.resource://com.example.your_package/raw/yourfile") val file = File(uri.getPath()); Alternatevily you can put your files in assets/ folder and get InputStream like this: assets/ ...

taskSnapshot.downloadUrl.toString() dont work in Kotlin

taskSnapshot.downloadUrl.toString() dont work in Kotlin Code Img I'm trying to follow a tutorial but the part that teaches the value of DownloadURL = taskSnapshot.downloadUrl.toString() does not work anymore. I tried to substitute some options like : DownloadURL = taskSnapshot.downloadUrl.toString() val DownloadURL = taskSnapshot.storage.downloadUrl.toString() Or : val DownloadURL = ImageRef.downloadUrl.toString() But I only get print values such as : com.google.android.gms.tasks.zzu@8a94cv2 and not a valid url. Think we need a little bit more code. What is class is ImageRef since it seems like you don't follow common casing conventions and is probably a val ? – JRomero 2 days ago ImageRef val I inserted an image with a code sample and println. I think it has something to do with...

Which one should I use between it and this in kotlin when I use extensions class?

Which one should I use between it and this in kotlin when I use extensions class? ScreenDef is a class, I add a function setDevice for the class, which one is correct between Code A and Code B? why? I think that Code B is correct, right? Code C data class ScreenDef( val brightness: Int ): DeviceDef class ScreenHelper(val mContext: Context) { fun setScreen(aScreenDef: ScreenDef){ } } Code A fun ScreenDef.setDevice(mContext: Context) { ScreenHelper(mContext).setScreen(this) } Code B fun ScreenDef.setDevice(mContext: Context) { ScreenHelper(mContext).setScreen(it) } Code B cannot even compile? – Hong Duan 2 days ago Yes, Code B is wrong! – HelloCW 2 days ago ...