Not able to display the clicked image - android

i have few images that i am displaying in gridview. what i want is when i click that image it should be displayed in full screen. i have also put viewpager to have sliding functionality. the problem that i am having is when i click on an image the first image is displayed(not that one).
he is my main activity
public class Ppdtsample extends Activity {
GridView grid;
static int[] imageId = { R.drawable.ppdt1, R.drawable.ppdt2,
R.drawable.ppdt3, R.drawable.ppdt4, R.drawable.ppdt5,
R.drawable.ppdt6, R.drawable.ppdt7, };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ppdtsamples);
ImageAdapter adapter = new ImageAdapter(Ppdtsample.this, imageId);
grid=(GridView)findViewById(R.id.grid_view);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
this is my activity to display in full screen with sliding functionality:
public class FullImageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
// passing array index
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setBackgroundResource(0);
SlideImage adapter = new SlideImage(this);
viewPager.setAdapter(adapter);
}
}
and this is my class for viewpager
public class SlideImage extends PagerAdapter {
Context context;
private int[] GalImages = { R.drawable.ppdt1, R.drawable.ppdt2,
R.drawable.ppdt3, R.drawable.ppdt4, R.drawable.ppdt5,
R.drawable.ppdt6, R.drawable.ppdt7, };
SlideImage(Context context) {
this.context = context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
it is displaying the first image and not the image i click because i am not able to pass the 'position' of the clicked image. it is taking the position from this method in SlideImage.class
Object instantiateItem(ViewGroup container, int position)
which is the first position. i want to pass the position from fullImageActivity.class to the position in this line
imageView.setImageResource(GalImages[position]);
which is on SlideshowActivity.
This may look complicated but i just want to pass the value of position from FullImageActivity.class to SlideImage.class. Please help me

Your Ppdtsample looks strange.Here I tried to modify your Ppdtsample class,which worked for me:
public class Ppdtsample extends Activity {
GridView grid;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
grid = (GridView) findViewById(R.id.grid_view);
// Instance of SlideImage Class
gridView.setAdapter(new SlideImage(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent i = new Intent(getApplicationContext(),FullImageActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
}
}
And the reason you can't pass the id is because you set the wrong adapter in Ppdtsample.It should be SlideImage in you code, not ImageAdapter.

Related

ImageView fullscreen in Activity

I have a GridView populated with images. When I click on a GridView item it should open a new Activity and show an image in full screen, but it doesn't happen.
I'm using the Glide library. I've tried to debug, but I didn't find the problem.
Any ideas?
Gallery.java
gridViewGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(AdvertisementGallery.this,FullScreenImage.class);
intent.putExtra("image",i);
startActivity(intent);
}
});
FullScreenImage.java
viewPager.clearFocus();
imageItems = Constants.items;
bitmap = getIntent().getExtras().getInt("image");
// ImagePageAdapter imagePageAdapter = new ImagePageAdapter(this,imageItems);
imagePageAdapter = new ImageAdapter(this,imageItems);
viewPager.setAdapter(imagePageAdapter);
viewPager.setCurrentItem(bitmap,false);
class ImageAdapter extends PagerAdapter {
public Context context;
public ArrayList data = new ArrayList();
public ImageAdapter(Context context, ArrayList data){
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return this.data.size();
}
#Override
public int getItemPosition(Object object) {
int position = data.indexOf(object);
return POSITION_NONE ;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View row = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.image_pager_item_list_layout, container, false);
// holder.imageTitle = (TextView) row.findViewById(R.id.text);
image = (TouchImageView) row.findViewById(R.id.imgDisplay);
row.setTag(image);
} else {
image = (ImageView) row.getTag();
}
ImageItem item = (ImageItem) data.get(position);
Glide.with(getApplicationContext())
.load(item.getImage())
.into(image);
container.addView(row);
return row;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((LinearLayout) object);
}
}
First of all, you are passing the position of the image. you need to pass the item. instead of position. you need to do following changes in the following way.
in Gallery.java
gridViewGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ImageItem item = (ImageItem) data.get(position)
Intent intent = new Intent(AdvertisementGallery.this,FullScreenImage.class);
//intent.putExtra("image",i);
intent.putExtra("imagePosition",i);
startActivity(intent);
}
});
Now, Retrieve your image in FullScreenImage.java by following way.
imageItems = Constants.items;
int position=getIntent().getExtras().getInt("imagePosition");
ImageItem item = imageItems.get(position);
bitmap = item.getImage();
To set in your ViewPager you can follow this code:
imagePageAdapter = new ImageAdapter(this,imageItems);
viewPager.setAdapter(imagePageAdapter);
viewPager.setCurrentItem(position, true);
And for the issue of
some time image display and some time is not display in viewpager.
Try to add following line .placeholder (new ColorDrawable (Color.WHITE)). so the code looks like :
Glide.with(getApplicationContext())
.load(item.getImage())
.placeholder (new ColorDrawable (Color.WHITE))
.into(image);
And, If you are getting issue only in release mode apk then try this solution
Check how to send an image from one activity to another activity via intent. In your code,what you are sending is not an image that is a position. How did you get the position in the bitmap? this is not the way. sorry for my english.
check this link
first, convert the image into bitmap then pass bitmap to other activity and receive it.

how to get image from gridview list when i click image, i'm using picasso library

i want when i click image from gridview, image send to new layout (Details Image)
this is MainActivity
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
i.putExtra("arg3", arg2);
startActivity(i);
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "okey", Toast.LENGTH_SHORT).show();
}
});
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
public static final String URL ="http://api.androidhive.info/json/movies/";
Context mContext;
int mThumbIds = 18;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds;
}
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(95, 95));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else {
imageView = (ImageView) convertView;
}
Picasso.with(this.mContext)
.load(URL + position +".jpg")
// .placeholder(R.drawable.loader).error(R.drawable.ic_launcher).fit()
.into(imageView);
// imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images }
in here i try get image from gridview, i try use intent, it's not work
Details Image
public class SingleViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
Intent i = getIntent();
int position = i.getExtras().getInt("arg3");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.getItemViewType(position));}}
can anyone help me
Two points:
Pass the URL of the image you selected from MainActivity to SingleViewActivity.
Use ImageLoader, and do not modify the width/heigth otherwise the memory cache will miss.
Others the cache will do everything for you.

how to make a swipe between images from a grid view?

what do i have is many tabs between fragments .. each fragment has grid view and each time we press an image in the grid view it should be displayed full screen in a new activity .. and i want to add swipe functionality in this full screen image so the user can swipe left and right between other images in the grid view ... but i keep see an error on the full screen activity and log gave NULL POINTER EXCEPTION !!! please help ..
first of all here is the code for one of the fragments class :
public class KitchenBlinds extends Fragment {
public Integer[] mThumbIds = {
R.drawable.kit_1, R.drawable.kit_2,
R.drawable.kit_3, R.drawable.kit_4,
R.drawable.kit_5, R.drawable.kit_6,
R.drawable.kit_7, R.drawable.kit_8,
R.drawable.kit_9, R.drawable.kit_10,
R.drawable.kit_11, R.drawable.kit_12,
R.drawable.kit_13, R.drawable.kit_14,
R.drawable.kit_15
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_kitchen_blinds, container, false);
GridView gridview = (GridView)view.findViewById(R.id.grid_view);
try{
// Instance of ImageAdapter Class
gridview.setAdapter(new ImageAdapter(getActivity(), mThumbIds));
} catch (OutOfMemoryError E) {
E.printStackTrace();
}
/**
* On Click event for Single Gridview Item
* */
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getActivity(), FullImageActivity2.class);
// passing array index
i.putExtra("id", mThumbIds[position]);
Log.d("ID", "" + mThumbIds[position]);
startActivity(i);
}
});
return view;
}
}
and then here is the code of the image adapter for the grid view class :
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {};
public ImageAdapter(Context c,Integer[] mThumbIds2){
mContext = c;
this.mThumbIds=mThumbIds2;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setBackgroundResource(R.layout.edit_border);
imageView.setPadding(3, 3, 3, 3);
return imageView;
}
}
and here is the code for the Image Pager Adapter :
public class ImagePagerAdapter extends PagerAdapter{
private List<ImageView> images;
public ImagePagerAdapter(List<ImageView> images) {
this.images = images;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = images.get(position);
container.addView(imageView);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
#Override
public int getCount() {
return images.size();
}
#Override
public boolean isViewFromObject(View view, Object o) {
// TODO Auto-generated method stub
return view == o;
}
}
and finally here is the code of the full screen activity that it crash on it each time i select the image :
public class FullImageActivity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image_activity2);
// Loop through the ids to create a list of full screen image views
ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
images.add(imageView);
}
// Finally create the adapter
ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(imagePagerAdapter);
// Set the ViewPager to point to the selected image from the previous activity
// Selected image id
int position = getIntent().getExtras().getInt("id");
viewPager.setCurrentItem(position);
}
}
and i keep see an error on this line :
ImageAdapter imageAdapter = new ImageAdapter(this);
because i configure my constructor in the image adapter to take two parameter which work fine , but i don't know how to pass it here !!!
please help !! and thx in advance ...
well .. i fix the problem by sending the array in the kitchen blinds activity like this :
i.putExtra("array", mThumbIds);
then i receive it like this and add it to the constructor :
Intent i = getIntent();
Object[] s = (Object[]) getIntent().getSerializableExtra("array");
Integer[] newArray = Arrays.copyOf(s, s.length, Integer[].class);
and add it to the constructor :
ImageAdapter imageAdapter = new ImageAdapter(this, newArray);

getting image id from ImageAdapter

I'am new to android and I've a project of making a grid view of images and onclick on image it shows it , thats the ImageAdapter class code
public class ImageAdapter extends BaseAdapter{
private static LayoutInflater inflater = null;
private Activity activity;
private String mode = "";
int[] images=null ;
public ImageAdapter(Activity act, String mode , int[] images){
inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
activity = act;
this.mode = mode;
this.images= images ;
}
public ImageView getImage(int pos)
{
ImageView im = (ImageView) getItem(pos);
return im ;
}
#Override
public int getCount() {
return images.length;
}
#Override
public Object getItem(int position) {
return new Integer(images[position]);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if(mode.equalsIgnoreCase("grid")){
if (view == null) {
view = inflater.inflate(R.layout.each_image1, null);
}
ImageView iv = (ImageView)view.findViewById(R.id.imageView);
iv.setImageResource(images[position]);
} else if(mode.equalsIgnoreCase("gallery")){
if (view == null) {
view = inflater.inflate(R.layout.each_image_gallery, null);
}
ImageView iv = (ImageView)view.findViewById(R.id.imageView);
iv.setImageResource(images[position]);
}
return view;
}
}
thats my grid activity
public class GridActivity extends Activity {
GridView grid = null;
public static ImageAdapter adapter1 ;
public static ImageAdapter adapter2 ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
adapter1= new ImageAdapter(GridActivity.this, "grid" , Images.images1);
adapter2 = new ImageAdapter(GridActivity.this, "grid" , Images.images2);
Intent i = this.getIntent();
if (i!=null)
{
String unique = i.getExtras().getString("Unique");
if (unique.equals("islam"))
{
Toast.makeText(GridActivity.this, "islam", Toast.LENGTH_LONG).show();
grid = (GridView)findViewById(R.id.gridView1);
grid.setAdapter(adapter1);
}
if (unique.equals("natural"))
{
Toast.makeText(GridActivity.this, "nat", Toast.LENGTH_LONG).show();
grid = (GridView)findViewById(R.id.gridView1);
grid.setAdapter(adapter2);
}
}
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long id) {
Intent i = new Intent(GridActivity.this, imgPrevActivity.class);
i.putExtra("selectedIntex", pos);
startActivity(i);
Toast.makeText(GridActivity.this,"ddd",Toast.LENGTH_LONG).show();
}
});
}
}
and this is the activity where it supposes to show any clicked on image
public class imgPrevActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.each_image1);
ImageView im = (ImageView)findViewById(R.id.imageView);
int pos = getIntent().getExtras().getInt("selectedIntex");
// ImageAdapter adapter = new ImageAdapter(imgPrevActivity.this, "image prev", null);
long Id= GridActivity.adapter1.getItemId(pos);
im.setImageResource((int) Id);
}
}
i've tried to get the position of image clicked on from the grid
then getting the Id from the position
and setting the image view to that ID
but it doesnt work !!
thats where the images are put in arrays
public class Images {
public static int[] images1 = {
R.drawable.buds, R.drawable.cherry_34,
R.drawable.clouds_2, R.drawable.coffee_beans_2,
R.drawable.death_valley_sand_dunes
};
public static int[] images2= {
R.drawable.morning_glory_pool,
R.drawable.pink_flowers, R.drawable.sun_flower,
R.drawable.sunrise_3, R.drawable.yellow_rose_3,
};
}
You have to pass your image resource id with intent. The list item id of adapter is useless because you have new Activity on the screen.
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long id) {
Intent i = new Intent(GridActivity.this, imgPrevActivity.class);
i.putExtra("selectedIntex", grid.getAdapter().getItem(pos);
startActivity(i);
Toast.makeText(GridActivity.this,"ddd",Toast.LENGTH_LONG).show();
}
});
and change your code in your ImgPreviewActivity:
public class imgPrevActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.each_image1);
ImageView im = (ImageView)findViewById(R.id.imageView);
int pos = getIntent().getIntExtra("selectedIntex", 0);
im.setImageResource(pos);
}
}
I think it should work

Focus issue in gridview

I have a gridview in my layout. I have added three imageviews to this gridview.
I have the following requirement. By default when I launch or resume the activity, I need to have an imageview selected by default. How can I accomplish that? Placing gridView.setSelection(1); in onResume() have no effect ie; the imageview does not get selected on launching or resume, I wonder why! Can someone please let me know why this does not get highlighted?
Following is my main activity and adapter class.
public class MyActivity extends Activity
{
GridView gridView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
// set up the onClick listener
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Log.d("Pos", "setOnItemClickListener() - pos:" + position);
handleItemClick(position);
}
});
}
public void onResume() {
super.onResume();
gridView.setSelection(1);
}
private void handleItemClick(int position) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
switch(position) {
case 0: // Phone
intent.setClassName("com.android.contacts",
"com.android.contacts.DialtactsActivity");
break;
case 1: // People
intent.setClassName("com.android.contacts",
"com.android.contacts.activities.PeopleActivity");
intent.setAction("com.android.contacts.action.LIST_CONTACTS");
break;
case 2: // Places
intent.setClassName("com.android.contacts",
"com.android.contacts.DialtactsActivity");
break;
default:
break;
}
startActivity(intent);
}
}
Adapter class
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in an array
public Integer[] mThumbIds = {
R.drawable.call,
R.drawable.contacts,
R.drawable.music,
};
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(350, 350));
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
imageView.setId(position);
return imageView;
}
}
You can try this:
public void onResume() {
super.onResume();
Thread.sleep(1);
gridview.requestFocusFromTouch();
gridview.setSelection(1);
}
or also this can help:
public void onResume() {
super.onResume();
mGridView.setSelection(1);
mGridView.requestFocusFromTouch();
mGridView.setSelection(1);
}
Just a guess may work
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
gridView.setSelection(1); <--- try to add this here

Categories

Resources