/*******************************************************************************************
Program
Name:
Write an android application using Grid view.
Author: sujith
Date:
8/10/2016
*******************************************************************************************/
AndroidManifest.xml
<?xml
version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sujithbaby.gridview2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".grid_items" />
<activity
android:name=".GridDetails"></activity>
</application>
</manifest>
Activity_main.xml
<?xml
version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sujithbaby.gridview2.MainActivity">
<GridView
android:numColumns="2"
android:id="@+id/id_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"></GridView>
</RelativeLayout>
MainActivity.java
package
com.example.sujithbaby.gridview2;
import
android.content.Intent;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.GridView;
import
android.widget.ListAdapter;
import
android.widget.ListView;
public
class MainActivity extends AppCompatActivity {
GridView
gridView;
ListAdapter
listAdapter;
String[]
film_name={"godfather","shawshank
redemtion","godfather 2","Forest gump","12
angry men"};
int[]
images={R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m5};
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView=(GridView)findViewById(R.id.id_grid);
listAdapter=
new grid_items(MainActivity.this,images);
gridView.setAdapter(listAdapter);
gridView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public
void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Intent intent= new
Intent(MainActivity.this,GridDetails.class);
intent.putExtra("value",images[position]);
intent.putExtra("value1",film_name[position]);
startActivity(intent);
}
});
}
}
Activity_grid_items.xml
<?xml
version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_grid_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sujithbaby.gridview2.grid_items">
<ImageView
android:id="@+id/img_id"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Grid_items.java
package
com.example.sujithbaby.gridview2;
import
android.app.Activity;
import
android.content.Context;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.ImageView;
public
class grid_items extends BaseAdapter {
private
int[] images;
private
Context c;
ImageView
imageView;
public
grid_items(Context c,int[] images) {
this.images
= images;
this.c
= c;
}
@Override
public
int getCount() {
return
images.length;
}
@Override
public
Object getItem(int position) {
return
position;
}
@Override
public
long getItemId(int position) {
return
position;
}
@Override
public
View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater
li=(LayoutInflater)c.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView=li.inflate(R.layout.activity_grid_items,null);
imageView=(ImageView)convertView.findViewById(R.id.img_id);
imageView.setImageResource(images[position]);
return
convertView;
}
}
activity_grid_details.xml
<?xml
version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_grid_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sujithbaby.gridview2.GridDetails">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/grid_details"
/>
<TextView
android:id="@+id/grid_details2"
android:layout_below="@+id/grid_details"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:gravity="center"
android:textSize="15dp"
android:textStyle="bold"
android:layout_height="wrap_content"
/>
</RelativeLayout>
package
com.example.sujithbaby.gridview2;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.widget.ImageView;
import
android.widget.TextView;
public
class GridDetails extends AppCompatActivity {
ImageView
imageView;
TextView
txtView;
int
img_loc;
String
txtdetails;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_details);
imageView=(ImageView)findViewById(R.id.grid_details);
txtView=(TextView)
findViewById(R.id.grid_details2);
img_loc=getIntent().getIntExtra("value",0);
txtdetails=getIntent().getStringExtra("value1");
imageView.setImageResource(img_loc);
txtView.setText(txtdetails);
}
}
OUTPUT
No comments:
Post a Comment