Search code examples
androidandroid-fragmentsandroid-recyclerviewscrollbanner

Android Autoscrolling view without images


I am currently trying to implement a banner-like view where given a list of strings, the view will cyclically show each string one after the other with a pause when they arrive in the center of the screen. An example of what I want to do is the following Sliding banner

In HTML, this element is a swiper slide from swiperjs, however, Android has no native element that does this simply.

I have looked online for a couple of days now, testing various implementations using a custom RecyclerView, simple TextView with marquee (which doesn't have the pause I want to add) and have found that the majority of "solutions" revolve around either a RecyclerView, a ListView or a ViewPager with usually a TimerTask to handle the scrolling timing.

Unfortunately, I haven't found any implementation that works nor any indication as to which view is best to use.

All solutions implement an image and a dot selector showing which image from the list is shown on screen but I have absolutely no desire to have an image, I simply want a TextView with the same functionality. Before anyone asks, yes I have tried to simply replace the Images with a TextView but it simply doesn't work.

One important aspect to note is that since I am only trying to make some text scroll into view, I don't think ViewPager is an effective solution since I am not at all implementing some scrolling fragments. Here is a layout of how the screen elements are disposed (Note this is the MainActivity screen and I have a fragment host within it for all other fragments):

+-------------+-------------+
|             |   Toolbar   |
|             |-------------+
|             |    Banner   |
|             |-------------+
| Nav Drawer  |             |
|             |             |
|             |  Fragment   |
|             |             |
|             |             |
+-------------+-------------+

Any leads in the direction how to implement this functionality is welcome.


Solution

  • I did it using LoopingViewPager like this.

    Step 1: add dependancy in your app gradle

    implementation 'com.asksira.android:loopingviewpager:1.1.2'
    

    Step 2: add following code in your activity/fragment

        val sliderAdapter = SliderAdapter(this, arrayListOf("Your Text here","Your Text here","Your Text here"))
    
        val viewPager=findViewById<LoopingViewPager>(R.id.viewPager)
    
        viewPager.adapter = sliderAdapter
    

    Step 3: Add LoopingViewPager view in your Activity/Fragment Layout

    <?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"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical">
    
        <com.asksira.loopingviewpager.LoopingViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:clickable="true"
            app:autoScroll="true"
            app:isInfinite="true"
            app:scrollInterval="3000"
            app:wrap_content="true" />
    
    </LinearLayout>
    

    Step 4: Create Adapter for LoopingViewPager

    class SliderAdapter(context: Context, val itemList2: ArrayList<String>) :
    
    LoopingPagerAdapter<String>(context, itemList2, true) {
    
    override fun inflateView(viewType: Int, container: ViewGroup?, listPosition: Int): View {
        return LayoutInflater.from(context).inflate(R.layout.slider_layout, container, false)
    }
    
    override fun bindView(convertView: View?, listPosition: Int, viewType: Int) {
    
        var a = itemList2.get(listPosition)
        convertView?.findViewById<TextView>(R.id.textView)?.text=a
    }
    }
    

    Step 5: Create layout for an item

    <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">
    
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />
    
    </LinearLayout>