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

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);

Related

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.

Not able to display the clicked image

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.

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

how to show my url images in full screen?

i created one grid view application, it's working fine now showing all images in grid view. now i am trying to display selected image in full screen, but i am getting error. my grid view images are downloaded in url link. my url link are stored in array list. how to solve this error?
error class name is FullImageActivity.class
error: imageAdapter1.GridViewConfig cannot be resolved or is not a field
error line :
imageView.setImageResource(imageAdapter1.GridViewConfig.getResim_list().get(position));
this is my coding:
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");
ImageAdapter imageAdapter1 = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter1.GridViewConfig.getResim_list().get(position));
}
}
ImageAdapter.class
public class ImageAdapter extends BaseAdapter implements ListAdapter {
private Context context;
public ImageAdapter(Context context) {
super();
this.context = context;
//Listeye image url si ekliyor
GridViewConfig.addImageUrls();
}
#Override
public int getCount() {
return GridViewConfig.getResim_list().size();
}
#Override
public Object getItem(int position) {
return GridViewConfig.getResim_list().get(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(context.getApplicationContext());
//imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+ id));
imageView.setLayoutParams(new GridView.LayoutParams(100,100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
}else{
imageView=(ImageView)convertView;
}
imageView.setImageDrawable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
return imageView;
}
//Internetten imageleri stream olarak cekip drawable olsurturuyor.
private Drawable LoadImageFromURL(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
}catch (Exception e) {
System.out.println(e);
return null;
}
}
}
GridViewconfig.class
public class GridViewConfig {
private static ArrayList<String> resim_list=new ArrayList<String>();
public static int[] getResim_list;
public static ArrayList<String> getResim_list() {
return resim_list;
}
public static void setResim_list(ArrayList<String> resim_list) {
GridViewConfig.resim_list = resim_list;
}
public static void addImageUrls(){
resim_list.add("http://igen.com/Images/Home/Client-Logo.png");
}
}
MyGridView.class
public class MyGridView extends Activity {
private GridView girGridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
girGridView=(GridView) findViewById(R.id.gridView1_bir);
//ListView gibi buna da adapter set ediliyor.
girGridView.setAdapter(new ImageAdapter(getApplicationContext()));
girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
I posted my full source code.....
First of all: GridViewConfig indeed is not a field of your ImageAdapter.
Second:
imageView.setImageResource(imageAdapter1.GridViewConfig.getResim_list().get(position));
it will not work: getResim_list() returns ArrayList, it contains String values NOT int
You can try change :
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");
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
GridViewConfig.addImageUrls();
imageView.setImageDravable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
}
private Drawable LoadImageFromURL(String url) {
....
}
}
But in my opinion you should rewrite all your code. The main problem: the drawings must be loaded in a separate thread.

how to display video thumbnails?

i am trying to show my arraylist value in my tablayout. now i am successfully passed two values in my two tabhost it's working. i have three tabhost 2tab host working fine. so i am trying show my stored path value video thumb. how to show my stored path value to video thumb nail? i am trying to show but i am getting error
error is:
The type of the expression must be an array type but it resolved to ArrayList<String>
line:
imgVw.setImageBitmap(getImage(tabview.videoList[position]));
full source code:
public class video extends Activity {
//set constants for MediaStore to query, and show videos
//flag for which one is used for images selection
private Gallery _gallery;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mavideo);
//set GridView for gallery
_gallery = (Gallery) findViewById(R.id.videoGrdVw);
//set gallery adapter
setGalleryAdapter();
}
private void setGalleryAdapter() {
_gallery.setAdapter(new VideoGalleryAdapter(this));
}
//
private class VideoGalleryAdapter extends BaseAdapter
{private Context mContext;
public VideoGalleryAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return tabview.videoList.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imgVw= new ImageView(mContext);;
try
{
if(convertView!=null)
{
imgVw= (ImageView) convertView;
}
imgVw.setImageBitmap(getImage(tabview.videoList[position]));
imgVw.setLayoutParams(new Gallery.LayoutParams(96, 96));
imgVw.setPadding(8, 8, 8, 8);
}
catch(Exception ex)
{
System.out.println("StartActivity:getView()-135: ex " + ex.getClass() +", "+ ex.getMessage());
}
return imgVw;
}
// Create the thumbnail on the fly
private Bitmap getImage(int id) {
Bitmap thumb = MediaStore.Video.Thumbnails.getThumbnail(
getContentResolver(),
id, MediaStore.Video.Thumbnails.MICRO_KIND, null);
return thumb;
}
}
}
note:
parxmlactivity.java this is my main class here i am passing my value using below code:
sdcardImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Intent intent = new Intent(ParxmlActivity.this, tabview.class);
intent.putExtra("spec",model_List.get(position).spec);
intent.putStringArrayListExtra("imageList", model_List.get(position).imageList);
intent.putStringArrayListExtra("videoList", model_List.get(position).videoList);
startActivity(intent);
}
});
and i am getting this passed value in below code in tabview.java class file:
tab_intent=tabview.this.getIntent().getExtras();
spec=tab_intent.getString("spec");
imageList = tab_intent.getStringArrayList("imageList");
videoList = tab_intent.getStringArrayList("videoList");

Categories

Resources