When running an instrumented Android test, or “androidTest”, and you encounter the following error: It means that you need to disable animations for it to successfully run the test. But, it can take a lot of clicks on the emulator/device screen to get to the developer settings to turn off animations! So, for a quicker […]
coding
Fix coroutine error: Cannot access database on the main thread // Android Dev Diary
As I was running a test for a ViewModel function, I encountered the following error: […] Main Thread @coroutine java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time. I think it may be caused by the viewModelScope from viewModelScope.launch {} being used by […]
Remove room database on app uninstall // Android Dev Diary
The problem: Installing on an Android device that has an old version of an app I’m developing triggers an error: Room Database Migration: java.lang.IllegalStateException: Migration didn’t properly handle Problem persists despite uninstalling the Android app and deleting all app data. The solution: Very simple. I set allowBackup to false in my AndroidManifest.xml file like so: […]
A working shared ViewModel between parent and child Fragments //Android Kotlin Dev Diary
I initially had a problem with the shared ViewModel because the child fragments were creating a new instance from the one held by the parent fragment. The ViewModel instantiations below work for me: CreateLogFragment.kt (parent)
1 2 3 4 |
class CreateLogFragment : Fragment() { private val mainViewModel: MainViewModel by activityViewModels { MainViewModel.Factory } . . . |
CreateLogFoodFragment.kt (child)
1 2 3 4 5 |
class CreateLogFoodFragment : Fragment() { private val mainViewModel: MainViewModel by activityViewModels( null, factoryProducer = { MainViewModel.Factory }) . . . |
CreateLogNotesFragment.kt (another child)
1 2 3 4 5 |
class CreateLogFoodFragment : Fragment() { private val mainViewModel: MainViewModel by activityViewModels( null, factoryProducer = { MainViewModel.Factory }) . . . |
by activityViewModels turned things around!
Two NavHosts with their own NavControllers and NavGraphs (Navigation Component) //Android Kotlin Dev Diary
I am trying to make a single-activity app because I’d like to easily share data across screen using, ideally one, or as few as possible ViewModels. NavGraph 1 Host: Layout file of MainActivity Destination(s): CreateLogFragment NavGraph 2 Host: Layout file of CreateLogFragment Destination(s): CreateLogFoodFragment, CreateLogNotesFragment (CreateLogFragment has a BottomNavigationBar that switches between CreateLogFoodFragment and CreateLogNotesFragment) […]
My Fave Android Studio Keyboard Shortcuts
I have been working on small Android apps, and I just want to share some of my favorite keyboard shortcuts. Some of them were manually added by me. Keyboard shortcuts are one of my must-haves for productivity as a programmer, as they let me navigate through code faster, allowing me to focus on creating new […]
Dao Error: Not sure how to handle query method’s return type //Android Kotlin Dev Diary
As I brush up on my Room database knowlege I came upon the following error while building my app:
1 |
WordDao.java:21: error: Not sure how to handle query method's return type (java.lang.Object). DELETE query methods must either return void or int (the number of deleted rows). |
The solution: Set Kotlin version to 1.6.10 then Room to 2.4.2 (Or use more recent compatible versions) in my top-level build.gradle file. Reference: Android Room Kotlin throws Delete Query error
Error: Type mismatch. Required: ActivityMainBinding. Found: ViewDataBinding! //Android Kotlin Dev Diary
The problem is that this line of code is producing an error: And the error says: Type mismatch.Required:ActivityMainBindingFound:ViewDataBinding! The solution is to add a <layout> tag in the layout file that you are inflating!
Expand ScrollView vertically if height is smaller than screen // Android Dev Diary
I have the following XML layout code in an Android app that I’ve been tinkering with: I wrapped my GridLayout in a ScrollView because on the landscape orientation of the Android app’s screen, my grid contents are cut from the view because they’re too tall, and there’s no way to see them without a scrollbar, […]
Sample Instrumented Test for Retained Text After Screen Rotation // Android Kotlin Dev Diary
First I added the following UI Automator to my app: So now my build.gradle (Module) file has the following dependecies: Then under the package app/src/androidTest/java/net/catzie/samplecalculatorapp I added the following instrumented test class: What it does is press the buttons 1, period, and 5 on the calculator’s UI to show “1.5” which should show up the […]