Array Adapter With Grid View
Array Adapter With Grid View
View
Output
What is Gridview?
• A GridView is a type of AdapterView that
displays items in a two-dimensional scrolling
grid.
• Items are inserted into this grid layout from
a database or from an array.
• The adapter is used for displaying this
data, setAdapter() method is used to join
the adapter with GridView.
• The main function of the adapter in GridView
is to fetch data from a database or array and
insert each piece of data in an appropriate
item that will be displayed in GridView.
XML Attributes of GridView
• android:numColumns: This attribute of GridView will be used
to decide the number of columns that are to be displayed in
Grid.
• android:horizontalSpacing: This attribute is used to define
the spacing between two columns of GridView.
• android:verticalSpacing: This attribute is used to specify the
spacing between two rows of GridView.
Step 1: Creating a New Project
• Create an Empty Project and select Java as a your
programming Language
<?xml version="1.0" encoding="utf-8"?>
Step 2: Modify <androidx.constraintlayout.widget.ConstraintLayout
activity_main.x xmlns:android="http://schemas.android.com/apk/res/android"
ml file xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
Add GridView
to activity_main <!-- android:numColumns=2 is the number of columns for
Grid View
.xml file. android:horizontalSpacing is the space between
horizontal
grid items.-->
<GridView
android:id="@+id/idGVcourses"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="6dp"
android:numColumns="2"
android:verticalSpacing="6dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
Step 3: Create an <!--XML implementation of Card Layout-->
XML layout file for <androidx.cardview.widget.CardView
GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_gravity="center"
Create an XML file for grid item to android:layout_margin="5dp"
be displayed in GridView. Click on app:cardCornerRadius="5dp"
the app > res > layout > Right- app:cardElevation="5dp">
Click > Layout Resource file and <LinearLayout
then name the file as card_item. android:layout_width="match_parent"
Below is the code for android:layout_height="wrap_content"
android:orientation="vertical">
the card_item.xml file.
Step 3:
Continuation of the <ImageView
XML file android:id="@+id/idIVcourse"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
Note: Please add a small android:src="@mipmap/ic_launcher" />
icon like image in the
imageview inside drawable <TextView
android:id="@+id/idTVCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAlignment="center" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Step 4: Create a Modal Class for storing
Data
• Modal Class is the JAVA Class that handles data to be added in
each GridView item of GridView. For Creating Modal Class.
• Now click on app > java > apps package name > Right-Click on
it.
• Then Click on New > Java Class.
• Name your Java Class file as CourseModel.
public class CourseModel {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coursesGV = findViewById(R.id.idGVcourses);
Step 6: Modify ArrayList<CourseModel> courseModelArrayList = new
the MainActivity.java file ArrayList<CourseModel>();
courseModelArrayList.add(new CourseModel("DSA",
R.drawable.ic_gfglogo));
courseModelArrayList.add(new CourseModel("JAVA",
Now in this R.drawable.ic_gfglogo));
file, we will courseModelArrayList.add(new CourseModel("C++",
perform all R.drawable.ic_gfglogo));
backend courseModelArrayList.add(new CourseModel("Python",
operations that R.drawable.ic_gfglogo));
will be adding courseModelArrayList.add(new CourseModel("Javascript",
data to R.drawable.ic_gfglogo));
GridView. courseModelArrayList.add(new CourseModel("DSA",
R.drawable.ic_gfglogo));