DEV Community

Mustufa Ansari
Mustufa Ansari

Posted on • Originally published at Medium

3

Custom Error Screen Instead of Default Crash Dialog — Android

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.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/errorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:fontFamily="sans-serif"
android:text="@string/ouch_something_went_wrong"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_error_img" />
<com.google.android.material.button.MaterialButton
android:id="@+id/restartApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:text="Restart App"
android:textAllCaps="false" />
</RelativeLayout>

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

package com.example.customerrorscreen
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import kotlinx.android.synthetic.main.activity_error.*
class ErrorActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_error)
/** you can log the reason of crash by adding this line of code
* CustomActivityOnCrash.getStackTraceFromIntent(intent)
*/
//getting crashing intent
val config = CustomActivityOnCrash.getConfigFromIntent(intent)
/**
* If config is null or not getting an intent simply finish the app
*/
if (config == null) {
finish()
return
}
if (config.isShowRestartButton && config.restartActivityClass != null) {
restartApp.text = "Restart App"
restartApp.setOnClickListener {
CustomActivityOnCrash.restartApplication(
this,
config
)
}
} else {
restartApp.text = "Close App"
restartApp.setOnClickListener {
CustomActivityOnCrash.closeApplication(
this,
config
)
}
}
}
}

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

package com.example.customerrorscreen
import android.app.Application
import cat.ereza.customactivityoncrash.config.CaocConfig
/**
* Created by Mustufa on 04/11/2020.
* Email : mustufaayub82@gmail.com
*/
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
CaocConfig.Builder.create()
.trackActivities(true)
.errorActivity(ErrorActivity::class.java)
.apply()
}
}
view raw MyApp.kt hosted with ❤ by GitHub

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.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

AI Agent image

How to Build an AI Agent with Semantic Kernel (and More!)

Join Developer Advocate Luce Carter for a hands-on tutorial on building an AI-powered dinner recommendation agent. Discover how to integrate Microsoft Semantic Kernel, MongoDB Atlas, C#, and OpenAI for ingredient checks and smart restaurant suggestions.

Watch the video 📺

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay