ImageView won't initialized in view pager - android

I'm trying to create Intro Slider for users so they can better understand the concept of application, but i have problems in initializing image view inside view pager.
Here i'm setting layouts to view pager and setting parameters to it's layout:
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(layouts[position], container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
Picasso.with(WelcomeActivity.this)
.load(imageResources[position])
.into(imageView);
container.addView(view);
return view;
}
I have three layouts and three image views, but ImageView won't initialized and i can't set image resources to it. Images are not showing in image views and i'm getting no errors. What i'm doing wrong here?

Related

RemoveView Function On ViewPager Not Working

I'm trying to remove a view from a Container of type ViewGroup like below, and I cast it into a ViewPager, but I noticed that the removeView() function is not working, as at now, I'm not sure of what I'm doing wrong.
I need help.
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
ViewPager viewPager = (ViewPager)container;
View view = (View)object;
ViewPager.removeView(view); //removeView is not working
layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.custom_layout,null);
ImageView imageView = (ImageView) view.findViewById(R.id.myImageView);
imageView.setImageResource(images[position]);
ViewPager viewPager = (ViewPager)container;
ViewPager.addView(view);
return view;
Yeah. It is works as expected. Instead of doing above, you need to remove the item for the ViewPager adapter. Something like below.
// Delete a page at a `position`
public void deletePage(int position) {
// Remove the corresponding item in the data set
pageIndexes.remove(position);
// Notify the adapter that the data set is changed
notifyDataSetChanged();
}

how to put array of download URLs of images to viewpager

I made a view pager with an image view inside it, and I want to make something like an image slider using the view pager. I have an array of download URLs. how can i put those URLs to open the image to the image view. I tried searching for other tutorials but they all use bitmaps.
Use Global Array to store URLs and than display using GLide Like>>>
#Override
public Object instantiateItem(ViewGroup container, final int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);
ImageView imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);
final ProgressBar pbLoad = view.findViewById(R.id.pbLoad);
String image = images.get(position);
imageViewPreview.setTag(image);
Glide.with(getActivity()).load(image).thumbnail(0.5f).crossFade().diskCacheStrategy(DiskCacheStrategy.ALL).into(imageViewPreview);
container.addView(view);
return view;
}

Dynamically set image resource to a list-item

I'm creating a custom ListView with multiple elements - TextView and ImageView.
Here's a part of my getView method:
#override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.individual_items_in_options, parent, false);
}
// Find the option to start with.
Options currentOption = optionsOfAQuestion.get(position);
// option number
TextView optionNumber = (TextView) itemView.findViewById(R.id.option_number);
optionNumber.setText(currentOption.getOptionNumber());
// option content
TextView optionContent = (TextView) itemView.findViewById(R.id.option_content);
optionContent.setText(currentOption.getOptionContent());
ImageView imageView = (ImageView)findViewById(R.id.option_icon);
imageView.setImageResource(R.drawable.icon_wrong);
I'm getting NPE while setting the image resource, though I've defined it in my layout. Why is setting image giving me NPE, while setting the TextViews work completely fine? What's the point I'm missing here?
replace this
ImageView imageView = (ImageView)findViewById(R.id.option_icon);
by this
ImageView imageView = (ImageView)itemView.findViewById(R.id.option_icon);
this will help you
Please Update your code like below
ImageView imageView = (ImageView) itemView .findViewById(R.id.option_icon);
imageView.setImageResource(R.drawable.icon_wrong);

Add Zoom in/out to ListView of Images?

I have a fragment which contains a ListView and multiple images (7-10) are displaced each time fragment is launched.
I tried using different libraries for the zooming purpose but the ListView won't show up as then I will have to replace ImageView with library's defined ImageView
How can I add zoom in/out for those images?
EDIT: I want to add zoom in/out to the ListView that contain different number of images on each load, as my question states.
EDIT 2: This is getview method of my Custom Adapter. How do I attach PhotoViewAttacher in it?
public View getView(int position, View convertView, ViewGroup parent) {
View iv = convertView;
ViewHolder holder = null;
if (iv == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
iv = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) iv.findViewById(R.id.imagview);
iv.setTag(holder);
} else {
holder = (ViewHolder) iv.getTag();
}
Picasso.with(context)
.load((Integer) data.get(position))
.resize(999, 720)
.onlyScaleDown()
.centerInside()
.into(holder.image);
return iv;
}
static class ViewHolder {
ImageView image;
PhotoViewAttacher mAttacher;
}
}
This is how I made it work
After importing PhotoView
In layout file (if you use layout):
<uk.co.senab.photoview.PhotoView
android:id="#+id/your_photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
... />
And in adapter
PhotoView photoView = (PhotoView) findViewById(R.id.your_photo_view);
Picasso.with(context)
.load(file)
...
.into(photoView);
Thanks everyone for your time.
Instead of taking simple ImageView in BaseAdapter's getView list item add
TouchImageView

Android - add Layout views into ListView

the problems is, that I wan't to make ListView with elements which are containing image, description and two buttons. I'm making them in my own BaseAdapter extension, but fragment which is containing ListView is closing (wihtout errors in logcat..). I've found, that ListView is working well, when I'm not returning layout-type elements. So there is my sample with 'sample linear layout', which is not working.. Is there any possibility, to show layouts in ListView?
Here is my code:
Creating part:
lv = (ListView) getView().findViewById(R.id.main_wall_ambajes_lv);
AmbajAdapter aa = new AmbajAdapter(getActivity().getApplicationContext(), StaticData.ambajes);
lv.setAdapter(aa);
My getView method from adapter:
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
ImageView iv = new ImageView(getActivity());
iv.setImageBitmap(placeholderBitmap);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
return ll;
}
I don't know why you don't have any error however I don't think you proceed the correct way.
Usually you create the layout in the xml file of the layout folder and only inflate it in the getView(), for example as follow :
private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = mInflater.inflate(R.layout.your_custom_layout, parent, false);
}
//your code for setting the image or other things goes here
//for example if you have a textView
TextView textView = (TextView) view.findViewById(R.id.my_textview_id);
textView.setText("my custom text for this cell");
return (view);
}
and your_custom_layout is simply the xml file of your layout.
Note that for performance reason due to cell recycling I only inflate the view when it is null and I only read once the LayoutInflater context and put it in mInflater. However for the best performance you should use a ViewHolder, but it is out of the scope of your question.

Categories

Resources