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
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).
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 theHorizontalScrollViewtakes care of that.hscrvis the horizontal scroll view containing the linear layout and the buttons.