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

  • SEARCH
  • Home
  • 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 8569649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:23:07+00:00 2026-06-11T18:23:07+00:00

I have a Horizontal Scroll view in my layout. It contains a a linear

  • 0

I have a Horizontal Scroll view in my layout. It contains a a linear layout(orientation vertical) which in turn contains buttons. in this area I can horizontally scroll (The buttons don’t fit together in one screen at a time in most phones). Android provides me this comfort. Now below this horizontal scroll view, there is a Relative layout.
Now what I want is, when I swipe horizontally in the Relative Layout, I want the buttons in the horizontal scroll view to scroll.
I have tried to implement this by overriding the onTouchEvent(). The problem with this is that, the buttons scroll infinitely(they go out of the screen). I am not able to put a limit. I tried to put a limit. But some how it exceeds the limit by 1 and stops. Then I am not able to scroll.

This is my layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:focusableInTouchMode="true" >

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"  
        android:id="@+id/llt"
        >

        <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="@drawable/button1"
        style="@style/ButtonText"
       android:text="Groups" />

        <Button 
            android:id="@+id/login1"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="@drawable/button1"
        style="@style/ButtonText"
            android:text="QandA"/>
                <Button 
            android:id="@+id/login2"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="@drawable/button1"
            style="@style/ButtonText" 
            android:text="Pending Requests"/>
            <Button
            android:id="@+id/login3"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="@drawable/button1"
            style="@style/ButtonText"
            android:text="Settings"
            ></Button>
                        <Button 
            android:id="@+id/login4"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="@drawable/button1"
        style="@style/ButtonText" 
            android:text="Help"/>
    </LinearLayout>        
        </HorizontalScrollView>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/rl1"
     android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
   >
    .
    .
    .

This is what I have tried :

@Override 
 public boolean onTouchEvent(MotionEvent event) {
   switch (event.getAction()) {
       case MotionEvent.ACTION_DOWN: {
           currentX = (int) event.getRawX();
           currentY = (int) event.getRawY();
           break;
       }

       case MotionEvent.ACTION_MOVE: {
           int x2 = (int) event.getRawX();
           int y2 = (int) event.getRawY();
           if(location1[0]<=leftconst&&(location2[0]+rightmost.getWidth())>=rightconst)
           {
           llt.scrollBy(currentX - x2 ,0);
           }
           currentX = x2;
           currentY = y2;
           break;
       }   
       case MotionEvent.ACTION_UP: {
           break;
       }
   }
     return true; 
 }

leftconst and rightconst are equal to 3 and screenwidth, respectively. But when scrolling it it stops a both ends. Then after that scrolling is not possible. leftmost is the button with id login and rightmost is the button with id login4

  • 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-06-11T18:23:08+00:00Added an answer on June 11, 2026 at 6:23 pm

    After analyzing my own code more deeply, I found that the limits made the scrolling stop forever(it should not stop if the user is scrolling the other way). So I have added another check in the if statement to the check the direction of scrolling (by storing the previous position).

    case MotionEvent.ACTION_MOVE: {
               int x2 = (int) event.getRawX();
               int y2 = (int) event.getRawY();
               int location1[] = new int[2];
               int location2[]=new int[2];
               leftmost.getLocationOnScreen(location1);
               rightmost.getLocationOnScreen(location2);
              Toast.LENGTH_SHORT).show();
               if((x2-currentX<0||location1[0]+1<leftconst)&&((location2[0]+rightmost.getWidth())+1>rightconst||x2-currentX>0))
               {
               llt.scrollBy(currentX - x2 ,0);
               }
               currentX = x2;
               currentY = y2;
               break;
           }
    

    Edit
    A more better and simpler solution without the if statement itself. Instead of scrolling the LinearLayout(llt), just scroll the **HorizontalScrollView** itself! There is no need to specify any limit as the HorizontalScrollView takes care of that.

    case MotionEvent.ACTION_MOVE: {
               int x2 = (int) event.getRawX();
               int y2 = (int) event.getRawY();
               hscrv.scrollBy(currentX - x2 ,0);
               currentX = x2;
               currentY = y2;
               break;
           }
    

    hscrv is the horizontal scroll view containing the linear layout and the buttons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have a scroll view above a horizontal linear layout. My root
I have an HorizontalScrollView which contains a RelativeLayout . This layout is empty in
I have a layout which contains 4 horizontal LinearLayouts with 3 ImageButtons inside each
I have a table to which I have added a vertical & horizontal scrollbars.
I am building a view in my Android application which should scroll both horizontal
I have ScrollView which contains 10 buttons. When a activity is launched only first
I have applied scroll view to the entire layout and that layout has one
I have layout which contains a header, content in the scrollview and a footer.
I am planning to include the feature of horizontal scroll view which gets dynamically
I have a layout that has some horizontal scroll on top, some text box

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.