vinay
vinay

Reputation: 23

Select image from gridview and display it on other intent

I am creating Gallery. I have gridlayout which display all images. Now i want to do that when we select one image then another intent will start that i have done,but now i want to display that selected image in this new intent.

thanx in advance.

Upvotes: 2

Views: 6476

Answers (2)

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Please Use below code of gridview.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center" />

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

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

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(Constants.mThumbIds[position]);
        return imageView;
    }
}

Constants.java

public class Constants {
    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

MainActivity.java

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intentt mInDisplay=new Intent(MainActivity.this, DisplayActivity.class);
                mInDisplay.putExtra("Index", position);
                startActivity(mInDisplay);
            }
        });
    }
}

display.xml

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

    <ImageView
        android:id="@+id/mImgView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

DisplayActivity.java

public class DisplayActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bundle bdl=getIntent().getExtras();
        int index=bdl.getInt("Index");      
        ImageView mImage = (ImageView) findViewById(R.id.mImgView1);
        mImage.setImageResource(Constants.mThumbIds[index]);
    }
}

And Declare both activities into your android manifest file.

Upvotes: 2

Furzel
Furzel

Reputation: 616

You can set a click listener on every image via setOnClickListener, the listener can then send the intent to start the new activity.

We also want to send the image path with the intent. To achieve this you can use putExtra on your intent with a name you choose as first parameter and the image path as second parameter.

You should then be able to retrive the image path by using getStringExtra in your new activity. You will need to get the intent by calling getIntent() in your activity

Good luck

Upvotes: 0

Related Questions