How to get images dynamically from drawable folder?

by Mansoor Malik   Last Updated December 06, 2018 11:11 AM

I'm getting images manually like private int[]image = {R.drawable.image1,R.drawable.image2,R.drawable.image3}; If I have some 50 images I cant give each and every file name in array so it needs to be dynamic how can I achieve this. And also please tell me where to put my dynamically code

public class SwipeAdapter extends PagerAdapter {
private int[]image =  {R.drawable.image1,R.drawable.image2,R.drawable.image3};private Context cx;

SwipeAdapter(Context cx){

    this.cx=cx;
}

@Override
public int getCount() {
    return image.length;
}@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return (view==(RelativeLayout)object);
}

@SuppressLint("SetTextI18n")
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {

    LayoutInflater layoutInflater = (LayoutInflater) cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    assert layoutInflater != null;
    View view= layoutInflater.inflate(R.layout.page_fragment,container,false);
    ImageView imageView= view.findViewById(R.id.imageView);
    imageView.setImageResource(image[position]);
    container.addView(view);
    return view;
}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView((RelativeLayout) object);
}

}



Related Questions


Updated July 10, 2015 15:04 PM

Updated May 10, 2016 08:04 AM

Updated October 04, 2016 09:04 AM

Updated January 31, 2017 15:04 PM

Updated May 20, 2017 16:11 PM