Skip to main content

How to Create Image Gallery by View Pager In Android

 In some application we have requirement of Image Gallery in which all respective images will show on single UI like Image Gallery App. In this blog we will learn about how to create Image gallery app using View Pager.

before going further here is brief about View Pager:

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android. app.

So let's start making our App Step by Step.


Step 1: Create activity_main.xml and before this make 2 icons prev and next under mipmap folder.

 <?xml version="1.0" encoding="utf-8"?>  
 <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"  
   tools:context=".MainActivity">  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="0dp"  
     android:layout_weight="0.8"  
     android:orientation="horizontal">  
     <ImageView  
       android:id="@+id/prev"  
       android:layout_width="0dp"  
       android:layout_height="wrap_content"  
       android:layout_gravity="center"  
       android:layout_weight="0.1"  
       android:src="@mipmap/prev" />  
     <android.support.v4.view.ViewPager  
       android:id="@+id/view_pager"  
       android:layout_width="0dp"  
       android:layout_height="wrap_content"  
       android:layout_weight="0.8">  
     </android.support.v4.view.ViewPager>  
     <ImageView  
       android:id="@+id/next"  
       android:layout_width="0dp"  
       android:layout_height="wrap_content"  
       android:layout_gravity="center"  
       android:layout_weight="0.1"  
       android:src="@mipmap/next" />  
   </LinearLayout>  
   <HorizontalScrollView  
     android:layout_width="match_parent"  
     android:layout_height="0dp"  
     android:layout_weight="0.2">  
     <LinearLayout  
       android:id="@+id/container"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:orientation="horizontal" />  
   </HorizontalScrollView>  
 </LinearLayout>  

Step 2: Its time to add some more images which we want to show in mipmap folder. we have added a.png,b.png,c.png,d.png,e.png,f.png and g.png icons . 

Step 3: Create fragment_page.xml inside layout folder

 <FrameLayout 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"  
   tools:context="com.droidmedium">  
   <ImageView  
     android:id="@+id/image"  
     android:layout_width="match_parent"  
     android:contentDescription="@string/app_name"  
     android:layout_height="match_parent" />  
 </FrameLayout>  

Step 4 : Now create item_image.xml inside layout folder

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="horizontal">  
   <ImageView  
     android:id="@+id/img_thumb"  
     android:layout_width="80dp"  
     android:layout_height="80dp"  
     android:layout_marginTop="5dp"  
     android:scaleType="centerCrop" />  
 </LinearLayout>  

Step 5: Create PageFragment.java file 

 package com.droidmedium;  
 import android.graphics.Bitmap;  
 import android.graphics.BitmapFactory;  
 import android.os.Bundle;  
 import android.support.v4.app.Fragment;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.ImageView;  
 public class PageFragment extends Fragment {  
   private int imageResource;  
   private Bitmap bitmap;  
   public static PageFragment getInstance(int resourceID) {  
     PageFragment f = new PageFragment();  
     Bundle args = new Bundle();  
     args.putInt("image_source", resourceID);  
     f.setArguments(args);  
     return f;  
   }  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     imageResource = getArguments().getInt("image_source");  
   }  
   @Override  
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
     return inflater.inflate(R.layout.fragment_page, container, false);  
   }  
   @Override  
   public void onViewCreated(View view, Bundle savedInstanceState) {  
     super.onViewCreated(view, savedInstanceState);  
     ImageView imageView = (ImageView) view.findViewById(R.id.image);  
     BitmapFactory.Options o = new BitmapFactory.Options();  
     o.inSampleSize = 4;  
     o.inDither = false;  
     bitmap = BitmapFactory.decodeResource(getResources(), imageResource, o);  
     imageView.setImageBitmap(bitmap);  
   }  
   @Override  
   public void onDestroy() {  
     super.onDestroy();  
     bitmap.recycle();  
     bitmap = null;  
   }  
 }  

Step 6: Create ViewPagerAdapter.java file

 package com.droidmedium;  
 import android.support.v4.app.Fragment;  
 import android.support.v4.app.FragmentManager;  
 import android.support.v4.app.FragmentStatePagerAdapter;  
 import java.util.List;  
 public class ViewPagerAdapter extends FragmentStatePagerAdapter {  
   private List<Integer> images;  
   public ViewPagerAdapter(FragmentManager fm, List<Integer> imagesList) {  
     super(fm);  
     this.images = imagesList;  
   }  
   @Override  
   public Fragment getItem(int position) {  
     return PageFragment.getInstance(images.get(position));  
   }  
   @Override  
   public int getCount() {  
     return images.size();  
   }  
 }  

Step 7 : Now finally our MainActivity.java

 package com.droidmedium;  
 import android.graphics.Bitmap;  
 import android.graphics.BitmapFactory;  
 import android.os.Bundle;  
 import android.support.v4.app.FragmentStatePagerAdapter;  
 import android.support.v4.view.ViewPager;  
 import android.support.v7.app.AppCompatActivity;  
 import android.view.View;  
 import android.widget.ImageView;  
 import android.widget.LinearLayout;  
 import java.util.ArrayList;  
 public class MainActivity extends AppCompatActivity {  
   private ArrayList<Integer> images;  
   private BitmapFactory.Options options;  
   private ViewPager viewPager;  
   private View btnNext, btnPrev;  
   private FragmentStatePagerAdapter adapter;  
   private LinearLayout thumbnailsContainer;  
   private final static int[] resourceIDs = new int[]{R.mipmap.a, R.mipmap.b,  
       R.mipmap.c, R.mipmap.d, R.mipmap.e, R.mipmap.f, R.mipmap.g};  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     images = new ArrayList<>();  
     //find view by id  
     viewPager = (ViewPager) findViewById(R.id.view_pager);  
     thumbnailsContainer = (LinearLayout) findViewById(R.id.container);  
     btnNext = findViewById(R.id.next);  
     btnPrev = findViewById(R.id.prev);  
     btnPrev.setOnClickListener(onClickListener(0));  
     btnNext.setOnClickListener(onClickListener(1));  
     setImagesData();  
     // init viewpager adapter and attach  
     adapter = new ViewPagerAdapter(getSupportFragmentManager(), images);  
     viewPager.setAdapter(adapter);  
     inflateThumbnails();  
   }  
   private View.OnClickListener onClickListener(final int i) {  
     return new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         if (i > 0) {  
           //next page  
           if (viewPager.getCurrentItem() < viewPager.getAdapter().getCount() - 1) {  
             viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);  
           }  
         } else {  
           //previous page  
           if (viewPager.getCurrentItem() > 0) {  
             viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);  
           }  
         }  
       }  
     };  
   }  
   private void setImagesData() {  
     for (int i = 0; i < resourceIDs.length; i++) {  
       images.add(resourceIDs[i]);  
     }  
   }  
   private void inflateThumbnails() {  
     for (int i = 0; i < images.size(); i++) {  
       View imageLayout = getLayoutInflater().inflate(R.layout.item_image, null);  
       ImageView imageView = (ImageView) imageLayout.findViewById(R.id.img_thumb);  
       imageView.setOnClickListener(onChagePageClickListener(i));  
       options = new BitmapFactory.Options();  
       options.inSampleSize = 3;  
       options.inDither = false;  
       Bitmap bitmap = BitmapFactory.decodeResource(getResources(), images.get(i), options );  
       imageView.setImageBitmap(bitmap);  
       //set to image view  
       imageView.setImageBitmap(bitmap);  
       //add imageview  
       thumbnailsContainer.addView(imageLayout);  
     }  
   }  
   private View.OnClickListener onChagePageClickListener(final int i) {  
     return new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         viewPager.setCurrentItem(i);  
       }  
     };  
   }  
 }  

our final application will look like this:




That's All .Thank you .


Comments

Popular posts from this blog

How to Download Apk file from Url and Install Programmatically

In this post we learn about download apk file from server or website and then install it Programmatically in Phone. Sometimes we have to download external apk file from server and then install if downloading successfully finished.For this we use AsyncTask class  for background process. So here is Code Snippet for this task.Lets Start :- Before this we have to add these Permissions in Manifest.xml file : <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> DownloadNewVersion.java class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> { @Override protected void onPreExecute() { super.onPreExecute(); bar = new ProgressDialog(getActivity()); bar.setCancelable(false); bar.setMessage("Downl

How to open pdf file url using webview in Android

 Sometimes we have requirement of showing pdf file in our Android Application. Although there are many third party gradle dependencies available online by which you can show your pdf file easily but one of the major drawback of using these libraries are they will increase you apk size .In some cases they will increase apk size upto ~14 MB. Alternatively you can access and show pdf file using assets and from storage options. This can take less size than any third party library. Here we use pdf using url and if you directly open your pdf file url in webview you will get no result because it will start downoad if you open this link in any web browser.  So in this article we will learn how we can open our pdf url using webview and without using any third party library. So lets Start step by Step:- Lets Suppose you have a pdf file url something like   http://yourwebsite.com/files/mydocumentfile.pdf Step 1: So we you this pdf file url to embed in our webview .First of all you have to host yo

How to use UPI Deep Linking in Android

 In this Article we will discuss about the UPI Deep linking. By this Deep linking we can perform UPI Payments through intent .Basically it open the All UPI supported Application in our mobile then user choose their preference and initiate the Payment and through this we get the payment credentials like payment id ,transaction id ,etc. without any payment gateway. So lets start with what is UPI? Unified Payments Interface(UPI) is an instant real-time payment system developed by National Payments Corporation of India facilitating inter-bank transactions. The interface is regulated by the Reserve Bank of India and works by instantly transferring funds between two bank accounts on a mobile platform. (from Wikipedia) Coding part: package com.example.droidmedium; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Butt