0

I have a horizontal scroll bar with icons (buttons) on it.

I try to move the selected icon to the middle of the bar.

I have read this post about positioning views in android.

I have got this code, which seems logically ok to me:

    public void selectButton() {
 ...
            HorizontalScrollView sv=(HorizontalScrollView)button.getParent().getParent();
            int offsetX=getButtonXPosition()-sv.getWidth()/2;
            sv.smoothScrollTo(offsetX, 0);

..
    }


    public int getButtonXPosition() {
        return (button.getLeft()+button.getRight())/2;
    }

I move left top corner a pixels to right\left (negative\positive number of pixels),

where a = X position of middle of selected button - middle of the bar.

meaning I want to move the middle of the button a pixels to the right\left (negative\positive)

However the buttons stop too left\right (if it's the rightmost\leftmost button)

see images attached:

enter image description here

enter image description here

Community
  • 1
  • 1
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • Maybe you should use smoothScrollBy? It looks like you're counting difference instead of real position. – Desert Jul 15 '13 at 12:55
  • @user1873880 I thought you were right. But a) it's difference relative to (0,0) so it doesn't matter. b)`smoothScrollBy` made things even messier – Elad Benda Jul 15 '13 at 13:41

1 Answers1

-1

Try this....

<HorizontalScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"  
        android:layout_margin="10dp">

    </LinearLayout>
</HorizontalScrollView>


for (int x = 0; x < ImagesList.size(); x++) {
    imageHor = new ImageView(FullIMageScreenNew.this);
    linearLayout.setTag(x);
    imageHor.setImageBitmap(decodeSampledBitmapFromResource(ImagesList.get(x))); 
    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    llp.setMargins(10, 10, 10, 10); // 4 margin values for Top/Left/Right/Bottom
    linearLayout.addView(imageHor, llp);
}

linearLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < ImagesList.size(); i++) {   
                indexNumber.add(i);
                int index = indexNumber.indexOf(i);
                Log.e("IndexValue=====", "" + index);
                awesomePager.setCurrentItem(index, true);
            }
        }
});
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Nitesh Khosla
  • 875
  • 8
  • 20