Sandeep Mishra Sandeep Mishra - 3 months ago 13
Android Question

How to set custom Dialog Box when my App is crashed

I'm using

ACRA lib 4.9.0
in my app. I just want to show dialog box to the user when my app is crashed.

I am trying to do this.

public class App extends MultiDexApplication {

@Override
public void onCreate() {
ConfigurationBuilder config = new ConfigurationBuilder(this);
config.setMailTo("mailto@gmail.com");
config.setResDialogIcon(android.R.drawable.ic_dialog_info);
config.setResDialogText(R.string.crash_dialog_text);
config.setResDialogTitle(R.string.crash_dialog_title);
config.setResDialogCommentPrompt(R.string.crash_dialog_comment_prompt);
config.setResDialogOkToast(R.string.crash_dialog_ok_toast);
config.setMode(ReportingInteractionMode.DIALOG);

try{
config.setMode(ReportingInteractionMode.DIALOG);
} catch (Exception e) {
e.printStackTrace();
return;
}

ACRA.init(this, config);
super.onCreate();
Instance = this;
applicationHandler = new Handler(getInstance().getMainLooper());
}

Answer

Fully working demo

Application class

@ReportsCrashes(formUri = "http://yourserver.com/yourscript",
        mode = ReportingInteractionMode.DIALOG,
        resToastText = R.string.app_name,
        resDialogText = R.string.app_name,
        resDialogIcon = android.R.drawable.ic_dialog_info,
        resDialogTitle = R.string.app_name,
        resDialogCommentPrompt = R.string.app_name,
        resDialogEmailPrompt = R.string.app_name,
        resDialogOkToast = R.string.app_name,
        resDialogTheme = R.style.AppTheme_Dialog)

public class App extends Application {

    private Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        ACRA.init(this);
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {
    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        EditText number = (EditText) findViewById(R.id.foodName);
        number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a = 0 / 0;
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:background="@color/white"
    android:orientation="horizontal"
    android:weightSum="10">


    <EditText
        android:id="@+id/foodName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="64dp"
        android:layout_marginRight="64dp"
        android:hint="Food name"
        android:inputType="textCapWords"
        android:textColor="@color/colorPrimaryDark"
        android:textColorHint="@color/colorPrimaryDark"
        android:textSize="32sp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Dialog:

enter image description here