Monday, 26 December 2016

load images from url and load into ViewPager with json in android

STEP 1: viewpager xml file  
<android.support.v4.view.ViewPager 
 android:id="@+id/pager"
 android:layout_width="match_parent" 
 android:layout_height="match_parent"
 android:background="@color/white"/>

STEP 2: ViewPagerAdapter class
public class ViewPagerAdapter extends PagerAdapter {
    Context c;
    private List<String> _imagePaths;
    private LayoutInflater inflater;
    public ViewPagerAdapter (Context c, List<String> imagePaths) {
        this._imagePaths = imagePaths;
        this.= c;
    }
    @Override    public int getCount() {
        return this._imagePaths.size();
    }
    @Override    public boolean isViewFromObject(View view, Object object) {
        return view == (object);
    }
    @Override    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imgDisplay;
        inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewLayout = inflater.inflate(R.layout.product_viewpager_image, container,
                false);
        imgDisplay = (ImageView) viewLayout.findViewById(R.id.image);
        Glide.with(c).load(_imagePaths.get(position)).
        placeholder(R.drawable.about).error(R.drawable.about).into(imgDisplay);
        (container).addView(viewLayout);
        return viewLayout;
    }
    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) {
        (container).removeView((RelativeLayout) object);
    }

STEP3:main activity code:
List<String> images=new ArrayList<>();
ViewPager viewpager = (ViewPager) findViewById(R.id.pager);
images.add("imageurl");
images.add("imageurl");
images.add("imageurl");
images.add("imageurl");
ViewPagerAdapter adapter new ViewPagerAdapter (MainActivity.thisimages);
viewpager.setAdapter(adapter);

Simple way to circle any image view android






<android.support.v7.widget.CardView 
 android:id="@+id/view2
 android:layout_width="80dp" 
 android:layout_height="80dp"
 android:elevation="12dp" 
 android:innerRadius="0dp" 
 android:shape="ring" 
 android:thicknessRatio="1.9"
 app:cardCornerRadius="40dp"
 <ImageView 
 android:id="@+id/imageView1" 
 android:layout_width="match_parent" 
 android:layout_height="80dp" 
 android:layout_alignParentTop="true" 
 android:layout_centerHorizontal="true" 
 android:src="@drawable/about"/>
 </android.support.v7.widget.CardView>
circle image view xml