Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3983410
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:38:41+00:00 2026-05-20T05:38:41+00:00

I want to implement a rotating banner. What control is best suitable for such

  • 0

I want to implement a rotating banner. What control is best suitable for such UI?

here is what it needs to do:

  1. rotate 1 image per view
  2. rotate them only on swipe (or what’s called onFling())
  3. remember the index of the current banner and show next banner when user is redirected onto another activity
  4. rotation is infinite in both directions

So essentially when you land on first activity the first index is shown. When you do swipe it should show next or previous image depending if the swipe is to the left or to the right. This should iterate in the infinite loop.

Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:38:41+00:00Added an answer on May 20, 2026 at 5:38 am

    In case anybody wanted the solution, here is the code:

    public class CustomBanner extends RelativeLayout {
            //================================================================================
            //==== declaration.
            protected List<Banner> _items = new ArrayList<Banner>();
            protected GestureDetector gestureDetector;
                protected View.OnTouchListener gestureListener;
                protected ImageView _banner = null;
            protected String TAG = "CustomBanner";
            //================================================================================
    
            //================================================================================
            //==== properties
            /**
             * This property pulls the banners from the state manager. If the banners are not there they will be pulled from the database.
             * */
            protected List<Banner> getBanners()
            {
                // get your banners however way you get them
                return this._items;
            }
            //================================================================================
    
            //================================================================================
            //==== constructors
            public CustomBanner(Context context, AttributeSet attributes)
            {
                super(context, attributes);
                this.Init(context, attributes);
            }
            //================================================================================
    
            //================================================================================
            //==== protected methods
            protected void Init(Context context, AttributeSet attributes)
            {
                //---- inflate our layout
                LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                layoutInflater.inflate(R.drawable.controls_custombanner, this);
    
                this._banner = (ImageView)findViewById(R.id.imgBanner);
    
                    // swipe detection on the image
                 this.gestureDetector = new GestureDetector(new MyGestureDetector());
                this.gestureListener = new View.OnTouchListener() {
                        public boolean onTouch(View v, MotionEvent event) {
                            if (gestureDetector.onTouchEvent(event)) {
                                return true;
                            }
                            return false;
                        }
                    };
                    // leave this, it needs to be here in order to redirect the swipe correctly to touch gesture direction
                   this._banner.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // leave blank, click is redirected to gesture 
                        }
                    }); 
    
                 this._banner.setOnTouchListener(gestureListener);
    
                    // set banner image
                    this.setBanner(this.getCurrentIndexByWndId());
            }
    
    
            protected int getCurrentIndexByWndId() {
    
                // add your logic here
                // you need to access your state to get the last saved id
            }
    
            /**
             * this function does nothing but set the banner image by the position from the array of banners. 
             * */
            protected void setBanner(int position)
            {
                _banner.setImageResource(this.getBanners().get(position).ResourceId);
            }
    
            /**
             * this function tests the validity of the index in the banners array and resets it to 0 if its not valid.
             * */
            protected int checkValidIndex(int currentIndex){
                if (currentIndex > this.getBanners().size()-1)
                    currentIndex = 0;
                return currentIndex;
            }
    
            protected void processBannerClick(Banner clickedBanner){
                try
                {
                    // here do your logic for the click
                }
                catch (Exception ex)
                {
                    Log.e(App.Current.getErrorTag(), ex.getMessage());
                }
            }
    
            //================================================================================
    
            //================================================================================
            //==== helper classes
            /**
             * this is a helper class to detect the gesture or swipe. It uses the onFling() to test whether it was a right or left swipe. 
             * It increments the position of and sets the appropriate next image on the banner.
             * */
            protected class MyGestureDetector extends SimpleOnGestureListener {
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    Log.i(TAG, "BANNER: onFling() happened");
                    int currentIndex = 0;
                    try {
                        currentIndex = FromState.getCurrentAdRotatorPosition();
                        if(isScrollingLeft(e1, e2)) {
                            // left swipe
                            if (currentIndex > 0)
                                currentIndex --;
                            else currentIndex = getBanners().size()-1;
                        }  
                        else {
                            // right swipe
                            if (currentIndex < getBanners().size()-1)
                                currentIndex ++;
                            else currentIndex = 0;
                        }
                    } catch (Exception e) {
                        // do nothing
                    }
                    FromState..setCurrentAdRotatorPosition(currentIndex);
                    setBanner(FromState.getCurrentAdRotatorPosition());
                    return false;
                }
    
                @Override
                public boolean onSingleTapUp(MotionEvent ev)
                {
                    Log.i(TAG, "BANNER: onSingleTapUp() happened");
                    processBannerClick(getBanners().get(FromState.getCurrentAdRotatorPosition()));
                    return false;
                }
    
    
                /** check if left swipe happened */
                private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){ 
                    return e2.getX() > e1.getX(); 
               }
            }
            //================================================================================
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement the solution using the pre-processor described here: Reuse define statement
I want to implement search functionality for a website (assume it is similar to
I want to implement an automatic update system for a windows application. Right now
I want to implement a paperless filing system and was looking to use WIA
I want to implement in Java a class for handling graph data structures. I
I want to implement a two-pass cache system: The first pass generates a PHP
I want to implement an ISAPI filter like feature using HttpModule in IIS7 running
I want to implement a simple debug log, which consists of a table into
I want to implement user stories in a new project where can i find
I want to implement forms authentication on an ASP.NET website, the site should seek

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.