Android simple image slider example

Android simple image slider example

In order to display something different, you simply have to change the context type your passing. In this case you want to pass images instead of text, as shown in the sample below:

In this Example i will show you how to make a image slider in android.

1. Create a new project in Android studio IDE.
2. Add permissions to your AndroidManifest.xml if you want to load image from url.

<uses-permission android:name="android.permission.INTERNET" />

3.Open your app.gradle file and add dependancies.

    compile 'com.squareup.picasso:picasso:2.3.2'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.daimajia.slider:library:1.1.5@aar'
    compile 'com.android.support:design:25.0.0'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.daimajia.androidanimations:library:1.0.3@aar'

4. Open your layout file and do like this..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/content_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.mr_gunjan.ecommerce.HomeActivity"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    tools:showIn="@layout/app_bar_home">
    <com.daimajia.slider.library.SliderLayout
        android:id="@+id/slider"
        android:layout_width="match_parent"
        custom:pager_animation="Accordion"
        custom:auto_cycle="true"
        custom:indicator_visibility="visible"
        custom:pager_animation_span="1100"
        android:layout_height="200dp"/>

    <com.daimajia.slider.library.Indicators.PagerIndicator
        android:id="@+id/custom_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        custom:selected_color="#0095BF"
        custom:unselected_color="#55333333"
        custom:selected_drawable="@drawable/bird"
        custom:shape="oval"
        custom:selected_padding_left="5dp"
        custom:selected_padding_right="5dp"
        custom:unselected_padding_left="5dp"
        custom:unselected_padding_right="5dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        custom:selected_width="6dp"
        custom:selected_height="6dp"
        custom:unselected_width="6dp"
        custom:unselected_height="6dp"
        android:layout_marginBottom="20dp"
        />
    <com.daimajia.slider.library.Indicators.PagerIndicator
        android:id="@+id/custom_indicator2"
        style="@style/AndroidImageSlider_Corner_Oval_Orange"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        />

</RelativeLayout>

5. Chnage Your mainActivity.java implements this “BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener”.

Like.

public class HomeActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener {
....
....
}

6.In your class oncreate method add this for slider.

//        ADD SLIDER
        mDemoSlider = (SliderLayout)findViewById(R.id.slider);

        HashMap url_maps = new HashMap();
        url_maps.put("Hannibal", "http://www.izzonet.com/wp-content/uploads/2015/09/ecommerce-data-sets.jpg");
        url_maps.put("Big Bang Theory", "http://brotherlabs.com/blog/wp-content/uploads/2015/08/035ec3b.jpg");
        url_maps.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg");
        url_maps.put("Game of Thrones", "http://i.amz.mshcdn.com/Lk8pY_ECprgsM_0W-utY3Pvfl1A=/950x534/2013%2F12%2F26%2F68%2FECommerce.006cd.jpg");

//if you want to load image locally then use below code
//        HashMap file_maps = new HashMap();
//        file_maps.put("Hannibal",R.drawable.hannibal);
//        file_maps.put("Big Bang Theory",R.drawable.bigbang);
//        file_maps.put("House of Cards",R.drawable.house);
//        file_maps.put("Game of Thrones", R.drawable.game_of_thrones);

        for(String name : url_maps.keySet()){
            TextSliderView textSliderView = new TextSliderView(this);
            // initialize a SliderLayout
            textSliderView
                    .description(name)
                    .image(url_maps.get(name))
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .setOnSliderClickListener(HomeActivity.this);

            //add your extra information
            textSliderView.bundle(new Bundle());
            textSliderView.getBundle()
                    .putString("extra",name);

            mDemoSlider.addSlider(textSliderView);
        }
        mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
        mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        mDemoSlider.setCustomAnimation(new DescriptionAnimation());
        mDemoSlider.setDuration(4000);
        mDemoSlider.addOnPageChangeListener(HomeActivity.this);

//        END ADD SLIDER

7. Now add all override method

   @Override
    protected void onStop() {
        // To prevent a memory leak on rotation, make sure to call stopAutoCycle() on the slider before activity or fragment is destroyed
        mDemoSlider.stopAutoCycle();
        super.onStop();
    }

    @Override
    public void onSliderClick(BaseSliderView slider) {
        Toast.makeText(this,slider.getBundle().get("extra") + "",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    @Override
    public void onPageSelected(int position) {
        Log.d("Slider Demo", "Page Changed: " + position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {}




Categories: Android

Tags: ,,,

7 Comments

  1. We’re a group of volunteers and starting a new scheme in our community.
    Your website offered us with valuable info to work on. You have
    performed a formidable job and our whole community will probably be grateful to you.

    Reply
  2. Thanks for any other excellent article. The place else could anybody
    get that kind of information in such an ideal approach of writing?
    I have a presentation subsequent week, and I’m at the search for such info.

    Reply
  3. This is a memo to the website creator. I discovered your Android Image Slider Example – All Tutorial page by searching on Google but it was difficult to find as you were not on the first page of search results. I know you could have more traffic to your website. I have found a company which offers to dramatically improve your rankings and traffic to your website: http://re2l.in/1fe I managed to get close to 500 visitors/day using their services, you could also get many more targeted visitors from search engines than you have now. Their services brought significantly more traffic to my website. I hope this helps!

    Reply
  4. Hello my name is Livia Schacter and I just wanted to drop you a quick note here instead of calling you. I came to your Android Image Slider Example – All Tutorial page and noticed you could have a lot more hits. I have found that the key to running a popular website is making sure the visitors you are getting are interested in your website topic. There is a company that you can get keyword targeted traffic from and they let you try the service for free for 7 days. I managed to get over 300 targeted visitors to day to my website. http://iscripts.co/r

    Reply

Leave A Reply

Your email address will not be published.

error: Alltutorial.in content is protected !!