postheadericon GridView Example in Android





MainActivity.java

package com.example.gridviewdemo;

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 MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gv=(GridView) findViewById(R.id.gridView1);


ImageAdapter myImageAdapter= new ImageAdapter(this);
gv.setAdapter(myImageAdapter);

gv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

                Intent i = new Intent(getApplicationContext(), SelectedImage.class);
                i.putExtra("position",arg2);
                startActivity(i);

}
});

}
}


ImageAdapter.java

package com.example.gridviewdemo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

Context myContext;
Integer myImgArray[] = { R.drawable.img_1, R.drawable.img_2,
R.drawable.img_3, R.drawable.img_4, R.drawable.img_5,
R.drawable.img_6, R.drawable.img_7, R.drawable.img_8,
R.drawable.img_9, R.drawable.img_10, };

ImageAdapter(Context c) {
myContext = c;
}

@Override
public int getCount() {
return myImgArray.length;
}

@Override
public Object getItem(int arg0) {
return myImgArray[arg0];
}

@Override
public long getItemId(int arg0) {
return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView myImageView = new ImageView(myContext);
myImageView.setImageResource(myImgArray[arg0]);
myImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
myImageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return myImageView;
}

}


SelectedImage.java

package com.example.gridviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class SelectedImage extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selected_image);

        Intent i = getIntent();

        int position = i.getExtras().getInt("position");
        ImageAdapter imageAdapter = new ImageAdapter(this);

        ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setImageResource(imageAdapter.myImgArray[position]);
     
        final Button b=(Button) findViewById(R.id.btnBack);
        b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
}
}


selected_image.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp"
        android:src="@drawable/img_2" />

    <Button
        android:id="@+id/btnBack"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="Back" />

</RelativeLayout>

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=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="108dp"
        android:layout_marginTop="14dp"
        android:text="www.BipinRupadiya.com" />

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="29dp"
        android:gravity="center"
        android:horizontalSpacing="8dp"
        android:verticalSpacing="8dp"
        android:numColumns="5"
        android:columnWidth="80dp"
        android:stretchMode="columnWidth" >
    </GridView>

</RelativeLayout>


AndroidMaifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gridviewdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gridviewdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="SelectedImage"
            android:theme="@android:style/Theme.Dialog"></activity>
    </application>

</manifest>





0 comments:

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.