Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 324
Olumide Avatar asked Jul 13 '16 02:07

Olumide


People also ask

Is GridLayout deprecated?

GridLayout is also not deprecated.

How do I add components to GridLayout?

To add Components to a GridLayout You do not (can not) use the row and column to tell where to add the components -- add them in starting at the top left and going across the row first.

What is the difference between GridView and GridLayout?

GridView simply gives us a two-dimensional view to display the items on the screen, under ViewGroup. On the other hand, GridLayout is a layout manager that arranges the views in a grid.

What are parameters of GridLayout?

Parameters: rows - the rows, with the value zero meaning any number of rows. cols - the columns, with the value zero meaning any number of columns.

What is the GridLayout class?

The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle. For example, the following is an applet that lays out six buttons into three rows and two columns:

How to add widget in a GridLayout in Java?

A GridLayout class object presents with a grid of cells arranged in rows and columns. The class contains addWidget () method. Any widget can be added by specifying the number of rows and columns of the cell. Optionally, a spanning factor for row as well as column, if specified makes the widget wider or taller than one cell.

How do I run a gridlayoutdemo?

Click the Launch button to run GridLayoutDemo using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index. The complete code of this demo is in the GridLayoutDemo.java file. A GridLayout object places components in a grid of cells.

What is the difference between GridView and GridLayout in Android?

GridView is a view, whereas GridLayout is a layout that can hold various views in it. Using GridView, you can display the items that are coming from the Adapter. GridLayout allows you to place many views and elements inside the layout and then style them. It allows you to set the number of rows and columns for your grid.


1 Answers

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.

like image 144
nshmura Avatar answered Nov 09 '22 03:11

nshmura