CEO tech4lifeapps
CEO tech4lifeapps

Reputation: 895

Inflating ConstraintLayout crashes on some devices

I have an xml-layout that works on some devices but crashes when inflated in others (xml-code only partly shown):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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:id="@+id/constraintLayout_record_audio"
android:minWidth="@android:dimen/dialog_min_width_major"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

Inflating the layout works fine on some devices but causes an InflateException on others.

I found the culprit in my xml-file:

<!-- Causes InflateException on some devices: 
     android:minWidth="@android:dimen/dialog_min_width_major"  -->

Unfortunately, after removing the line that causes the InflateException, android:layout_height="wrap_content" does not work anymore. The layout_height always gets rendered as if it was set to "match_parent". Any ideas about what's going on and how to solve this?

ADDED FOR CLARITY

Here's how I call the Dialog:

fun showRecordAudioDialog(view: View, categoryId: String, detailId: String) {
    val dialog = RecordAudioDialogFragment.newInstance(categoryId, detailId)
    dialog.show([email protected], "RecordAudioDialog")
}

In my RecordAudioDialogFragment:

    // Use the Builder class for convenient dialog construction
    val builder = AlertDialog.Builder(activity, style.CustomTheme_Dialog)

    val inflater = activity.layoutInflater
    val rootView = inflater.inflate(layout.dialog_record_audio, null)

Upvotes: 4

Views: 883

Answers (1)

CEO tech4lifeapps
CEO tech4lifeapps

Reputation: 895

For some strange reason android:layout_height does not work if android:minWidth is not set.

So, in dimens.xml we need to add a line like <dimen name="dialog_min_with">500dp</dimen>

and then add android:minWidth="@dimen/dialog_min_with" to our mylayout.xml file.

This is how the head of the layout.xml file looks now:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraintLayout_record_audio"
android:minWidth="@dimen/dialog_min_with"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

Upvotes: 2

Related Questions