Olumide Olumide - 4 months ago 12
Android Question

Extending GridLayout

With GridLayout this is a valid layout definition. There's no warning about

'layout_height' attribute should be defined
or
'layout_width' attribute should be defined


<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView />
</GridLayout>


On the otherhand, if I extend GridLayout the equivalent layout gives both warnings
'layout_height' attribute should be defined
and
'layout_width' attribute should be defined


<ExtendedGridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView />
</ExtendedGridLayout>


this is what the extended gridlayout looks like

package com.github.extendedgridlayout;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.GridLayout;

@SuppressWarnings("unused")
public class ExtendedGridLayout extends GridLayout {
public ExtendedGridLayout(Context context){
super(context);
}

public ExtendedGridLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public ExtendedGridLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ExtendedGridLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}


I tried looking through the
GridLayout
source, and it seems like what they did was to extend ViewGroup.LayoutParams and set a default width and height, just like for
PercentRelativeLayout


So it should seem that based off of inheritance, ExtendedGridLayout should also set a default width and height for it's children or do whatever it is that GridLayout does to avoid the warning message in the layout editor.

So my question is why does
ExtendedGridLayout
have the warning and how do I prevent it?

Answer

That is the default behavior of AndroidStudio.
One way to avoid that error is suppressing.

<ExtendedGridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!--suppress AndroidDomInspection -->
    <ImageView />

</ExtendedGridLayout>

AndroidStudio skips showing that error with GridLayout, But does not skip with the children of GridLayout. Here is the source code of inspector of AndroidStudio.

Here is related bug report.
By this bug report, your issue occurred like this.