I'm having a weird problem and I can't figure out why my ViewPager won't instantiate. According to all kinds of SO questions and tutorials, I'm doing it correctly, but when I print to the stack, it looks like the instantiateItem() method doesn't run at all, even though I'm calling setAdapter(adapter) in the onCreateView of my parent fragment. Everything above the ViewPager inflates fine, but the ViewPager just shows blank nothing.
Here's the code.
public class Profile extends Fragment {
ListView userInfo;
FeedAdapter adapter;
ViewPager pager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.user_profile, container, false);
ProfilePagerAdapter adapter = new ProfilePagerAdapter();
pager = (ViewPager) view.findViewById(R.id.profileViewPager);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
TextView userName = (TextView) view.findViewById(R.id.userName);
userName.setText(Utility.userName);
Log.i("Profile", "Fragment and Pager instantiated");
return view;
}
private class ProfilePagerAdapter extends PagerAdapter{
public ProfilePagerAdapter(){
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch(position){
case 0:
resId = R.layout.news_feed;
View view1 = inflater.inflate(resId,null);
ListView feedList;
FeedAdapter feedAdapter;
feedList = (ListView) view1.findViewById(R.id.feedList);
feedAdapter = new FeedAdapter(getActivity(), Utility.newsFeed);
feedAdapter.notifyDataSetChanged();
feedList.setAdapter(feedAdapter);
((ViewPager) container).addView (view1,0);
Log.i("Profile", "Instantiated case 0");
return view1;
case 1:
resId = R.layout.splash;
View view2 = inflater.inflate(resId,null);
((ViewPager) container).addView(view2,0);
Log.i("Profile", "Instantiated case 1");
return view2;
}
return resId;
}
#Override
public int getCount() {
return 0;
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
}
And here's the xml layout.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profileLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/coverPhoto"
android:layout_width="match_parent"
android:layout_height="133dp"
android:layout_alignParentTop="true"
android:background="#color/grey_color" />
<ImageView
android:id="#+id/profilePhoto"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="100dp"
android:background="#color/com_facebook_blue" />
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/coverPhoto"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#id/profilePhoto"
android:text="User's Name"
android:textSize="24sp" />
<LinearLayout
android:id="#+id/profileButtonBar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/profilePhoto"
android:layout_marginTop="5dp"
android:background="#33b5e5"
android:orientation="horizontal" >
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/profileViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/profileButtonBar"
android:layout_marginTop="5dp"
android:layout_weight="1" />
</RelativeLayout>
It's because you're telling it you have 0 items:
#Override
public int getCount() {
return 0;
}
Related
I want to add a button and button Clicklistenner.
I couldn't find where to add button click, and when I click button, I would like to change transparency of imageview (viewer area) and change position pof text and appear it.
public class SlideshowDialogFragment extends DialogFragment {
private String TAG = SlideshowDialogFragment.class.getSimpleName();
private ArrayList<Image> images;
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private TextView lblCount, lblTitle, lblDate;
private int selectedPosition = 0;
private AdView mAdView;
private Button button;
static SlideshowDialogFragment newInstance() {
SlideshowDialogFragment f = new SlideshowDialogFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_image_slider, container, false);
viewPager = (ViewPager) v.findViewById(R.id.viewpager);
lblCount = (TextView) v.findViewById(R.id.lbl_count);
lblTitle = (TextView) v.findViewById(R.id.title);
lblDate = (TextView) v.findViewById(R.id.date);
button = (Button) v.findViewById(R.id.button);
mAdView = (AdView) v.findViewById(R.id.adView2);
AdRequest adRequest = new AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);
images = (ArrayList<Image>) getArguments().getSerializable("images");
selectedPosition = getArguments().getInt("position");
Log.e(TAG, "position: " + selectedPosition);
Log.e(TAG, "images size: " + images.size());
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
setCurrentItem(selectedPosition);
//Toast start
Context context = v.getContext();
CharSequence text = "Hello Click Fragment!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
//Toast finish
return v;
}
private void setCurrentItem(int position) {
viewPager.setCurrentItem(position, false);
displayMetaInfo(selectedPosition);
}
// page change listener
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
displayMetaInfo(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
};
private void displayMetaInfo(int position) {
lblCount.setText("< "+ (position + 1) + " of " + images.size()+" >");
Image image = images.get(position);
lblTitle.setText(image.getName());
lblDate.setText(image.getTimestamp());
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
// adapter
public class MyViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
public MyViewPagerAdapter() {
}
#Override
public Object instantiateItem(ViewGroup container, 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);
Image image = images.get(position);
Glide.with(getActivity()).load(image.getLarge())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageViewPreview);
container.addView(view);
return view;
}
#Override
public int getCount() {
return images.size();
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((View) obj);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#android:color/black" />
<TextView
android:id="#+id/lbl_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="#android:color/white"
android:textSize="16dp"z
android:textStyle="bold" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/viewpager"
android:background="#color/colorPrimaryDark"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_home_footer">
</com.google.android.gms.ads.AdView>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#android:color/white"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:textColor="#android:color/white" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
To add OnClickListener you should use the function setOnClickListener on your buttons in the onCreateView function.
I am Trying to insert GridView in fragment. I have developed custom adapter to populate the gridview. Now everything is working fine but the gridview is not displaying in the fragment. I have check the getCount() method it is returning total length. So that means data is there but it is not displaying. I am pretty new with gridview so have no idea why it is happening. I tried the following answer but they didn't help me either.
Android: getView in adapter never called
Custom Adapter getView() method is not called
Gridview not displaying in fragment
Here is my code:
Tile Fragment Class:
public class TileFragment extends Fragment
{
public TileFragment(){
}
GridView gv;
Context context;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.mipmap.dicover_disable,R.mipmap.discover_active, R.mipmap.home_disable,
R.mipmap.home_active,R.mipmap.lists_disable,R.mipmap.lists_active,
R.mipmap.profile_disable,R.mipmap.profile_active,R.mipmap.ic_search };
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
#SuppressLint("InflateParams")
// View v = LayoutInflater.from(getActivity()).inflate(R.layout.tile_fragment, null);
View v = inflater.inflate(R.layout.tile_fragment, container, false);
gv = (GridView) v.findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(getActivity(), prgmNameList,prgmImages));
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
}
Custom Adapter:
public class CustomAdapter extends BaseAdapter {
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(FragmentActivity tileFragment, String[] prgmNameList, int[] prgmImages) {
result=prgmNameList;
imageId=prgmImages;
context = tileFragment;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return imageId.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#SuppressLint("ViewHolder")
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.programlist, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
tile_fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="3"
android:visibility="visible">
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</RelativeLayout>
prgramlist
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
This is how I am adding the fragment:
HomeFragment:
public class HomeFragment extends Fragment
{
public HomeFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
FragmentTabHost mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
Bundle b = new Bundle();
b.putString("key", "Tile");
mTabHost.addTab(mTabHost.newTabSpec("tile").setIndicator("Tile"), TileFragment.class, b);
//
b = new Bundle();
b.putString("key", "Map");
mTabHost.addTab(mTabHost.newTabSpec("map").setIndicator("Map"), ProfileFragment.class, b);
return mTabHost;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
//
}
Any Help will be appreciated ...
Try this:
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);
where R.layout.my_parent_fragment is your layout fragment where you have the tabhost.
Once you have verified that the GridView is displayed correctly in an Activity, we could deduce that this is not a problem with the Gridview neither the Adapter, the problem come from your FragmentTabHost definition or setup. You are returning your tabHost from your onCreateView(), for that you must set your layout or container in your tabHost, if you don't do this, how the fragment know where find your layout?
For more info, api here
Can't figure out the root of the problem but try this:
View rowView = convertView;
if(rowView != null) {
rowView = inflater.inflate(R.layout.programlist, null);
}
Try using this layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
>
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</LinearLayout>
I have a ViewPager and it is working. It is showing one image and text on every page.
This is my adapter:
private class SuggestionsAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
private LayoutInflater inflater;
private ViewPager pager;
public SuggestionsAdapter(ViewPager pager) {
this.pager = pager;
}
#Override
public void onPageSelected(int position) {
if (position == 0) {
pager.setCurrentItem(gallery.size(), true);
} else if (position == gallery.size() + 1) {
pager.setCurrentItem(1, true);
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public int getCount() {
return gallery.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.one_suggestion_layout, container, false);
ImageView ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
ivImage.setImageBitmap(gallery.get(position));
// Add viewpager_item.xml to ViewPager
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
container.removeView((RelativeLayout) object);
}
}
And I'm using very simple in my code:
ViewPager vpSuggestions = (ViewPager) findViewById(R.id.vpSuggestions);
SuggestionsAdapter sugAdapter = new SuggestionsAdapter(vpSuggestions);
vpSuggestions.setAdapter(sugAdapter);
This is mu xml for slider:
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7">
<TextView
android:gravity="center"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/suggestions"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/vpSuggestions" />
</LinearLayout>
And this is xml for one page:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ivImage1"
android:adjustViewBounds="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:src="#drawable/cat_321_sofa" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center"
android:id="#+id/tvTitle1"
android:padding="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/ivImage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
Because the size of pager is rectangle (small height, huge width) I want to show two images in one page.
The pager now looks like this image:
I want to be like this image:
How can I do that?
The solution is to override getPageWidth() and divide it with 2
#Override
public float getPageWidth(int position) {
return (super.getPageWidth(position) / 2);
}
I am trying to create a ViewPager that shows a list of ImageViews. I am able to implement the pager and adapter properly, and it works as expected on swiping. However, it does not show the content of the ImageView properly, i.e. the image itself. Its as if the images are there, but they are transparent. Only one image is visible out of the whole lot.
Here is my code-
ImagePagerAdapter.java
public class ImagePagerAdapter extends PagerAdapter {
LayoutInflater inflater;
List<ImageView> views = new ArrayList<>();
boolean[] done = {false,false,false,false,false};
ItemDetailFragment idf;
public ImagePagerAdapter(ItemDetailFragment idf,
LayoutInflater inflater) {
this.idf = idf;
this.inflater = inflater;
for (int i = 0; i < getCount(); i++) {
ImageView iv = new ImageView(idf.getActivity());
iv.setImageResource(R.drawable.light_grey_background);
views.add(iv);
}
}
public ImagePagerAdapter(LayoutInflater inflater) {
this.inflater = inflater;
for (int i = 0; i < getCount(); i++) {
ImageView iv = new ImageView(inflater.getContext());
iv.setImageResource(R.drawable.light_grey_background);
views.add(iv);
}
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
if (done[position]) {
return views.get(position);
}
ImageView v = views.get(position);
views.set(position, v);
((ViewPager) container).addView(v);
done[position] = true;
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return true;
}
}
ItemDetailFragment.java : This is where I set the adapter.
public class ItemDetailFragment extends Fragment {
ViewPager pager;
ImagePagerAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail,
container, false);
pager = (ViewPager) rootView.findViewById(R.id.pager);
adapter = new ImagePagerAdaper(this, inflater);
pager.setAdapter(adapter);
}
fragment_item_detail.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.trial.piclist.ItemDetailFragment" >
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="200dip" >
</android.support.v4.view.ViewPager>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/item_title"
style="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textIsSelectable="true" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/controls_layout" />
</TableRow>
<ScrollView
android:id="#+id/descScrollView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/item_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Please Help. Thanks.
Use:
public class ImagePagerAdapter extends PagerAdapter {
LayoutInflater Inflater;
Activity act;
int count;
public ViewPagerAdapter(Activity a, int c) {
act = a;
count=c;
Inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return count;
}
#Override
public Object instantiateItem(View collection, int position) {
View v1 = Inflater.inflate(R.layout.element_image, null);
ImageView image = (ImageView) v1
.findViewById(R.id.about_image);
image.setImageResource(R.drawable.light_grey_background);
((ViewPager) collection).addView(v1, 0);
return v1;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
Make a layout element_image.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/smv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
Replace
adapter = new ImagePagerAdaper(this, inflater);
with
adapter = new ImagePagerAdaper(getActivity(), count_of_images);
initiate the ImageView in the instantiateItem() method, set the LayoutParams of the imageView
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
Also set the bg colour for debugging
imageView.setBackgroundColor(Color.GREEN);
viewPager.setBackgroundColor(Color.RED);
I have implemented a view pager in android app. I want dynamic text views to be displayed at the top and bottom of the image view. How to set different texts for every image on view pager? Let me know your suggestions.
Thanks.
You need to create custom PagerAdapter for this. I will show you a littel example :
Your Activity :
public class MainActivity
extends Activity{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
List<CustomObject> items = new ArrayList<CustomObject>();
items.add(new CustomObject("First Top","First Bot"));
items.add(new CustomObject("Second Top","Second Bot"));
items.add(new CustomObject("Third Top","Third Bot"));
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
CustomPagerAdapter customPagerAdapter = new CustomPagerAdapter(this,items);
viewPager.setAdapter(customPagerAdapter);
}}
Your adapter :
public class CustomPagerAdapter
extends PagerAdapter{
List<CustomObject> items;
LayoutInflater inflater;
public CustomPagerAdapter(Context context, List<CustomObject> items)
{
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((RelativeLayout) object);
}
#Override
public int getCount()
{
return items.size();
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((View)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
View itemView;
itemView = inflater.inflate(R.layout.your_item_layout, container, false);
TextView topTextItem = (TextView) itemView.findViewById(R.id.topText);
TextView bottomTextItem = (TextView) itemView.findViewById(R.id.bottomText);
CustomObject customObject = items.get(position);
topTextItem.setText(customObject.topText);
bottomTextItem.setText(customObject.bottomText);
((ViewPager) container).addView(itemView);
return itemView;
}}
Main layout - view_main :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Layout item - your_item_layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/topText"
style="#android:style/TextAppearance.Medium"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Top Text" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#android:drawable/ic_delete" />
<TextView
style="#android:style/TextAppearance.Medium"
android:layout_alignParentBottom="true"
android:id="#+id/bottomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="Bottom Text" />
Best wishes.
you can do this in your Fragment class
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.spannerbord_image_new, null);
// RelativeLayout
// layout=(RelativeLayout)v.findViewById(R.id.spanner_image_relative);
Log.v("TEST", "is this printing everytime");
TextView tv = (TextView ) v.findViewById(R.id.my_tv );
}
i hope this works