GridView In Android
In this tutorial we will learn how to implement GridView in Android Studio with simple example.
GridView:
GridView is a viewgroup that displays items in a two dimensional scrollable grid. The grid items are automatically inserted to the layout using Adapter.
- Create a new project and name it as Gridview.
- In the activity_main.xml define the GridView and it components.
File: activity_main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hss_24.gridview.MainActivity">
<GridView android:id="@+id/grid_view" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2"> </GridView> </RelativeLayout>
- Create a new layout and name it as grid_cell.
- Define ImageView and TextView in the layout to display images and texts.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <ImageView android:id="@+id/image" android:layout_width="180dp" android:layout_height="180dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dddd" android:textColor="#303F9F" android:textStyle="bold" /> </LinearLayout></LinearLayout>
- Do the following code in the java file MainActivity.java.
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends AppCompatActivity { String [] names = {"Cupcake","Donut","Eclair","Froyo","Ginger Bread","Honeycomb", "IceCream Sandwich","Jelly Bean","Kitkat","Lollipop","MarshMallow"}; int [] images = {R.drawable.cupcake,R.drawable.donut,R.drawable.eclair,R.drawable.froyo,R.drawable.gingerbread, R.drawable.honeycomb,R.drawable.icecream_sandwich,R.drawable.jellybean,R.drawable.kitkat, R.drawable.lollipop,R.drawable.marsh_mallow}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView gridView = (GridView)findViewById(R.id.grid_view); gridView.setAdapter(new MyAdapter()); } public class MyAdapter extends BaseAdapter { public MyAdapter() { super(); } @Override public int getCount() { return names.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 inflater = LayoutInflater.from(MainActivity.this); convertView = inflater.inflate(R.layout.grid_cell,null); ImageView imageView = (ImageView)convertView.findViewById(R.id.image); TextView textView = (TextView)convertView.findViewById(R.id.text); imageView.setImageResource(images[position]); textView.setText(names[position]); return convertView; } } }
- Run the Application.
- Now we have learned successfully the GridView in Android.
Comments
Post a Comment