How to use grid view in Android Studio.
We will set the data in the gridview using the adapter class.
Gridview is used when we want to show the name and picture grid layout.
Set the automatic item scrollable view in the gridview.
First of start android studio.
Type the name of your Project.
MainActivity.java
package com.pd.gridview; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.GridView; public class MainActivity extends AppCompatActivity { GridView gridView; int Images[]={R.drawable.ic_baseline_add_a_photo_24, R.drawable.ic_baseline_chat_24, R.drawable.ic_baseline_notifications_active_24, R.drawable.ic_baseline_perm_media_24, R.drawable.ic_baseline_settings_applications_24, R.drawable.ic_baseline_store_mall_directory_24, R.drawable.ic_baseline_store_mall_directory_24, R.drawable.ic_baseline_store_mall_directory_24}; String name[]={"one","two","three","four","five","six","seven","eight"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView=findViewById(R.id.gridview); MyAdapter myAdapter=new MyAdapter(MainActivity.this,Images,name); gridView.setAdapter( myAdapter); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <GridView android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2" android:columnWidth="100dp" android:id="@+id/gridview"/> </LinearLayout>
MyAdapter.java
package com.pd.gridview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyAdapter extends BaseAdapter { private Context context; private final int images[]; private final String name[]; private ImageView imageView; private TextView textView; private LayoutInflater layoutInflater; public MyAdapter(Context context, int[] images, String[] name) { this.context = context; this.images = images; this.name = name; } @Override public int getCount() { return name.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView=layoutInflater.inflate(R.layout.singledata,null); imageView=(ImageView)convertView.findViewById(R.id.imageview); textView=(TextView)convertView.findViewById(R.id.textview); imageView.setImageResource(images[position]); textView.setText(name[position]); return convertView; } }
singledata.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/teal_700" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/imageview" android:layout_width="100sp" android:layout_height="100sp" android:src="@drawable/ic_launcher_background" /> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textSize="25dp" /> </LinearLayout>
Download Source code