Android Grid View

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.

In this tutorial, We’ll create a grid of image thumbnails. When an image  is clicked, a new screen  will display  full image. I am showing the Grid View Output in Emulator.

AndroidGridView

Full screen image

we are going to create a new Android Project in Eclipse. Click on File Menu and Select a new Android project named it as androidGridView . Now Follow the Following Procedure to show GridView in android.

1. Put Image in Drawable Folder   Inside your project Folder there is drawable folder. Put the image you want to show as Grid and when User click it the same image Open in Full screen.

2. Inside layout folder create two xml file  inside your project there is a folder named as layout folder. inside that folder create two new xml file named as full_image.xml and grid_layout.xml. now put   the following code inside grid_layout.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<GridView xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@+id/grid_view”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:layout_marginTop=”15dp”
android:numColumns=”auto_fit”
android:columnWidth=”100dp”
android:horizontalSpacing=”10dp”
android:verticalSpacing=”10dp”
android:gravity=”center”
android:stretchMode=”columnWidth” >

</GridView>

and following code inside full_image.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >

<ImageView android:id=”@+id/full_image_view”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:contentDescription=”@string/hello_world”/>

</LinearLayout>

3 .Now create following class inside src folder

Now right click on src folder and create following class.

ImageGridActivity, ImageAdapter and FullImageActivity

Now put the following code in ImageGridActivity
public class ImageGridActivity 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));

gridView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra(“id”, position);
startActivity(i);

}

});

Same way Now put Following code inside ImageAdapter class

public class ImageAdapter extends BaseAdapter{

private Context mContext;

// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9, R.drawable.pic_10,
R.drawable.pic_12,R.drawable.pic_14
};

// Constructor
public ImageAdapter(Context c){
mContext = c;
}

public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}

public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));

return imageView;
}

}

Finally in FullImageActivity class put the following code. 

public class FullImageActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);

// get intent data
Intent i = getIntent();

// Selected image id
int position = i.getExtras().getInt(“id”);
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView)findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);

}
}

4 Now Final Step Android Manifest.xml File

Finally your Android Manifestfile looks like following way..

<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.sks.kindle”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”15″ />

<application
android:icon=”@drawable/android”
android:label=”@string/app_name” >
<activity
android:name=”.ImageGridActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<!– FullImageActivity –>
<activity android:name=”.FullImageActivity”></activity>
</application>

</manifest>

Now You have done all coding part now run your project and Get your Grid View.

Note Put all 11  png image  in Drawable folder named like pic_1.png, 

Hello world! in Android

This is a tutorial for Android Beginner. I am going to describe step by step  to build your first Android Application.

We need following things to run our first application.

1. JDK1.6 or Jdk1.7

2. Eclipse

3. Android sdk

4. Android Plunging for Eclipse.

At first install JDK in your machine(computer). You can get jdk from Oracle site. You can download jdk version based on your machine OS.

Download latest Eclipse from eclipse.org based on your machine OS. Eclipse comes in Zip file download it and Uncompressed it in D or E drive of your machine.

When you uncompressed it you a get folder name Eclipse  inside that folder you get an icon of eclipse. Double click on it and you get a welcome screen. Close this screen and then go in File menu and create a new java project named it App1 or something like that . Inside that App1 you find src folder right click on it and create a class put package name and Class name.Inside this class you write same Hello world in java and run it. If it is working then half work down.

Now go on http://developer.android.com and download Android SDK depending on your machine OS and install it.

Now Go on Help menu of Eclipse and add plunging in it for Android. Follow the following step

click on help menu in that you get install new software click on that you get a pop-up window inside that window you get Add button click on that it again open a new pop-up window inside that window there are two field in first field put Any name and in Second field put this address  https://dl-ssl.google.com/android/eclipse/ after that click ok it download required ADT plugins for eclipse after installing eclipse asking for restart  restart it .

When Eclipse restarted you find two new icon below eclipse  menu section. One icon is SDK manager and other is AVD manager. Click on AVD manager and create a new Virtual device(mobile run time).

Now click on File menu in eclipse create a new android Project and named it HelloAndroid and follow the onscreen Simple procedure. When it finish you will get HelloAndroid folder inside that src folder open it you will get java class open it and run it by click run button or ctrl+f11. It automatic  lunch virtual device and after 5 to 6 min you get Hello world on Virtual device screen.

 Happy Coding.