For the past year or so, this issue had been a constant companion of mine. The reason for this crash is

“From Android 9 Pie if your service does not call startForeground within 5 seconds after it has been started with the command startForegroundService ... then it produces an ANR + Crash.”

No matter what I did including

  1. Adding a startForeground() command, with notification, right at the beginning of the onStartCommand method of foreground service.
  2. Adding startForeground() command, with notification in the onCreate method.

It didn’t work. Also, the issue couldn’t be reproduced during testing which makes it all the…

A cool animation for debit/credit cards

On a lazy Saturday, while browsing Medium, I came across an article explaining how to organize credit/debit cards in your digital wallet app. I didn’t read the article but decided to make some of the animations myself (You may find a similar article with same animation. However no code has been copied from the previous article). Below are the animations we will be trying to achieve in this article

Searchable RecyclerView

To make a searchable RecyclerView we need a RecyclerView (duh) and a SearchView which will accept the user query.

Lets design the layout first. The xml would look like this

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""

app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView …

Using Room with Hilt

Hilt is the new dependency Injection tool in town. It supports injection in android classes out of the box. In this article we will see how to use Room with Hilt.
Let’s talk a bit about what we are doing then we can get around to the how.

We have a Fragment (MyChannelsFragment) that has a view model (MyChannelsViewModel). The fragment shows data present in view model.
The view model gets the data from a repository (MyChannelRepository). The repository needs to access data from local storage. In this article we will inject a DAO object in our repository that allows…

Inject ViewModel using Hilt

Dependency Injection is the first step you have to take towards building an app that can be tested. So today we will use Hilt to introduce view model injection into a Fragment.
If you wish to carry along with this article you can checkout the starting code here

Setting up Hilt

First of all we need to add dependencies.

Add the hilt-android-gradle-plugin plugin to your project's root build.gradle file:

buildscript {
dependencies {
classpath ''

Then, apply the Gradle plugin and add these dependencies in your app/build.gradle file:

apply plugin: 'kotlin-kapt'
apply plugin: ''

dependencies {
kapt ""…

Getting started with Android testing — Part 2

In the previous article, we discussed how to set up instrumented tests for our Android app. In this tutorial we will add some new functionality to our app and write unit tests to test that behavior.

You can check out the previous post here

You can check out the beginning code for this article here

In our app we have a simple TextView which shows a string present in app reources.
We will now add a new TextView that will show the number of words shown in the above mentioned simple TextView.

Lets add another TextView to our MainActivity’s layout…

Getting Started with Android testing

If you are a new developer you must have heard about this dragon called testing that needs slaying. If you have some experience you probably know why it’s so important to add tests to your project as soon as possible. So without getting into why it’s important, let's get straight into the how. In this article, you will be introduced to the basics of writing tests for an Android app that we will develop.

We will start an empty Android project. If you wish to follow along you can check out the code for empty Android project here.

First we…

Saurabh Vashisht

Lazy programmer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store