Gridview Demo
1. Create a new project by going to File ⇒ New Android Project and fill required details. (I named my main activity as AndroidGridLayoutActivity.java)
2. Prepare your images which you want to show in grid layout and place them in res ⇒ drawable-hdpifolder.
3. Create a new XML layout under layout and name it as grid_layout.xml (Right Click) layout ⇒ New ⇒ Android XML File
1. Create a new project by going to File ⇒ New Android Project and fill required details. (I named my main activity as AndroidGridLayoutActivity.java)
2. Prepare your images which you want to show in grid layout and place them in res ⇒ drawable-hdpifolder.
3. Create a new XML layout under layout and name it as grid_layout.xml (Right Click) layout ⇒ New ⇒ Android XML File
<? xml version = "1.0" encoding = "utf-8" ?> < GridView xmlns:android = "http://schemas.android.com/apk/res/android" android:id = "@+id/grid_view" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:numColumns = "auto_fit" android:columnWidth = "90dp" android:horizontalSpacing = "10dp" android:verticalSpacing = "10dp" android:gravity = "center" android:stretchMode = "columnWidth" > </ GridView >
4. Create a new Class by right clicking on (Right Click) src ⇒ package folder ⇒ New ⇒ Class and name your class as ImageAdapter.java
6. Open your main activity class as shown below.
AndroidGridLayoutActivity.java
package com.example.nishal;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class AndroidGridLayoutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
}
}
|
VIEW MY TUTORIAL
ReplyDelete