I have a fragment that is suppose to populate a background with an Imageview that is created with Picasso.
public class Fragment1 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context c = getActivity().getApplicationContext();
View myFragmentView = inflater.inflate(R.layout.home, container, false);
TextView text = (TextView) myFragmentView.findViewById(R.id.text);
ImageView imageView = (ImageView) myFragmentView.findViewById(R.id.image);
String url = ModalScreens.byId(1).getBackgroundImagePath();
Picasso.with(c).load(url).fit().into(imageView);
return myFragmentView;
}
}
I can't figure out what I am doing wrong but the image wont load.
From your code it seems possible that you're loading a local resource and not from the web. If the former is the case, then you could just load it like
StackOverflow showing image View from local disk
If not and you're loading a web image, then this could be a variety of problems, here's some hopefully helpful troubleshooting
Have you added?
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Is the link a valid (as adavis as suggested)? Free of spaces from the beginning and end what not. I would do a
Log.wtf("Testing valid URL", "|"+url+"|")
Is the image too large? In that case it might return a null
Image too large Stackoverflow answer
Is the Imageview properly set to display? I would confirm by initially setting it to some image like this
android:src="#mipmap/large in your layout xml for your ImageView
Related
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;
}
i have try a lot of example or tutorial on tablayout, all work fine
(https://www.simplifiedcoding.net/android-tablayout-example-using-viewpager-fragments/, http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/)
but now i encounter 1 problem, which is i dont know how to set/change the TextView/ImageView while onCreate/onLoad
so far, what i able to do is, write all my code under onTabSelected, but this is not my solution because i cant show empty or static values on the 1st tab, then have to wait until click or slide again then only load the real data.
i'm sure it have a way or solution for my problem, can anyone share it to me (code/website)
or in order to have this Tab feature with viewpager contain recyclerview, Tablayout is not the right way to code, perhaps it have another more correct way to code.
Do these changes inside of onCreateView of fragment that you want
Example:
//Our class extending fragment
public class Tab1 extends Fragment {
//Overriden method onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Change R.layout.tab1 in you classes
View view = inflater.inflate(R.layout.tab1, container, false);
TextView textView = (TextView) view.findViewById(R.id.you_textview_id);
textView .setText("Your text to show");
//Returning the layout file after inflating
return view;
}
}
The TextView need to be previous declared in tab.xml that you're inflating, in our example, tab1.
I have a FragmentActivity in the first tab of a TabHost, and the FragmentActivity itself holds a ViewPager.
The ViewPager's setAdapter() method sets a FragmentPagerAdapter with a set of Fragments. The goal is to have swipeable images in the first pane of the TabHost.
To test it, I was loading a bunch of images that I had kept locally in the project's drawable directory. Everything worked beautifully.
After having tested this initial setup, I'm downloading a bunch of image URLs' off a REST web service. I want these images to load lazily in the ViewPager, and I tried calling Picasso's load() method in three places:
The onCreateView() method of the ViewPager's Fragments (the same place where I was earlier loading images from the local drawable directory).
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.layout_fragment, container, false);
ImageView imageView = (ImageView)getView().findViewById(R.id.fragment_image);
String url = getArguments().getString(IMAGE_RESOURCE_URL);
Context context = getActivity();
Picasso.with(context).load(url).fit().into(imageView);
return myFragmentView;
}
The onViewCreated() method of the ViewPager's Fragments.
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
Context context = getActivity();
String url = getArguments().getString(IMAGE_RESOURCE_URL);
ImageView imageView = (ImageView)view.findViewById(R.id.fragment_image);
Picasso.with(context).load(url).fit().into(imageView);
}
The onInstantiateItem() method of the FragmentPagerAdapter.
#Override
public Object instantiateItem(ViewGroup container, int position) {
View v = inflater.inflate(R.layout.layout_fragment, container, false);
Context context = Tab1Activity.this;
String url = getItem(position).getArguments().getString("IMAGE_RESOURCE_URL");
ImageView imageView = (ImageView)v.findViewById(R.id.fragment_image);
Picasso.with(context).load(url).fit().into(imageView);
return v;
}
None of these methods worked. I know that its not a problem with Picasso because the other day I tried using Picasso in a ListView and it worked like a charm. What am I doing wrong ?
Your first approach should work fine... if you implement it correctly. I would expect your code to crash with a NullPointerException.
Replace:
ImageView imageView = (ImageView)getView().findViewById(R.id.fragment_image);
with:
ImageView imageView = (ImageView)myFragmentView.findViewById(R.id.fragment_image);
Your lazy loader should work in the context of the
Fragment.createView()
where the Fragment is one of a collection being paged by the ViewPager and the FragmentpagerAdapter
That is your option one.
I use another lazyloader that manages local memcache and local filesys cache for the bitmaps needed to load the images. at the time of the "OnCreateView()" it will go to the network to get the URL for the loader if the bitmap is not already cached.
Seems like a simple question to me, but I can't figure it out. I have this code inside my Fragment:
myTextView.setText("Test");
However, this code brakes my app and I am unsure why. Perhaps this is a conflict with Views, but I still don't understand why it wont work. Any suggestions?
How did you set the content?
Edited my answer :) try this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.xmlName,
container, false);
TextView tv= (TextView) **view**.findViewById(R.id.textViewName);
tv.setText("yourText");
return view;
}
here is a tutorial for fragments : fragments tutorial
I need to implement a scroll view as shown:
That is, in "idle" state image "1" is visible in full size and image "2" is visible partially (thus giving a clue to the user that he can scroll the content). After scrolling scrool view must not stay in intermediate state and scrolling must be completed (like iOS's Scroll View does when "Paging Enabled" is turned on):
I refused to use HorizontalScrollView, because it has nothing similar to "Paging Enabled" property.
After googling, I came across android.support.v4.view.ViewPager. It's scrolling behavior is perfectly what I want, but I have no good idea how to support "partially visible" next image in ViewPager? Technically, what should I return in the
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)?
For the present, my code is
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.stage_select_image_layout, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.stage_select_image_layout_image);
imageView.setImageResource(m_imageResourceId);
return view;
}
But it results in "exactly one image per page" behavior, not what I want (see the very first figure).
This does the trick:
ViewPager pager = ...;
pager.setOffscreenPageLimit(2);
pager.setPageMargin(-200);
The easiest thing to do is, is in your PagerAdapter, implement an override for getPageWidth. The return value is a percent that the view takes up of the total space.
#Override
public float getPageWidth(int position) {
return 0.75f;
}
Use the widthFactor attribute of the ViewPager's LayoutParams. It should scale it so you can see a little of the next page.