DEV Community

loading...

Custom Error Screen Instead of Default Crash Dialog — Android

Mustufa Ansari
📱Mobile Application Developer 🎓Keen Learner 🎓Graduate 🎧Love Music 🎓 Tech Enthusiast!
Originally published at Medium ・2 min read

Alt Text

I don’t know about you but I feel very embarrassed when I deliver an app to tester or client for testing and it get crash. That moment is very painful for me.

I personally don’t like that crash alert dialog that appears when my app gets crash. So I found an interesting library that will show an error screen instead of an error dialog.

Let’s get started.

What I’m going to use:

After creating a new project add this dependency in your build.gradle(:app).

implementation 'cat.ereza:customactivityoncrash:2.3.0'

I am making my custom crash layout. Like this:

layout

I downloaded this error image from here.

Let’s do some coding part to catch the crash event and show custom screen instead of android default dialog.

Now you need to make an application class that will register in the android manifest to register crash library globally in-app.

Add this class in AndroidManifest.xml using name attribute.

AndroidManifest.xml

Now moving towards MainActivity.kt to see this in action. We need to crash our app to see the crash screen. I am implementing a crashing code in my onCreate method.

var array = mutableListOf<String>()
array[0] = "Hello"
findViewById<TextView>(R.id.textView).text = array[1]
Enter fullscreen mode Exit fullscreen mode

This will crash my app as this code will generate IndexOutOfBoundException.

Let’s run the app.

final app

Download Source code from here.

Discussion (0)