Coder Social home page Coder Social logo

android-image-slider's Introduction

Android image slider

Download Android Arsenal Apk

API License

This is an amazing image slider for the Android .

You can easily load images with your custom layout, and there are many kinds of amazing animations you can choose.

     implementation 'com.github.smarteist:autoimageslider:1.4.0'

If you are using appcompat libraries use this one, but please migrate to androidx as soon as you can.

     implementation 'com.github.smarteist:autoimageslider:1.4.0-appcompat'

New Feautures

  • Ability to disable default indicator.

New Changes

  • Auto cycle bugs fixed.
  • Swiping debounce implemented.

Demo

Integration guide

First put the slider view in your layout xml :

        <com.smarteist.autoimageslider.SliderView
                    android:id="@+id/imageSlider"
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    app:sliderAnimationDuration="600"
                    app:sliderAutoCycleDirection="back_and_forth"
                    app:sliderAutoCycleEnabled="true"
                    app:sliderIndicatorAnimationDuration="600"
                    app:sliderIndicatorGravity="center_horizontal|bottom"
                    app:sliderIndicatorMargin="15dp"
                    app:sliderIndicatorOrientation="horizontal"
                    app:sliderIndicatorPadding="3dp"
                    app:sliderIndicatorRadius="2dp"
                    app:sliderIndicatorSelectedColor="#5A5A5A"
                    app:sliderIndicatorUnselectedColor="#FFF"
                    app:sliderScrollTimeInSec="1"
                    app:sliderStartAutoCycle="true" />

Or you can put it inside the cardView to look more beautiful :

       <androidx.cardview.widget.CardView
               app:cardCornerRadius="6dp"
               android:layout_margin="16dp"
               android:layout_width="match_parent"
               android:layout_height="wrap_content">

               <com.smarteist.autoimageslider.SliderView
                           android:id="@+id/imageSlider"
                           android:layout_width="match_parent"
                           android:layout_height="300dp"
                           app:sliderAnimationDuration="600"
                           app:sliderAutoCycleDirection="back_and_forth"
                           app:sliderAutoCycleEnabled="true"
                           app:sliderIndicatorAnimationDuration="600"
                           app:sliderIndicatorGravity="center_horizontal|bottom"
                           app:sliderIndicatorMargin="15dp"
                           app:sliderIndicatorOrientation="horizontal"
                           app:sliderIndicatorPadding="3dp"
                           app:sliderIndicatorRadius="2dp"
                           app:sliderIndicatorSelectedColor="#5A5A5A"
                           app:sliderIndicatorUnselectedColor="#FFF"
                           app:sliderScrollTimeInSec="1"
                           app:sliderStartAutoCycle="true" />

       </androidx.cardview.widget.CardView>

Next step

The new version requires an slider adapter plus your custom layout for slider items, Although its very similar to RecyclerView & RecyclerAdapter, and it's familiar and easy to implement this adapter... here is an example for adapter implementation :

public class SliderAdapterExample extends
        SliderViewAdapter<SliderAdapterExample.SliderAdapterVH> {

    private Context context;
    private List<SliderItem> mSliderItems = new ArrayList<>();

    public SliderAdapterExample(Context context) {
        this.context = context;
    }

    public void renewItems(List<SliderItem> sliderItems) {
        this.mSliderItems = sliderItems;
        notifyDataSetChanged();
    }

    public void deleteItem(int position) {
        this.mSliderItems.remove(position);
        notifyDataSetChanged();
    }

    public void addItem(SliderItem sliderItem) {
        this.mSliderItems.add(sliderItem);
        notifyDataSetChanged();
    }

    @Override
    public SliderAdapterVH onCreateViewHolder(ViewGroup parent) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_slider_layout_item, null);
        return new SliderAdapterVH(inflate);
    }

    @Override
    public void onBindViewHolder(SliderAdapterVH viewHolder, final int position) {

        SliderItem sliderItem = mSliderItems.get(position);

        viewHolder.textViewDescription.setText(sliderItem.getDescription());
        viewHolder.textViewDescription.setTextSize(16);
        viewHolder.textViewDescription.setTextColor(Color.WHITE);
        Glide.with(viewHolder.itemView)
                .load(sliderItem.getImageUrl())
                .fitCenter()
                .into(viewHolder.imageViewBackground);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "This is item in position " + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getCount() {
        //slider view count could be dynamic size
        return mSliderItems.size();
    }

    class SliderAdapterVH extends SliderViewAdapter.ViewHolder {

        View itemView;
        ImageView imageViewBackground;
        ImageView imageGifContainer;
        TextView textViewDescription;

        public SliderAdapterVH(View itemView) {
            super(itemView);
            imageViewBackground = itemView.findViewById(R.id.iv_auto_image_slider);
            imageGifContainer = itemView.findViewById(R.id.iv_gif_container);
            textViewDescription = itemView.findViewById(R.id.tv_auto_image_slider);
            this.itemView = itemView;
        }
    }

}

Custom Slider Image Layout

you can make your own layout for slider view

here is an example for adapter implementation :

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_auto_image_slider"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY" />


    <ImageView
        android:id="@+id/iv_gif_container"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="bottom|start"
        android:layout_margin="50dp" />


    <FrameLayout
        android:id="@+id/fl_shadow_container"
        android:background="@drawable/bg_overlay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <TextView
            android:id="@+id/tv_auto_image_slider"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="25dp"
            android:padding="6dp"
            android:textColor="#FFF"
            android:textSize="18sp" />

    </FrameLayout>

</FrameLayout>

Set the adapter to the Sliderview

After the instantiating of the sliderView (inside the activity or fragment with findViewById|BindView...), set the adapter to the slider.

    SliderView sliderView = findViewById(R.id.imageSlider);
    sliderView.setSliderAdapter(new SliderAdapterExample(context));

You can call this method if you want to start flipping automatically and you can also set up the slider animation :

    sliderView.setIndicatorAnimation(IndicatorAnimationType.WORM);
    sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
    sliderView.startAutoCycle();

Elaborate more?

Here is a more realistic and more complete example :

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

            SliderView sliderView = findViewById(R.id.imageSlider);

            SliderAdapterExample adapter = new SliderAdapterExample(this);

            sliderView.setSliderAdapter(adapter);

            sliderView.setIndicatorAnimation(IndicatorAnimationType.WORM); //set indicator animation by using IndicatorAnimationType. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!!
            sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
            sliderView.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH);
            sliderView.setIndicatorSelectedColor(Color.WHITE);
            sliderView.setIndicatorUnselectedColor(Color.GRAY);
            sliderView.setScrollTimeInSec(4); //set scroll delay in seconds :
            sliderView.startAutoCycle();

        }

Contribute

Suggestions and pull requests are always welcome. Special Thanks [Roman Danylyk] (https://github.com/romandanylyk) for nice indicator!

Licence

Copyright [2019] [Ali Hosseini]

Licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

android-image-slider's People

Contributors

abilogos avatar aliakseikanapeshka avatar guilhermehmcarvalho avatar itsniaz avatar pavan-vemuri avatar sarankumar-ns avatar smarteist avatar srggrch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-image-slider's Issues

Not working for 2 slides only

Everything works fine if there is minimum 3 slides but it not works for 2 slides only, in this case the second slide immediately shifts without waiting.

Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference com.smarteist.autoimageslider.DefaultSliderView.getView

Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference com.smarteist.autoimageslider.DefaultSliderView.getView

android.view.LayoutInflater.from (LayoutInflater.java:234)
com.smarteist.autoimageslider.DefaultSliderView.getView (DefaultSliderView.java:33)
com.smarteist.autoimageslider.SliderAdapter.instantiateItem (SliderAdapter.java:54)
androidx.viewpager.widget.ViewPager.addNewItem (ViewPager.java:1010)
androidx.viewpager.widget.ViewPager.setCurrentItem (ViewPager.java:623)
com.smarteist.autoimageslider.SliderLayout$1.run (SliderLayout.java:269)
android.os.Handler.handleCallback (Handler.java:739)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)

Turn of over scrolling

Hi Ali Hosseini,

I really appreciate your great work.
Just have a short question.
Is there a way to stop scrolling to a direction when there is no image and the view jumps to the first image?
E.g. there are 3 images and my current position is the third one. So when i am tryin to scroll on. It jumps to the first image.

Thanks.

Best regards

Clear sliderViews from SliderLayout

Requesting a feature to clear the sliderViews contained within a SliderLayout.
This'd be a necessary feature to use the Slider on a RecycleView's row, for instance.

SliderView was not registered

I am getting the following error, when I try to open another activity which uses sliderview from an activity that uses sliderview too

IllegalStateException: Observer com.smarteist.autoimageslider.SliderView$1@1cdc833 was not registered.

how to passing image url from slider to other activity?

i was trying to pass image url from setOnSliderListener and return null,
can someone help me?

here my code

    for (i in 0..3) {

        val sliderView = SliderView(ctx)

        when (i) {
            0 -> sliderView.imageUrl = "https://images.pexels.com/photos/547114/pexels-photo-547114.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"
            1 -> sliderView.imageUrl = "https://images.pexels.com/photos/218983/pexels-photo-218983.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"
            2 -> sliderView.imageUrl = "https://images.pexels.com/photos/747964/pexels-photo-747964.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260"
            3 -> sliderView.imageUrl = "https://images.pexels.com/photos/929778/pexels-photo-929778.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"
        }

        sliderView.setImageScaleType(ImageView.ScaleType.CENTER_CROP)
        sliderView.setDescription("setDescription " + (i + 1))
        sliderView.setOnSliderClickListener {
            startActivity<DetailSliderActivity>(
                    "imageSliderUrl" to i
            )
        }

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

Set selected item programmatically

Hello and thank you for this library!

I'm trying to implement a fullscreen image viewer when tapping the slideshow and so far I;ve got it working, but I need to set the selected item in the slideshow programmatically, so when the user changes the page in my fullscreen image view, the slide show changes as well. Is this possible?

Thank you

Indicator placement

Hi @smarteist , is it possible to change the location of the indicator? I'm hoping it can be placed at the bottom right of the SliderView. I also tried to hide the default PagerIndicator using setPagerIndicatorVisibility(false) but it does not set its visibility to GONE.

error when we take 2 banner on same activty.

When i am taking 2 slider layout on same activity as <com.smarteist.autoimageslider.SliderLayout
android:id="@+id/imageSliderTop"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
and
<com.smarteist.autoimageslider.SliderLayout
android:id="@+id/imageSliderBottom"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

then slider comes 1 for both.

How to resolve this issue?

slider overlay effect

Hello, Currently I am using your slider library in one of my apps. And I am facing some problems

  1. app:defaultIndicators="square" can't use this function on this slider

  2. And most Importantly, I want to make the slider Effect to be white or transparent. what I am saying is, in this slider the bottom is getting blurred by black shading. I want to remove that shade or make that shade to white shading.
    How can I do that? please, suggest...

slider effect

how to use in androidx?

I use androidx and there are some missing errors like the following :

Cannot access '': it is public/package/ in 'SliderView'

Change description text color and size

I have used the following code:

  1. sliderView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
  2. sliderView.setDescriptionTextColor(R.color.colorRed);
  3. sliderView.setDescriptionTextSize(16);
  4. sliderView.setDescription(sliderDesc[i]);

code at line 2 and 3 has no effect on the sliders. I want that code in working.

Problem in transition

Setting the time to more than 10 seconds, when the next side is scrolled, it doesnot obey the mentioned slider time set.

how to use without androidx?

I cannot use adapt. It's rise an error: setSliderAdapter (androidx.viewpaper.widget.PaperAdapter) in SliderView cannot be applied to ...

Error getCount() java.lang.NullPointerException

Hi, have an error when run app. It's rise "java.lang.NullPointerException: Attempt to invoke virtual method 'int androidx.viewpager.widget.PagerAdapter.getCount()' on a null object reference at com.smarteist.autoimageslider.SliderView$2.run(SliderView.java:397)..."

Handle Click Event on sliderView

Hello Ali Hosseini,

Thank you for such a wonderful library. I really appreciate your work.

I am a beginner in android and learnt about your library. I have implemented the Image Slider using your tutorial but I don't know how can I handle Click events on the Image ?

add url and name of pics in imageslider

Hi. Thank you for the library. I want to use VOLLEY to get the image address and description for each image from JESON and add it to SwitchCase. How should I do that?
Thank you for your help

Missing placeholder in glide

Hello your Library is working fine but it has missing placeholder image functionality in glide
SliderView.java line 152, 155
Please add
.apply(new RequestOptions().placeholder(placeholderImageDrawable))

Because of missing Placeholder if image at the specified URL doesn't exist it will show blank screen

TypeFace!!

Please Insert The SetTypeFace For TextView in SliderView Class.
Thanks...

This is not an issue!!! want to use this with android rather than androidx

i wanted to use this project without androix but this lib is made using androidx support.
this because i have came so far with my project and there are so many lib and i can't bear any bugs/ error by importing the project at androidx because there are so many third party library that i use which is not made on androidx so what i did i clone your project ,remove the pakage name,change the androidx to android and created the AAR file to be used in my project without androidx support . now i want you to take this project from me and publish it at your repo as jar
so that it can be accessible to all who want to use it without androidx.

but i don't know how to contact you or how i can it to give you ..
and thanks again for this amazing lib

Clear sliderViews from SliderLayout didn't clear previous images

I want to clear slider before add another slider, but the previous image didn't clear.

private fun setSliderViews(bannerArr: JsonArray) {
        if (imageSlider != null) {
            imageSlider.clearSliderViews()
        }
        val sliderView = DefaultSliderView(requireContext())

        for (i in 0 until bannerArr.size()) {
            val bannerObj = bannerArr[i].asJsonObject
            sliderView.imageUrl = bannerObj.get("image_url").asString
            

            if (imageSlider != null) {
                imageSlider.addSliderView(sliderView)
            }
        }
    }

Delete slider image onLongClickListener

Hi,
I would like to delete an image and update the slider afterwards. How can I do that?

@Override
public void onBindViewHolder(SliderAdapterVH viewHolder, final int position) {
    viewHolder.itemView.setOnLongClickListener(v -> {
    AbstractMarkedImage image = myImages.get(position);
    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
    builder.setIcon(android.R.drawable.ic_dialog_alert);
    builder.setMessage(R.string.del_img_msg).setTitle(R.string.del_img_title);
    builder.setNegativeButton(R.string.cancle, (dialog, which) -> dialog.cancel());
    builder.setPositiveButton(R.string.delete, (dialog, which) -> {
            if(myImages.contains(aMarkedImage)) {
                aMarkedImage.setDeleted(true);
                myImages.remove(aMarkedImage);
                setImageCount(myImages.size());

                notifyDataSetChanged();
            }
    });

    AlertDialog dialog = builder.create();
    dialog.show();

      return true;
    });
  }

Thanks in advance!

Error when slide to left and right from start and end position.

java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager.widget.ViewPager.setCurrentItem(int)' on a null object reference
at com.smarteist.autoimageslider.CircularSliderHandle.onPageScrollStateChanged(CircularSliderHandle.java:37)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.