Android activity Binary XML file line : You must supply a layout_width attribute-ThrowExceptions

Exception or error:

I’ve read about it already, but still it’s here.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".TaskEditActivity" >

So, the exception java.lang.RuntimeException: Binary XML file line #34: You must supply a layout_width attribute.

Attribute is there, schema too…
Solution?

How to solve:

Also i recommend check your dimens. You may have layout width in dimens, you go to it and you have value like “150” not “150dp” (missing dp) also cause this (that was my case)

###

I had a dimension for a given width but it wasn’t present in the default dimens.xml

res/
   values-w320dp/
          dimens.xml -> ring_radius=5dp
   values/
          dimens.xml -> missing ring_radius

###

It’s clear. Child elements missed layout_width attribute.

###

For me it was having an extra attribute on one of my view groups (scrollview):

xmlns:android="http://schemas.android.com/apk/res/android"

After that there was I forgot to change my casting of EditText to TextView:

Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText

###

Your problem is not in this layout element, its on the inner one, take a look at line number 34. There is is another user interface element that lacks the layout_width attribute.

###

For me, inside one of my layout.xml files, I had

<ImageView
    android:id="@+id/row_1_col_0"
    android:layout_width="@string/default_picture_size"
    android:layout_height="@string/default_picture_size"
    android:layout_weight="1"
    android:background="@drawable/tile"
    android:onClick="mClickMethod" >
</ImageView>

and inside strings.xml, I had
wrap_content

and so it was showing this in Android Studio:

<ImageView
    android:id="@+id/row_1_col_0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/tile"
    android:onClick="mClickMethod" >
</ImageView>

I thought everything would work because there were no errors and the application compiled and ran. However, there was a run time error that said I didn’t set the layout_width.

Once I changed layout_width and layout_height from:

android:layout_width="@string/default_picture_size"
android:layout_height="@string/default_picture_size"

to

android:layout_width="wrap_content"
android:layout_height="wrap_content"

everything worked.

###

just incase all the solutions don’t work
clean project -> run
cheers

###

This can happen if you apply or don’t apply the correct theme on a fragment/activity.

For example Leanback’s OnboardingSupportFragment requires Theme.Leanback.Onboarding, which you have to apply either on the activity in the manifest android:theme="@style/Theme.Leanback.Onboarding" or in the fragment overriding onProvideTheme()

@Override
public int onProvideTheme() {
    return R.style.Theme_Leanback_Onboarding;
}

Leave a Reply

Your email address will not be published. Required fields are marked *