android - I have an image slider made up of 4 images.I would like to place a different text view on each of these images? -
below xml code image slider using viewpager. can see below have 1 textview appears on 4 images.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:id="@+id/image_view" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaletype="centercrop" /> <textview android:id="@+id/myimageviewtext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/image_view" android:layout_aligntop="@+id/image_view" android:layout_alignright="@+id/image_view" android:layout_alignbottom="@+id/image_view" android:layout_margin="1dp" android:gravity="bottom" android:textsize="30dp" android:text="welcome driftfan" android:textstyle="bold" android:textallcaps="true" android:textcolor="@color/colorprimary"/> </relativelayout>
here viewpager xml code
<android.support.v4.view.viewpager android:id="@+id/view_pager" android:layout_width="fill_parent" android:layout_height="204dp" android:scaletype="centercrop" ></android.support.v4.view.viewpager>
and here class
public class mytimertask extends timertask { @override public void run() { if (getactivity()== null) { return; } getactivity().runonuithread(new runnable() { @override public void run() { if (viewpager.getcurrentitem() == 0) { viewpager.setcurrentitem(1); } else if (viewpager.getcurrentitem() == 1) { viewpager.setcurrentitem(2); } else if (viewpager.getcurrentitem() == 2) { viewpager.setcurrentitem(3); } } }); } } }
is there bit of code java class can manually assign text view of images?
public class viewpageradapter extends pageradapter { private context context; private layoutinflater layoutinflater; private integer[] images = {r.drawable.img1, r.drawable.img2, r.drawable.img3, r.drawable.img4}; public viewpageradapter(context context) { this.context = context; } @override public int getcount() { return images.length; } @override public boolean isviewfromobject(view view, object object) { return view == object; } @override public object instantiateitem(viewgroup container, int position) { layoutinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view view = layoutinflater.inflate(r.layout.custom_layout, null); imageview imageview = (imageview) view.findviewbyid(r.id.image_view); imageview.setimageresource(images[position]); viewpager vp = (viewpager) container; vp.addview(view, 0); return view; } @override public void destroyitem(viewgroup container, int position, object object) { viewpager vp = (viewpager)container; view view = (view) object; vp.removeview(view); } }
above viewpager adapter class, basis of imageslidler.
public class viewpageradapter extends pageradapter { private context context; private layoutinflater layoutinflater; private integer[] images = {r.drawable.img1, r.drawable.img2, r.drawable.img3, r.drawable.img4}; private string[] texts= {"text1", "text2","text3","text4"}; public viewpageradapter(context context) { this.context = context; } @override public int getcount() { return images.length; } @override public boolean isviewfromobject(view view, object object) { return view == object; } @override public object instantiateitem(viewgroup container, int position) { layoutinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view view = layoutinflater.inflate(r.layout.custom_layout, null); imageview imageview = (imageview) view.findviewbyid(r.id.image_view); textview tv= (textview) view.findviewbyid(r.id.myimageviewtext); imageview.setimageresource(images[position]); tv.settext(texts[position]); viewpager vp = (viewpager) container; vp.addview(view, 0); return view; } @override public void destroyitem(viewgroup container, int position, object object) { viewpager vp = (viewpager)container; view view = (view) object; vp.removeview(view); } }
Comments
Post a Comment