title: Scrolling a HorizontalScrollView by clicking buttons on its sides
body: <p>I am using a <code>HorizontalScrollView</code> within a <code>Fragment</code>. When I scroll this view instead of scrolling the items within <code>HorizontalScrollView</code> the whole fragment is scrolled to either left or right. Now I have thought of using buttons on both sides of the layout. But cannot get how to scroll my scroll view on either side.</p>
  
  <p>Any help in this regard will be highly appreciated.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Following is my xml file.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" &gt;
  
  &lt;ImageButton
      android:id="@+id/btn_left"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:src="@drawable/left_arrow" /&gt;
  
  &lt;ImageButton
      android:id="@+id/btn_right"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:src="@drawable/right_arrow" /&gt;
  
  &lt;HorizontalScrollView
      android:id="@+id/horizontal_scrollview"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/btn_right"
      android:layout_toRightOf="@id/btn_left"
      android:fadeScrollbars="false"
      android:padding="5dp"
      android:scrollbarAlwaysDrawHorizontalTrack="true"
      android:scrollbars="horizontal" &gt;
  
      &lt;LinearLayout
          android:id="@+id/dynamic_generation"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal" &gt;
      &lt;/LinearLayout&gt;
  &lt;/HorizontalScrollView&gt;
  </code></pre>
  
  <p></p>
  
  <p>and my java file</p>
  
  <pre><code>public class MyGallery extends Activity {
  
  private LinearLayout linearLayout;
  
  private ImageButton leftBtn;
  private ImageButton rightBtn;
  private HorizontalScrollView hsv;
  int currentScrollX = 0;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.horizontalscrolling);
  
      leftBtn = (ImageButton) findViewById(R.id.btn_left);
      rightBtn = (ImageButton) findViewById(R.id.btn_right);
      hsv = (HorizontalScrollView) findViewById(R.id.horizontal_scrollview);
      linearLayout = (LinearLayout) findViewById(R.id.dynamic_generation);
  
      leftBtn.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
          }
      });
  
      rightBtn.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
          }
      });
  
      String[] users = new String[20];
      for (int i = 0; i &lt; 20; i++) {
          users[i] = "user " + (i + 1);
      }
  
      for (int i = 0; i &lt; users.length; i++) {
          final TextView userId = new TextView(MyGallery.this);
          userId.setText(users[i]);
          ImageView imageView = new ImageView(MyGallery.this);
          linearLayout.addView(userId);
          linearLayout.addView(imageView);
          userId.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  Toast.makeText(MyGallery.this,
                          userId.getText() + " has been clicked!",
                          Toast.LENGTH_LONG).show();
                  // hsv.
              }
          });
      }
  }
  
  }
  </code></pre>
  
  <p><strong>What I need</strong> </p>
  
  <p>When I press any of the left or right button scroll view must scroll to left or right respectively.</p>