Android image slider with json

Android image slider

This is an amazing image slider for the Android .

You can easily load images from an internet URL, drawable, or file. And there are many kinds of amazing animations you can choose.

 implementation 'com.github.smarteist:autoimageslider:1.2.0'
 implementation 'com.github.bumptech.glide:glide:4.7.1'

New Feautures

Ability to change slide animation by using sliderLayout.setSliderTransformAnimation(); Some issues fixed. Ability to implement a custom sliderView with exteding from class “SliderView” and change it any way you want.

Demo

Integration guide

Just put the view in the layout xml like this:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.smarteist.autoimageslider.SliderLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentTop="true"
        android:id="@+id/imageSlider"
        tools:ignore="MissingConstraints" />



</android.support.constraint.ConstraintLayout>

And implement the slider with your own programming Here is an example of the implementation of this library in java :

MainActivity.java

package com.androier.imageslider;


import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
import com.smarteist.autoimageslider.DefaultSliderView;
import com.smarteist.autoimageslider.IndicatorAnimations;
import com.smarteist.autoimageslider.SliderAnimations;
import com.smarteist.autoimageslider.SliderLayout;
import com.smarteist.autoimageslider.SliderView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class MainActivity extends AppCompatActivity {

    SliderLayout sliderLayout;
    public static final int CONNECTION_TIMEOUT = 10000;
    public static final int READ_TIMEOUT = 15000;
    String url1,url2,url3,url4,url5,url6,url7,url8,url9,url10=null;
    String description1,description2,description3,description4,description5,description6,description7,description8,description9,description10;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sliderLayout = findViewById(R.id.imageSlider);
        sliderLayout.setIndicatorAnimation(IndicatorAnimations.DROP); //set indicator animation by using SliderLayout.IndicatorAnimations. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!!
        sliderLayout.setSliderTransformAnimation(SliderAnimations.CUBEOUTSCALINGTRANSFORMATION);
        sliderLayout.setScrollTimeInSec(2); //set scroll delay in seconds :

        new AsyncFetch().execute();


    }




    //json
    private class AsyncFetch extends AsyncTask<String, String, String> {


        HttpURLConnection connection;
        URL url = null;
        Intent intent = getIntent();


        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //this method will be running on UI thread

        }

        @Override
        protected String doInBackground(String... strings) {

            try {

                // Enter URL address where your json file resides
                // Even you can make call to php file which returns json data
                url = new URL("http://cricket.androier.com/cwc/imagegellary.json");

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return e.toString();
            }

            try {

                // Setup HttpURLConnection class to send and receive data from php and mysql
                connection = (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(READ_TIMEOUT);
                connection.setConnectTimeout(CONNECTION_TIMEOUT);
                connection.setRequestMethod("GET");

                // setDoOutput to true as we recieve data from json file
                connection.setDoOutput(true);

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return e1.toString();
            }


            try {

                int response_code = connection.getResponseCode();

                // Check if successful connection made
                if (response_code == HttpURLConnection.HTTP_OK) {

                    // Read data sent from server
                    InputStream input = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                    StringBuilder result = new StringBuilder();
                    String line;

                    while ((line = reader.readLine()) != null) {
                        result.append(line);
                    }

                    // Pass data to onPostExecute method
                    return (result.toString());

                } else {

                    return ("unsuccessful");
                }

            } catch (IOException e) {
                e.printStackTrace();
                return e.toString();
            } finally {
                connection.disconnect();
            }

        }


        @Override
        protected void onPostExecute(String result) {
            //this method will be running on UI thread

            Toast.makeText(MainActivity.this, "receive json data successfully", Toast.LENGTH_LONG).show();



            try {

                JSONArray jArray = new JSONArray(result);

                // Extract data from json and store into ArrayList as class objects
                for (int j = 0; j < jArray.length(); j++) {
                    JSONObject json_data = jArray.getJSONObject(j);
                    url1 = json_data.getString("url1");
                    url2 = json_data.getString("url2");
                    url3 = json_data.getString("url3");
                    url4 = json_data.getString("url4");
                    url5 = json_data.getString("url5");
                    url6 = json_data.getString("url6");
                    url7 = json_data.getString("url7");
                    url8 = json_data.getString("url8");
                    url9 = json_data.getString("url9");
                    url10 = json_data.getString("url10");

                    description1 = json_data.getString("description1");
                    description2 = json_data.getString("description2");
                    description3 = json_data.getString("description3");
                    description4 = json_data.getString("description4");
                    description5 = json_data.getString("description5");
                    description6 = json_data.getString("description6");
                    description7 = json_data.getString("description7");
                    description8 = json_data.getString("description8");
                    description9 = json_data.getString("description9");
                    description10 = json_data.getString("description10");




                }
// Setup and Handover data to recyclerview

            } catch (JSONException e) {
                Toast.makeText(MainActivity.this, "Unexpected server error", Toast.LENGTH_LONG).show();


            }



            //slide start
            for (int i = 0; i <=9; i++) {

                DefaultSliderView sliderView = new DefaultSliderView(MainActivity.this);

                switch (i) {
                    case 0:
                        sliderView.setImageUrl(url1);
                        sliderView.setDescription(description1);

                        break;
                    case 1:
                        sliderView.setImageUrl(url2);
                        sliderView.setDescription(description2);
                        break;
                    case 2:
                        sliderView.setImageUrl(url3);
                        sliderView.setDescription(description3);
                        break;
                    case 3:
                        sliderView.setImageUrl(url4);
                        sliderView.setDescription(description4);
                        break;
                    case 4:
                        sliderView.setImageUrl(url5);
                        sliderView.setDescription(description5);
                        break;
                    case 5:
                        sliderView.setImageUrl(url6);
                        sliderView.setDescription(description6);
                        break;
                    case 6:
                        sliderView.setImageUrl(url7);
                        sliderView.setDescription(description7);
                        break;
                    case 7:
                        sliderView.setImageUrl(url8);
                        sliderView.setDescription(description8);
                        break;
                    case 8:
                        sliderView.setImageUrl(url9);
                        sliderView.setDescription(description9);
                        break;
                    case 9:
                        sliderView.setImageUrl(url10);
                        sliderView.setDescription(description10);
                        break;

                }

                sliderView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
                //sliderView.setDescription("The quick brown fox jumps over the lazy dog.\n" +
                       // "Jackdaws love my big sphinx of quartz. " + (i + 1));

                final int finalI = i;

                sliderView.setOnSliderClickListener(new SliderView.OnSliderClickListener() {
                    @Override
                    public void onSliderClick(SliderView sliderView) {
                        Toast.makeText(MainActivity.this, "This is slider " + (finalI + 1), Toast.LENGTH_SHORT).show();

                    }
                });

                //at last add this view in your layout :
                sliderLayout.addSliderView(sliderView);
            }






            //


        }



    }
}
One Comment

Add a Comment

Your email address will not be published. Required fields are marked *