`GridView` in Android is a view group that displays items in a two-dimensional,
scrollable grid. It is used to display static or dynamic data using adapters (such
as `ArrayAdapter`, `CursorAdapter`, etc.). Each item in the grid is automatically
arranged according to the screen size and the specified parameters.
### Key Attributes of `GridView`:
1. **`numColumns`**: Specifies the number of columns in the grid. It can be set to
a specific number or `auto_fit` to adjust columns automatically based on the screen
width and column width.
2. **`columnWidth`**: Defines the width of each column. This is particularly useful
when you set `numColumns` to `auto_fit`.
3. **`verticalSpacing`** and **`horizontalSpacing`**: These attributes control the
spacing between rows and columns, respectively.
4. **`gravity`**: Controls the alignment of all items in the grid view, similar to
layout alignment in other layout types.
5. **`stretchMode`**: This attribute determines how the columns should be stretched
to fill the available space. Possible values are `spacingWidth`, `columnWidth`,
`spacingWidthUniform`, and `none`.
6. **`scrollbars`**: Controls the visibility and behavior of the vertical and
horizontal scrollbars.
7. **`layout_height`** and **`layout_width`**: Standard layout parameters that
define the size of the `GridView`. These are typically set to `match_parent` or
`wrap_content`.
### Example Usage of `GridView`:
Here is a simple example that shows how to implement a `GridView` to display text
labels:
**XML Layout (activity_main.xml)**:
```xml
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="100dp"
android:verticalSpacing="20dp"
android:horizontalSpacing="20dp"
android:gravity="center"
android:stretchMode="columnWidth" />
```
**MainActivity.java**:
```java
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = findViewById(R.id.gridview);
String[] numbers = new String[] { "One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten" };
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, numbers);
gridView.setAdapter(adapter);
}
}
```
### Explanation:
- **XML Layout**: Defines a `GridView` with automatic fitting columns based on a
column width of 100dp and spacing of 20dp between items. The grid items will be
centered and columns will be stretched to match the specified column width.
- **MainActivity.java**: Sets up the `GridView` with an `ArrayAdapter` to display a
simple array of number names. The adapter uses a predefined simple list item
layout.
- **Adapter**: Connects the data source (`String[] numbers`) to the `GridView`,
specifying how each item in the data set should be converted into a view that can
be displayed in the grid.
This basic example shows how a `GridView` can be used to display data in a
structured grid format. GridView is versatile for displaying images, buttons, or
more complex views by using a custom adapter instead of a simple ArrayAdapter.