How to Add GridView in Android using android studio

0 525

GridView in Android

Today I will describe you how you can add GridView in AndroidGridView is ViewGroup that shows items in a two-dimensional, scrollable grid (rows&columns). The grid items are inserted into the layout using ListAdapter. The adapter acts as a bridge between UI components and the data source that put data into UI component. It can be used to provide data to the spinner, list view, and grid view. The GridView is a subclass of an AdapterView.

GridView has attributes some of which are given below:

  • android:id
  • android:columnWidth
  • android:gravity
  • android:horizontalSpacing
  • android:numColumns

EXAMPLE

Now let me explain you to add GridView in Android with the help of an example.

Steps to Follow

  1. Create a New Android Studio Project and name it then Press Next.
  2. Set the minimum SDK as API 21 and Android 4.1(Jelly Bean). As lower API levels target more devices.
  3. Create a Blank Activity and name it as MainActivity and press Finish.
  4. Start the project. Write the following code inside res>layout>activity_main.xml, to include GridView content with the self explanatory attributes.

5.Inside the MainActivity.java, write the following code.

6. Inside the package, create new class and name it as Book_class.
7. Write the following code inside Book_class.java.

8. Create a New Class and name it as BooksAdapter. Write the following code inside BooksAdapter.java.

9. Inside layout>New>Layout resource file, create new file and name it as linearlayout_book.xml. Write the following code inside it.

10 Select res>values>strings.xml and write the following code inside it.

11 Inside the scripts folder, select build.gradle and write the following code inside it.

12.Now you can compile and run your code and you would get the output.

 

Download Source code

Comments

comments

Leave A Reply

Your email address will not be published.