<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/imageGridView"
        android:numColumns="4"
        android:paddingTop="5dp"
        android:verticalSpacing="5dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load More"
        android:id="@+id/btnLoad"
        android:visibility="gone"
        android:layout_gravity="center_horizontal|bottom" />
</LinearLayout>


_______________________________


public class ImageThumbnailAdapter extends BaseAdapter {
        private DisplayMetrics display;
        private Context mContext;
        int width;

        public ImageThumbnailAdapter(Context c) {
            mContext = c;
            display = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(display);
            width = display.widthPixels;
        }

      

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(2 * width / 9, 2 * width / 9));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(0, 0, 0, 0);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageBitmap(bitmaps.get(position));
            return imageView;
        }
    }

_______________________________

    private ImageThumbnailAdapter adapter;
    private List<Bitmap> bitmaps = new ArrayList<Bitmap>();
    private List<String> photoIds = new ArrayList<String>();



_____________________________________________________________________



 public class AlbumTask extends AsyncTask<String, Integer, Void> {
        private Void getPhotosFromJson(String rawData) {
            JSONObject albumData;
            JSONArray photolistData;
            try {
                albumData = new JSONObject(rawData);
                photolistData = albumData.getJSONArray("data");

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
            int count = photolistData.length();
            Log.d("JSON", rawData);
            for (int i = 0; i < count; i++) {
                try {
                    photoIds.add(photolistData.getJSONObject(i).getString("id"));
                    //Here we actually download the thumbnail using URL obtained from JSONObject
                    try {
                        URL imageUrl = new URL(photolistData.getJSONObject(i).getJSONObject("images")
                                .getJSONObject("thumbnail")
                                .getString("url"));
                        //connect to given server
                        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
                        //safety features and avoiding errors is links redirect
                        conn.setConnectTimeout(30000);
                        conn.setReadTimeout(30000);
                        conn.setInstanceFollowRedirects(true);
                        //setting inputstream and decoding it into a bitmap
                        InputStream is = conn.getInputStream();
                        bitmaps.add(BitmapFactory.decodeStream(is));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        return null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }

            }
            return null;

        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                URL url;
                url = new URL(getRequestUri().toString());
                Log.d("Photos", url.toString());

                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                //read all the data
                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line + "\n"); //Debugging ease
                }

                if (buffer.length() == 0) {
                    Log.d("DONE","EMPTY BUFFER");

                    return null;
                } else {
                    Log.d("DONE", String.valueOf(buffer.length()));

                }
                String photosJsonStr = buffer.toString();
                getPhotosFromJson(photosJsonStr);

                if (inputStream == null) {
                    // Nothing to do.
                    return null;
                }
            } catch (Exception e) {
                Log.e("URI", "uri was invalid or api request failed");
                e.printStackTrace();
            }
            return null;
        }

        private Uri getRequestUri() {
            final String USER_ID = "1701574";    //Yale instagram user id
            final String BASE_URL = "https://api.instagram.com/v1/users/"
                    + USER_ID + "/media/recent?";
            Uri builtUri;
            builtUri = Uri.parse(BASE_URL).buildUpon()
                    .appendQueryParameter("client_id", "ae7e1eced7784baca1d6cc09f50848e1")
                    .build();
            return builtUri;
        }

        @Override
        protected void onPostExecute(Void result) {
            //notify the adapter and the view that is using it to get check for new data
            //in the arrays we defined at the beginning
            Log.d("DONE", "Reached on Post Exec");
            Log.d("DONE", "bitmap size:" + bitmaps.size());
            adapter.notifyDataSetChanged();

        }

    }
